Hi,
I want to ask you that how does an object access its private data members in copy constructor.
The relevant part of the code:
Code:
C::C(const C &obj)
{
x = obj.x;
y = obj.y;
}
Normally the object1 called "obj" cannot access its private data members outside. But in this situation it can access. How can it be explained?
Here are the complete code:
Code:
#include <iostream>
using namespace std;
class C{
public:
C(int,int);
C(const C &);
int area();
private:
int x;
int y;
};
C::C(int a,int b)
{
x = a;
y = b;
}
C::C(const C &obj)
{
x = obj.x;
y = obj.y;
}
int C::area () {
return x*y;
}
int main()
{
C object1(2,3);
C object2(object1);
cout << "Object 1 : " << object1.area() << endl;
cout << "Object 2 : " << object2.area() << endl;
return 0;
}