Click here to Skip to main content
15,867,874 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Without use copy constructor ..........
Posted
Comments
Volynsky Alex 25-May-13 11:45am    
You can add a public method as member of your class....
Volynsky Alex 25-May-13 11:52am    
In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends.
Friends are functions or classes declared with the friend keyword.
If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend:

class A;

class B {
int width, height;

void foo (A a);
};

class A {
private:
int x;

friend class B;
};

void B::foo (A a) {
width = a.x;
height = a.x;
}

1 solution

You can encapsulate your variables example:
http://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htm[^]

or you can create methos to set and get variables
 
Share this answer
 
Comments
Vijay Kumbhani 25-May-13 8:32am    
Class Mainclass
{
private:
int number;
public:
Mainclass(int n):number(n){}
int int n()
{
return this-> number;
}

};

void main()
{
Mainclass(20);
Mainclass main;
int ans = main.n();
cout <<ans<<endl;
}


give me sollution to above examples

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900