C++ Polymorphism Overriding
Polymorphism in C++ allows objects to take on multiple forms. One of the ways to implement polymorphism is through overriding. Overriding a function allows a derived class to provide its own implementation of a function that is already defined in the base class.
Syntax
To override a function in C++, the derived class must define a function with the same name, return type, and parameters as the function in the base class. The keyword "override" can also be used in the derived class to indicate that the function is intended to override a function in the base class.
class Base {
public:
virtual void display() {
cout << "This is the display function in the base class." << endl;
}
};
class Derived : public Base {
public:
void display() override {
cout << "This is the display function in the derived class." << endl;
}
};
Example
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "This is the display function in the base class." << endl;
}
};
class Derived : public Base {
public:
void display() override {
cout << "This is the display function in the derived class." << endl;
}
};
int main() {
Base *b;
Derived d;
b = &d;
b->display();
return 0;
}
Output
This is the display function in the derived class.
Explanation
In this example, we create a base class Base
with a virtual function display()
. We then create a derived class Derived
, which overrides the display()
function with its own implementation. In the main()
function, we create a pointer b
of type Base
, and we assign the address of an object of type Derived
to it. When we call b->display()
, the function in the Derived
class is called because the object that b
points to is of type Derived
.
Use
Overriding is often used in object-oriented programming to create a more specialized implementation of a method for a particular class. For example, if a base class has a calculate()
method, a derived class that represents a circle could override the calculate()
method to calculate the area of the circle.
Important Points
- To override a function in C++, the derived class must define a function with the same name, return type, and parameters as the function in the base class.
- The keyword "override" can be used in the derived class to indicate that the function is intended to override a function in the base class.
- The base class function must be marked as virtual to enable polymorphic behavior.
- When we call a virtual function through a pointer or reference to a base class, the function called is determined at runtime based on the actual type of the object pointed to or referred to.
Summary
Overriding in C++ allows a derived class to provide its own implementation of a function that is already defined in the base class. To override a function, the derived class must define a function with the same name, return type, and parameters as the function in the base class. The base class function must be marked as virtual to enable polymorphic behavior. When we call a virtual function through a pointer or reference to a base class, the function called is determined at runtime based on the actual type of the object pointed to or referred to.