Umal Stack
Umal Stack

Reputation: 241

base-derived class relationship

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.

Upvotes: 5

Views: 7735

Answers (3)

user995502
user995502

Reputation:

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

.

Upvotes: 4

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

When the Derived class object is made the Base class constructor is called first. The Derived class object contains the Base class object. This allows it to call its Base functions. Whereas a Base class object does not contain a Derived class object

enter image description here

Base *b=new Derived(); 

is useful in situations where you can use a single function to handle all derived class objects.

Consider,

Parent class : Animal Derived classes: Dog, Cat etc

Now, you have a function

void doSomethingtoTheAnimal(//take Animal here);

If you were not allowed to assign a base class object to parent reference variable. You will have to create a separate function for Dog, Cat and so on.

void doSomethingtoTheAnimal(Cat *b) or void doSomethingtoTheAnimal(Dog *b)

However, with polymorphism you can use void doSomethingtoTheAnimal(Base *b); Then you can do

Base *b1 = new Dog() or Base *b2 = new Cat();

and use the same function doSomethingtoTheAnimal(b1) or doSomethingtoTheAnimal(b2)

Also, the base class pointer when pointing to a derived class object can only call either the functions in parent class or overriden ones in child class. It is not aware of functions defined exclusively in the child class

Upvotes: 5

Sander De Dycker
Sander De Dycker

Reputation: 16243

Inheritance defines an "is-a" relationship. If Derived has Base as a base class, then Derived is-a Base.

In other words : every Derived object can be treated as a Base, which makes this valid :

Base* b = new Derived();

Dereferencing b allows to access the Base parts of the Derived instance.

This does not work the other way around, because a Base is not a Derived. The following is not valid :

Derived* d = new Base(); // NOT VALID

Upvotes: 2

Related Questions