Function Overriding in C++
To understand the topic, we must break down the terms. Let us first understand functions. In C++ or any programming language, functions are a block of code designed to perform a specific task and get executed only when called(invoked). Generally, functions take some input, process it, and give an output. We can better understand functions with a real-life examples. We give input to a calculator, which processes the inputs and gives output after performing specific tasks. Each operation on a calculator can be considered as a function.
Function Overriding
“Function overriding is an essential concept of object-oriented programming. It helps in achieving polymorphism. Polymorphism is a fundamental concept of object-oriented programming (OOPs), which allows objects of different classes to be treated as if they belong to one class.”
In function overriding, a method is defined in the derived class with the same name, return type, and parameters as in the base class. When an object of the derived class accesses the method, the method defined in the derived class get accessed. This is called method overriding.
Implementing Function Overriding in C++
In C++, function overriding is a way to implement polymorphism. Here we will see how we can implement function overriding in C++.
The syntax for Function Overriding
The syntax for implementing function overriding in C++ involves the declaration of the identical function signatures in both the derived class and base class, along with the virtual keyword in the base class definition.
Here is the stepwise syntax for the implementation of function overriding in C++.
1. Define the base class.
Syntax:
class BaseClass {
Public:
virtual void methodName() {
// Base class implementation
}
};
2. Define derived class
Syntax:
Class DerivedClass : public BaseClass {
Public:
void methodName() {
// Derived class implementation
}
};
3. Make an object of the derived class
Syntax:
DerivedClass object;
4. Call the overridden method
Syntax:
object.method name();
When the overridden method is called using the object of the derived class, the method implemented in the derived class is executed. This is how function overriding is achieved in C++. This provides a great facility and flexibility, as the user can later modify the previously defined methods with the help of function overriding. It is necessary to keep in mind that to implement function overriding correctly, we must have to declare a virtual keyword before the base class definition.
Algorithm for Function Overriding
Now let us develop an algorithm to achieve function overriding.
Algorithm:
- Create a base class with a virtual function you want to override in the derived class.
- In the derived class, define a function with the same name, return type, and arguments as the virtual function in the base class.
- Use the keyword "override" after the function definition in the derived class to indicate that it overrides the base class's virtual function.
- Inside the overridden function in the derived class, write the implementation using the same name, return type, and arguments as the virtual function in the base class.
- When calling the overridden function, use a pointer or reference to the derived class object to ensure that the overridden function is called instead of the virtual function in the base class.
Program to Implement Function Overriding
Now let us develop a program based on the above algorithm.
Program 1
#include <iostream>
class Shape {
public:
virtual void draw() {
std::cout << "Drawing a shape" << std::endl;
}
};
class Circle : public Shape {
public:
void draw() {
std::cout << "Drawing a circle" << std::endl;
}
};
class Rectangle : public Shape {
public:
void draw() {
std::cout << "Drawing a rectangle" << std::endl;
}
};
int main() {
Shape* shapes[2];
shapes[0] = new Circle();
shapes[1] = new Rectangle();
for (int i = 0; i < 2; i++) {
shapes[i]->draw();
}
return 0;
}
Output:
![Function Overriding in C++](https://static.tutorialandexample.com/cpp/function-overriding-in-cpp1.png)
Program 2:
// C++ program to access overridden function
// in main() using the scope resolution operator ::
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "Derived Function" << endl;
}
};
int main() {
Derived derived1, derived2;
derived1.print();
// access print() function of the Base class
derived2.Base::print();
return 0;
}
Output:
![Function Overriding in C++](https://static.tutorialandexample.com/cpp/function-overriding-in-cpp2.png)
Program 3:
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "Derived Function" << endl;
// Call the overridden function in the base class
Base::print();
}
};
int main() {
Derived derived1;
derived1.print();
return 0;
}
Output:
![Function Overriding in C++](https://static.tutorialandexample.com/cpp/function-overriding-in-cpp3.png)