Virtual function inheritance
Asked Answered
S

3

13

I have a confusion about the inheriting the virtual property of a method.

Let's suppose we have 4 classes: class A, class B, class C and class D. The classes are inherited by this way: A -> B -> C -> D, where A is the base class.

By this time, I'm sure about this: Beginning the class method declaration with virtual in a base class (class A), makes the method virtual for all classes derived from the base class, including the derived ones of the derived classes. (B and C class methods determined to be virtual).

The confusion is here. What if, in the base class A, there wouldn't be any virtual member. Instead, let's say, that class B declares a method to be virtual. I assume, that this change would make the function virtual for all the derived classes that belong to the inheriting chain (C and D classes). So logically, B for C and D, is a sort of their "base class", right? Or am I wrong?

Sanity answered 24/7, 2013 at 18:57 Comment(0)
O
23

You're correct.

I think that in this case the best solution is to try:

#include <iostream>

using namespace std;

class A {
   public:
      void print(){ cout << "print A" << endl; };
};

class B: public A {
    public:
       virtual void print(){ cout << "print B" << endl; };
};

class C: public B {
     public:
        void print(){ cout << "print C" << endl; };
};

int main()
{
   A *a = new C();
   B *b = new C();

   a->print(); // will print 'print A'
   b->print(); // will print 'print C'

   return 1;
}
Objectivity answered 24/7, 2013 at 19:10 Comment(0)
I
6

You are entirely correct. Child inherits what its ancestors have. Base classes can't inherit what the child has (such as a new function or variable). Virtual functions are simply functions that can be overridden by the child class if the that child class changes the implementation of the virtual function so that the base virtual function isn't called.

A is the base class for B,C,D. B is a the base class for C, D. and C is the base class for D too.

Intumesce answered 24/7, 2013 at 19:1 Comment(3)
Quick suggestion: if the base class has a virtual method, should I highlight the rest of virtual methods that belong to the derived classes with the keyword virtual? Just to be more expressive.Sanity
Not it is not necessary. Doing it once in the base class gives all the child classes the ability to change the function implementation(its all inherited).Intumesce
@RobertEagle Adding virtual or override in derived classes are not necessary* , but I would say yes, it's a good practice to put the visual reminder of the function's semantics wherever possible. *unless using a compiler like g++ that offers warnings if these qualifiers are missing, and you tell to treat warnings as errors.Shutout
M
0

Of course you can do it. Virtual method is optional to override so it doesn't matter that you declare it in class A or B. If you dont want to use that method in class A then simply declare in in class B.

Mauretta answered 24/7, 2013 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.