C++ Function Overriding
In this tutorial, we will learn about function overriding in C++ with the help of examples.
Function Overriding
Function overriding is a feature that allows us to have a same function in child class which is already present in the parent class. The child class inherits the data members and member functions of parent class but if you want to override a function in the child class then you can use function overriding. It is like creating a new version of an old function, in the child class.
As we know, inheritance is a feature of OOP that allows us to create derived classes from a base class.
If derived class defines same function as defined in its base class, This is known as function overriding in C++. The function in derived class overrides the function in base class. Through function overriding you can perform runtime polymorphism.
Example 1: C++ Program to demonstrate function overriding
#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 d1;
d1.print();
return 0;
}
Output
Derived Function
Here, the same function print()
is defined in both Base
and Derived
classes.
So, when we call print()
from the Derived
object d1, the print()
from Derived
is executed by overriding the function in Base
.
Access Overridden Function in C++
To access the overridden function of the base class, we use the scope resolution operator ::
.
We can also access the overridden function by using a pointer of the base class to point to an object of the derived class and then calling the function from that pointer.
Example 2: C++ Access Overridden Function to the Base Class
// C++ program to access overridden function using the scope resolution operator :: in main()
#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 d1, d2;
d1.print();
// access print() function of the Base class
d2.Base::print();
return 0;
}
Output
Derived Function Base Function
Here, this statement
d2.Base::print();
accesses the print()
function of the Base class.
Example 3: C++ Call Overridden Function From Derived Class
// C++ program to call the overridden function from a member function of the derived class
#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 overridden function
Base::print();
}
};
int main() {
Derived d1;
d1.print();
return 0;
}
Output
Derived Function Base Function
In this program, we have called the overridden function inside the Derived
class itself.
class Derived : public Base {
public:
void print() {
cout << "Derived Function" << endl;
Base::print();
}
};
Notice the code Base::print();
, which calls the overridden function inside the Derived
class.
Example 4: C++ Call Overridden Function Using Pointer
As we have seen above that when we make the call to function (involved in overriding), the child class function (overriding function) gets called. What if you want to call the overridden function by using the object of child class. You can do that by creating the child class object in such a way that the reference of parent class points to it. Lets take an example to understand it.
// C++ program to access overridden function using pointer
#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 d1;
// pointer of Base type that points to derived1
Base* ptr = &d1;
// call function of Base class using ptr
ptr->print();
return 0;
}
Output
Base Function
In this program, we have created a pointer of Base
type named ptr. This pointer points to the Derived
object d1.
// pointer of Base type that points to derived1
Base* ptr = &d1;
When we call the print()
function using ptr, it calls the overridden function from Base
.
// call function of Base class using ptr
ptr->print();
This is because even though ptr points to a Derived
object, it is actually of Base
type. So, it calls the member function of Base
.
In order to override the Base
function instead of accessing it, we need to use virtual functions in the Base
class.
Things to Remember
- Functions of both parent and child class must have the same name.
- Functions must have the same argument list and return type.
- A function declared static cannot be overridden.
- If a function cannot be inherited, it cannot be overridden.
Function Overloading vs Function Overriding
Function Overloading | Function Overriding |
---|---|
It can be done without inheritance. | It mainly occurs due to inheritance. |
In function overloading it should have a different signature | In function overriding the function signature should be same. |
It is done in the same scope. | It is done in different scopes. |
Number of overloading functions possible | Only one overriding function possible |
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Function Overriding in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Operator Overloading
.