C++ Inheritance: Overriding Base Class Methods (Pt. 4)
Oftentimes when defining a derived class, you need to add specific functionality to a method inherited from the base class to suit the needs of the subclass. This process is called overriding.
There is an important distinction between overriding and overloading. Overriding refers to the process of redefining a method inherited from the base class that contains the same parameters. Whereas, overloading refers to the process of defining a method in the derived class with the same name but different parameters.
An example of method overriding is:
// The Base Class
class Monster {
public:
string Shout() {
return "WSDFSFDS";
}
private:
int hearts;
}
// Derived Class
class Wumpus : public Monster {
public:
string Shout() {
return "wummmmmwummmmmwummmmmwummmmm";
}
private:
bool isVisible;
}
int main() {
Wumpus derived;
cout << derived.Shout() << endl;
return 0;
}
Calling main()
would produce the following output:
$ ./monster
wummmmmwummmmmwummmmmwummmmm
$ echo $?
0
Calling Base Class Functions
Sometimes when overriding functions you wish to add functionality at the derived class level instead of completely overhauling the inherited function. To call a base class function within a derived class, you would write the base class name followed by the scope resolution operator (::
):
// The Base Class
class Monster {
public:
string Shout() {
return "WSDFSFDS";
}
private:
int hearts;
}
// Derived Class
class Wumpus : public Monster {
public:
string Shout() {
return Monster::Shout() + "wummmmmwummmmmwummmmmwummmmm";
}
private:
bool isVisible;
}
When calling the base class method within the derived class method override, the derived class Shout()
method would now output:
$ ./monster
WSDFSFDSwummmmmwummmmmwummmmmwummmmm
$ echo $?
0