C++ override inherited methods

37,013

Solution 1

Currently Child::init is hiding Father::init, not overriding it. Your init member function needs to be virtual in order to get dynamic dispatch:

virtual void init () {
    cout << "I'm the father" << endl;
};

Optionally, you could mark Child::init as override to be explicit that you want to override a virtual function (requires C++11):

void init () override {
    cout << "I'm the child" << endl;
};

Solution 2

You should define the function with function specifier virtual

For example

#include <iostream>

using namespace std;

class Father {
    public:
        virtual ~Father() {}

        void start () {
            this->init();
        };

        virtual void init () const {
            cout << "I'm the father" << endl;
        };
};

class Child: public Father {
    void init () const override {
        cout << "I'm the child" << endl;
    };
};

int main()
{
    Child child;
    child.start();

    return 0;
}

Otherwise function start searches name init in the scope of its own class. And because function init is not virtual that is it is not overriden in the derived class the base class function init is called.

Solution 3

If you want the child to override the init method, you must make the init method in the base class virtual.

class Father {
    public:
        void start () {
            this->init();
        };

        virtual void init () {
            cout << "I'm the father" << endl;
        };
};

A class that re-declares and re-implements a virtual method of one of its bases, is said to override that method. In order for late binding to occur for a method, you need to declare that method virtual.

Share:
37,013

Related videos on Youtube

pistacchio
Author by

pistacchio

Updated on July 09, 2022

Comments

  • pistacchio
    pistacchio almost 2 years

    I have the following two classes. Since Child inherits from Father, I think that Child::init() overrides Father::init(). Why, when I run the program, I get "I'm the Father" and not "I'm the Child"? How to execute Child::init()?

    You can test it here: https://ideone.com/6jFCRm

    #include <iostream>
    
    using namespace std;
    
    class Father {
        public:
            void start () {
                this->init();
            };
    
            void init () {
                cout << "I'm the father" << endl;
            };
    };
    
    class Child: public Father {
        void init () {
            cout << "I'm the child" << endl;
        };
    };
    
    int main (int argc, char** argv) {
        Child child;
        child.start();
    }
    
    • James Adkison
      James Adkison over 8 years
      You didn't make the init function virtual (i.e., virtual void init();).
    • aslg
      aslg over 8 years
      Because init is not virtual, and start belongs to the Father class so it will call the init defined there.
    • Shark
      Shark over 8 years
      Child's init() doesn't override Father's init() Basically Child::start() is Father::init().
    • Martin James
      Martin James over 8 years
      I'm surprised that no warning is given for hiding a parent method:(
    • Shark
      Shark over 8 years
      Shadowing isn't something to warn about, as sometimes that's really what the coder wants :/
    • Андрей Беньковский
      Андрей Беньковский over 8 years
      Correct me if I'm wrong. init() is supposed to only be called from start(). If so, I'd make init() private in both Father and Child, not only in Child like it is now.
    • RyanP
      RyanP over 8 years
      @АндрейБеньковский If you make init() private, than Child will not be able to see that it exists, and will not be able to override it. init() needs to be made virtual so that derived classes can override it, and therefore it can be at most protected instead of public
    • Андрей Беньковский
      Андрей Беньковский over 8 years
      @RyanP C++ allows overriding private virtual methods. coliru.stacked-crooked.com/a/7e6f146be04bfe76. More on that topic: isocpp.org/wiki/faq/strange-inheritance#private-virtuals
  • Sridhar Thiagarajan
    Sridhar Thiagarajan over 4 years
    when there is not virtual, this evaluates to a pointer to base class at compile time since there is not late binding, is this what you mean?
  • Sridhar Thiagarajan
    Sridhar Thiagarajan over 4 years
    when there is not virtual, this evaluates to a pointer to base class at compile time since there is not late binding, is this what you mean? Because if it is not virtual, and init was public, child.init() prints child. So is this pointer the reason for this behaviour?
  • Job_September_2020
    Job_September_2020 over 3 years
    The other purpose of the "override" keyword is to let the compiler check and make sure that the both methods in the base and derived class have the same signatures. If the method in the derived class uses the keyword "override" but its signature does not match any method in the base class, then the compiler will error out.