Why isn't any change in a Base class member reflected in the Derived class?

Here's the snippet of my code:
class Base
{
public:
int a;
};
class D1:public Base
{
public: int b;
};
class D2:public Base
{
public: int c;
};
class D3:public D1,public D2
{
public: int d;
};
void main()
{
D3 ob; Base obb;
obb.a=8097;
ob.b=2;
ob.c=3;
ob.d=4;
cout<<ob.D1::a<<endl<<ob.b<<endl<<ob.c<<…
cout<<ob.D2::a<<endl;
cout<<obb.a;
}

I wanna know, when I made a change in "a" of the Base class, why isn't the same reflected in the Derived class? And if the changes in the Base class are not reflected in the Derived class(es) then doesn't it become more like borrowing 'memory' rather than members(not talking about member functions)?

Also, for ob.D1::a and ob.D2::a the compiler produces garbage values.
Last edited on
What do you mean? You have two instances of class 'Base' in that code. One if them is a 'Base' called 'obb' and the other is a 'D3' called 'ob'. If you change things in one instance of a class why would they be changed in another instance?
1
2
D3 ob; //An object of type D3 named ob
Base obb; //An object of type Base named obb 


These 2 objects are completely separate entities. ob.D1::a != obb.a if you wanted 2 seperate objects of the same class to share the same data members value, then you would have to make static int a. I don't think this was what you were attempting though. You have proven that changes in the base class are reflected in the derived class. Both of your objects, one being base and one being derived have the member variable a, them not storing the same value is because they are separate objects.
Thanks clanmjc!
Topic archived. No new replies allowed.