Inheritance of a Pure Virtual Function

use of pure virtual function in c++
pure virtual function in c++ with example program
pure virtual function c++
virtual function in multilevel inheritance c
c++ virtual function = 0
c++ abstract class constructor
virtual function allows you to
how to override a pure virtual function in c++

Could somebody please clear these concepts and answer these questions. Thank you in advance.

Class A is a base class and has a pure virtual function named display.

class A 
{ 
  public:
       virtual void display()const=0;

};

Class B is a derived class that inherits the A class. Furthermore, B overrides the display function,

class B:public A 
{ 
  public:
       void display()const{cout<<"Always"<<endl;}

};

Q1: Now B contains 1 overridden display and 1 inherited pure virtual display, right? Or does the pure virtual one become non-existent due to the override.

I'm asking this because the ultimate question I'm facing is this: Q2: Suppose B did override the display function, Now, if there a new class

class C:public B 
{ 
  public:
};

Now, I'm clear on the concept that the all the first level derived classes (like B) have a must on them that they override the pure virtual function(coming from the class A), suppose that they do override it, now what if the there is a new class C that is derived from one of the first-level-derived classes ( class B ), will it also need to override the pure virtual function (display) or will it be not required as it was overridden in the class B and C inherits B(consequently receiving an override of the the display function?

Suppose there is a virtual base class A

class A 
{ 
  public:
       virtual void display()const=0;

};

.

class B:public A 
{ 
  public:
       void display()const{cout<<"Always"<<endl;}

};

Q1: Now B contains 1 overridden display and 1 inherited pure virtual display, right? Or does the pure virtual one become non-existent due to the override.

Inheritance of functions is replacement. As you say, the base class function becomes non-existent in the derived class. This is true whether or not the base class function is virtual or not.

Q2: Suppose B did override the display function, Now, if there a new class C

class C : public B 
{ 
    ...
};

will it also need to override the pure virtual function (display)?

No, C gets all the functions from B, including the concrete implementation of display()

However, C may optionally override the display() function again. And so on and so forth. There is no limit on how many times a class can be subclassed.

Virtual function, is declared by assigning 0 in declaration. See the following example. derived from it. A pure virtual function is a virtual member function of a base class that must be overridden. When a class contains a pure virtual function as a member, that class becomes an abstract base class.

Q1: Now B contains 1 overridden display and 1 inherited pure virtual display, right?

I don't know what you mean by that.

C doesn't need to override display. If it doesn't, it will just use the one inherited from B.

Pure Virtual Functions and Abstract Classes in C++, What is the difference between a virtual function and a pure virtual function? A pure virtual function simply acts as a placeholder that is meant to be redefined by derived classes. To create a pure virtual function, rather than define a body for the function, we simply assign the function the value 0.

Q1: Now B contains 1 overridden display and 1 inherited pure virtual display, right? Or does the pure virtual one become non-existent due to the override.

Your first question here is meaningless. The class B inherits a pure virtual from A, and overrides it. It does not "contain" either.

A::display() does actually exist, as a validly named entity that can be referred to within B. For example, if A::display() is defined;

class A 
{ 
    public:
        virtual void display()const=0;
};

//   yes, defining a pure virtual function is allowed, albeit optional 
void A::display() const       
{
    std::cout << "A!\n";
}

then B::display() can call it.

class B:public A 
{ 
   public:
       void display()const
       {
           A::display();
           std::cout<<"Always"<<endl;
       }
};

so both A::display() and B::display() exist in this case. The above code for B::display() will compile (since the compiler recognises both A::display() and B::display() as distinct names of functions) regardless of whether A::display() is defined or not. (If A::display() is not defined, the linker will typically report a missing symbol).

Q2: Suppose B did override the display function, Now, if there a new class

class C:public B 
{ 
    public:
};

Now, I'm clear on the concept that the all the first level derived classes (like B) have a must on them that they override the pure virtual function(coming from the class A), suppose that they do override it, now what if the there is a new class C that is derived from one of the first-level-derived classes ( class B ), will it also need to override the pure virtual function (display) or will it be not required as it was overridden in the class B and C inherits B(consequently receiving an override of the the display function?

If B has overridden display(), then C inherits display() as a non-pure virtual function. This means that, if B can be instantiated, then so can C.

If C does not override the inherited display(), then some_c->display() will call the version inherited from B i.e. B::display().

If C does override display() - which is permitted with any inherited virtual function - then it can implement C::display() in any way needed.

Difference Between Virtual and Pure Virtual Function (with , base class, and all the inheriting derived classes has to redefine it. C++ Abstract class and Pure virtual Function. The goal of object-oriented programming is to divide a complex problem into small sets. This helps understand and work with problem in an efficient way. Sometimes, it's desirable to use inheritance just for the case of better visualization of the problem.

Now B contains 1 overridden display and 1 inherited pure virtual display, right?

NO.

You can understand it like this:

Or does the pure virtual one become non-existent due to the override.

When you declare a function virtual then a virtual (function) table is added (to the classes) which holds function pointers to virtual functions. Whenever a override function is declared it replaces the function pointer in the virtual table.

If B override's the function then it's function pointer goes in the virtual table.

C doesn't have to override. It will get the function from B.

C can override the function. Then it's pointer will go into the virtual table.

B doesn't have to override. But then a variable cannot be created of B. Then C should override the function (if you want a variable)

Virtual function inheritance, isn't called. A is the base class for B,C,D. Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

12.6, Pure virtual (abstract) functions and abstract base classes you use multiple inheritance on normal classes, they will let you multiple inherit as many interfaces​  Pure Virtual Functions and Abstract Classes in C++. Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, let Shape be a base class. We cannot provide implementation of function draw() in Shape,

C++ Inheritance with pure virtual functions, There is NO need to change the function signature. Look at following: class BaseItem {public: virtual std::string getDifferences(const BaseItem&  In multilevel inheritance, where a derived class that has inherited the virtual function from its base class, when itself is used as a base class for another derived class, the virtual function still can be overridden. So, when a virtual function is inherited its virtual nature is also inherited.

Default Implementation for Pure Virtual Functions in C++, What all is inherited from parent class in C++? · Virtual Functions and Runtime A pure virtual function (or abstract function) in C++ is a virtual function for which  Memory Layout in Virtual Inheritance In order to keep track of the single instance of the storable object, the compiler will provide a virtual function table (vtable) for classes transmitter and receiver. When a radio object is constructed, it creates one storable instance, a transmitter instance and a receiver instance.

Comments
  • Possible duplicate of C++ Virtual/Pure Virtual Explained
  • @MitchWheat I read the whole thread man, I couldn't find an absolute and to-the-point answer for my question anywhere in it.
  • it will not be required as it was overridden in the class B and C inherits B
  • The confusion may come from how you are looking at this. Class A is an Abstract Base Class because it contains at least 1 Pure Virtual Function declared without definition using =0 instead. As such, Class A cannot be instantiated, but only inherited from. The derived class must override the pure virtual function. Once the derived class does that -- it is simply a class itself. It no longer has the constraint of being an abstract base class (it isn't) so it may be inherited from just as any class may be inherited from with the normal rules of access, etc..
  • A well-known use for Abstract Classes is Interfaces. If you google on that, you get more info.