base-derived class relationship

c++, inheritance

Solution

You have answered your own question but I think what is confusing you is the pointer

Derived *d=new Base();

derived class would have all the information about the base class. but the base class wouldn't know anything about the derived class.

Yes so you are expecting since this is declaread as derived `Derived *d` it should know everything about base. But what matters is what is actually created. In this case you created Base `new Base();` And `Derived` could have added more to `Base`, so we cant use Base as Derived.

However in this case

Base *b=new Derived();

createes a new Derived. But we are only interested on what it inherited from Base. Which is alright.

Because Derived knows what is in base

.

Problem

I have this doubt on base-derived class relationship. i know when we derive an class from the base class, the derived class would have all the information about the base class. but the base class wouldnt know anything about the derived class. so, why is this acceptable? ``` Base *b=new Derived(); ``` and not ``` Derived *d=new Base();. ``` and basically i need to understand the need of the first statement? i mean, what is the use of assigning the derived class object to the base class pointer? Note : This is not an assignment. im in the early stage of learning programming. so basically need to understands the bits and pieces. Please ignore if this is very basic and already asked question.

Original source