0

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?

melpomene
  • 84,125
  • 8
  • 85
  • 148
amm98d
  • 1,997
  • 2
  • 9
  • 15
  • Possible duplicate of [C++ Virtual/Pure Virtual Explained](https://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained) – Mitch Wheat Apr 29 '18 at 08:13
  • @MitchWheat I read the whole thread man, I couldn't find an absolute and to-the-point answer for my question anywhere in it. – amm98d Apr 29 '18 at 08:19
  • it will not be required as it was overridden in the class B and C inherits B – Mitch Wheat Apr 29 '18 at 08:20
  • 1
    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.. – David C. Rankin Apr 29 '18 at 08:42
  • A well-known use for Abstract Classes is [Interfaces](https://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm). If you google on that, you get more info. – JHBonarius Apr 29 '18 at 09:02
  • The "first level" `B` doesn't have to override *all* pure virtual functions, it can leave some of them for further derived classes to override. That way `B` would also be abstract, just like `A`. – Bo Persson Apr 29 '18 at 09:12
  • >> Now B contains 1 overridden display and 1 inherited pure virtual display.<< Wrong. It contains virtual member function `void display() const` that overrides abstract virtual function from class A. Note: "method" and "pure" not quite correct term in C++. Mostly because there are static member functions that ARE NOT methods in OOP meaning of it. Also operators aren't methods , but they can be members. – Swift - Friday Pie Apr 29 '18 at 12:01
  • @AhmedMustafaMalik feel free to accept an answer – iPherian May 02 '18 at 13:19

4 Answers4

2

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.

iPherian
  • 908
  • 15
  • 38
0

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
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.

Peter
  • 35,646
  • 4
  • 32
  • 74
0

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)

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31