Q:

C++ program to demonstrate calling of private member functions inside public member function.

belongs to collection: C++ programs on various topics

0

C++ program to demonstrate calling of private member functions inside public member function.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ program (Code Snippet) – Access private member functions inside public member function in C++ class

Let’s consider the following example:

/*C++ program to demonstrate calling of 
private member functions inside public member function*/

#include <iostream>

using namespace std;

//class definition
class A{
	private:
		int a;
		int b;
		//set value of a
		void set_a(int a){
			this->a=a;
		}
		//set value of b
		void set_b(int b){
			this->b=b;
		}
	public:
		void getValues(int x,int y){
			set_a(x); //calling private member function
			set_b(y); //calling private member function
		}
		void putValues(){
			//printing values of private data members a,b
			cout<<"a="<<a<<",b="<<b<<endl;
		}
};

int main(){
	//creating object
	A objA;
	
	//set values to class data members
	objA.getValues(100,200);
	//print values
	objA.putValues();
	
	return 0;
}

Output

            a=100,b=200

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to demonstrate use of protected data m... >>
<< C++ - program for Array of Structure....