Hi All,
I know this is a really stupid question but I really needed to ask this !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class A
{
private:
int age;
public:
void printAge(void);
void changeAge(int);
};
class B : public A
{
private:
int pinNo;
public:
void printpin(void);
void changepin (int);
}
So, now when B is created it doesnt cannot access any private variables of A. but does it actually a new object of A which do contain the variables . just that we can't use it. and using printAge() and changeAge() when i change the variable of A (the object associated with an object of B), it does get changed an that change remains inside the object of B. ??
or am I wrong.
in case there is an object of A created for each object of B created and it has all the private variables as well, isn't it in some sense a waste of memory ??
No, derived classes mean in this case, that B is an A. B has the same data members as A, however you can not access private members of A for security reasons (private areas are subject to change, whereas public areas normally don't change by contract).
I don't really get what you mean by "waste of memory" though...
Uh... not really... B does use the values of A. You can still call A's changeAge and printAge functions on an instance of B, that wouldn't work if B didn't have the variables of A.