C++ override inherited methods
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
.
Related videos on Youtube
pistacchio
Updated on July 09, 2022Comments
-
pistacchio almost 2 years
I have the following two classes. Since
Child
inherits fromFather
, I think thatChild::init()
overridesFather::init()
. Why, when I run the program, I get "I'm the Father" and not "I'm the Child"? How to executeChild::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 over 8 yearsYou didn't make the
init
function virtual (i.e.,virtual void init();
). -
aslg over 8 yearsBecause init is not
virtual
, andstart
belongs to theFather
class so it will call theinit
defined there. -
Shark over 8 yearsChild's
init()
doesn't override Father'sinit()
BasicallyChild::start()
isFather::init()
. -
Martin James over 8 yearsI'm surprised that no warning is given for hiding a parent method:(
-
Shark over 8 yearsShadowing isn't something to warn about, as sometimes that's really what the coder wants :/
-
Андрей Беньковский over 8 yearsCorrect me if I'm wrong.
init()
is supposed to only be called fromstart()
. If so, I'd makeinit()
private in bothFather
andChild
, not only inChild
like it is now. -
RyanP over 8 years@АндрейБеньковский If you make
init()
private, thanChild
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 mostprotected
instead ofpublic
-
Андрей Беньковский 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 over 4 yearswhen 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 over 4 yearswhen 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 over 3 yearsThe 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.