• Forum
  • Lounge
  • Questions regarding inheritance and poly

 
Questions regarding inheritance and polymorphism

Hello, could anyone please help clarify several things for me regarding this topic? Here are some questions I have:

Q.When we inherit the features of a derived class, does this mean that we also have inherited the features which the derived class already inherited from?


Q.What exactly is the use of the virtual keyword? Do we only use it whenever we expect to override a certain function in a derived class?


That's all I have for now, any notes that could help make these less confusing would be great.

Thanks!
Last edited on
When we inherit the features of a derived class, does this mean that we also have inherited the features which the derived class already inherited from?


Why don't you try it? Make 3 levels of classes in an inheritance hierarchy, with public , protected and private functions - see what happens :+)


What exactly is the use of the virtual keyword? Do we only use it whenever we expect to override a certain function in a derived class?


Have you read through this? Or is there something in it you don't understand?
http://www.cplusplus.com/doc/tutorial/polymorphism/

The main idea is that one creates an interface of public pure virtual functions in the base class, these functions are overridden in derived classes - do use the C++11 override keyword. The important part is having a container declared with pointers to base class, then populate it with pointers to derived class. When a function is called, the correct derived function is executed.
> The main idea is that one creates an interface of public pure virtual
> functions in the base class
http://www.gotw.ca/publications/mill18.htm#Notes
1. Prefer to make interfaces nonvirtual, using Template Method.
2. Prefer to make virtual functions private.

> The important part is having a container declared with pointers to base class
I fail to see how is that the important part of inheritance/polymorphism.
1. Prefer to make interfaces nonvirtual, using Template Method.
2. Prefer to make virtual functions private.


Ok, that is more advanced - the OP is beginning with this subject. But it is a good thing you mentioned it though :+) The OP could keep that in mind for the future, or have a go at doing it now.

I fail to see how is that the important part of inheritance/polymorphism


Ok, maybe not the most important part - but nonetheless a very useful idea. I mentioned the importance of the contrast between the declaration of the container and what it is populated with because lots of people gets this confused. Lots of times I have seen people unnecessarily trying to cast base pointers to derived pointers, as one example. They don't realise that a pointer to derived is a valid pointer to base.

Thanks for your always expert advice - Regards :+)

Just in case you still hadn't fully comprehended the concept behind inheritance and polymorphism, I'll leave you few notes here.

Q.When we inherit the features of a derived class, does this mean that we also have inherited the features which the derived class already inherited from?


In short, yes.

The long version: (note: try to visualize it on paint if you get confused)
Think of inheritance as copying the contents of one class into another. So suppose you have three classes A, B and C. Suppose that B inherits from A; therefore B is a derived class and A is a base class. Now suppose that C inherits from B. Since B inherits from A, this means all the methods inside of the class A are copied into the class B (granted that the methods of class A are made public). So since C inherits from B, this means all the methods of B (including those already inherited from other classes) are also copied in class C.


Q.What exactly is the use of the virtual keyword? Do we only use it whenever we expect to override a certain function in a derived class?


In short, you use the virtual keyword in a base class whenever you want to give a specific function multiple definitions in different derived classes.

The long version: (note: try to visualize it on paint if you get confused)
When you create a function as virtual, 2 things gets created behind the scene. A virtual pointer, and a virtual table. The virtual pointer will point to the virtual table and store its address. The virtual table can be thought of as an array of pointers to virtual functions, each pointer will store the memory address of a virtual function you have in a class. When you inherit from this base class, the virtual pointer (short for vptr) and virtual table (short for vtable) also get inherited. So you can imagine how costly it can be if you have plenty of virtual functions. When you override a virtual function in your derived class, a pointer in the virtual table will start pointing to it and keep track of it.

Note: A good rule of thumb is to make the destructor virtual whenever you have a virtual function to ensure that a derived object have a chance to clean up after itself. Also, another good rule of thumb is to always put the keyword virtual next to your virtual functions in the derived class (although not necessary because when they are virtual in the base class, they are virtual everywhere) as they can still serve as a reminder that this particular function is virtual.

Hope this helps.
Thank you everybody, especially you Uk Marine.
Topic archived. No new replies allowed.