Shri. B. V. V.
Sangha’s Basaveshwar Engineering College, Bagalkote Department
of Electronics and Communication Engineering
Prepared by
Dr. NAGARATHNA RAJUR
Object Oriented Programming Using C++
Function Overriding in C++
A function is a block of statements that together performs a specific task by taking
some input and producing a particular output. Function overriding in C++ is
termed as the redefinition of base class function in its derived class with the same
signature i.e. return type and parameters. It can be of both type: Compile Time and
Runtime Polymorphism.
What is Function Overriding in C++?
Function overriding is a type of polymorphism in which we redefine the member
function of a class which it inherited from its base class. The function signature
remains same but the working of the function is altered to meet the needs of the
derived class. So, when we call the function using its name for the parent object,
the parent class function is executed. But when we call the function using the child
object, the child class version will be executed.
Real-Life Example of Function Overriding
The best Real-life example of this concept is the Constitution of India. India took
the political code, structure, procedures, powers, and duties of government
institutions and set out fundamental rights, directive principles, and the duties of
citizens of other countries and implemented them on its own; making it the biggest
constitution in the world.
Another Development real-life example could be the relationship between
RBI(The Reserve Bank of India) and Other state banks like SBI, PNB, ICICI, etc.
Where the RBI passes the same regulatory function and others follow it as it is.
Function Overriding
Function Overriding using Virtual Function
Function overriding is performed at the runtime, which means that function call
will be binded to its definition during runtime (also known as late binding or
dynamic binding). This can be done with the help of virtual functions.
class Base {
public:
virtual func()
// definition
};
class Derived : public Base {
public:
func() override
// new definition
};
Here, override keyword tells the compiler that the given overridden function
should be declared as virtual in the parent class. It is a kind of double check as
the program can compile without errors even if the function is not virtual. But
then, it will be compile time polymorphism and we won't get the desired
behaviour of the function overriding.
// C++ Program to illustrate how to implement
// function overriding using virtual function
#include <iostream>
using namespace std;
class Base {
public:
// Declare the function as virtual to allow overriding
// in derived classes
virtual void display()
{
cout << "Display method of Base class" << endl;
// Virtual destructor to ensure proper cleanup of
// derived class objects
virtual ~Base() {}
};
class Derived : public Base {
public:
// Override the display method
void display() override
cout << "Display method of Derived class" << endl;
};
int main()
Base* basePtr;
Derived derivedObj;
// Point base class pointer to derived class object
basePtr = &derivedObj;
// Call the display function
// This will call the display method of the Derived
// class due to the virtual function mechanism
basePtr->display();
return 0;
Output
Display method of Derived class
Advantages of Function Overriding
The following are the main advantages of function overriding:
Methods are selected at runtime based on object type.
Common interfaces and implementations can be reused as we only need to
implement the interface for base class.
Changes in base classes automatically affect derived classes.
Facilitates the implementation of design patterns different design patterns.
Client code interacts with base class interface, not specific implementations
reducing the coupling.
Limitations of Function Overriding
While runtime function overriding provides numerous advantages, it also comes
with certain limitations:
Virtual function calls are generally slower than non-virtual function calls due to
the indirection involved in the virtual table lookup.
Implementing and maintaining polymorphic code can be more complex
compared to simpler non-polymorphic code.
Virtual tables and virtual pointers add to the memory overhead, especially in
large systems with many classes.
Errors related to function overriding (e.g., incorrect signatures) might only be
detected at runtime, leading to potential bugs.
More Examples
Example 1: C++ Program to Call Overridden Function From Derived Class
// C++ program to demonstrate function overriding
// by calling the overridden function
// of a member function from the child class
#include <iostream>
using namespace std;
class Parent {
public:
void BEC_Print()
cout << "Base Function" << endl;
};
class Child : public Parent {
public:
void BEC_Print()
cout << "Derived Function" << endl;
// call of original function
Parent::BEC_Print();
};
int main()
Child Child_Derived;
Child_Derived.BEC_Print();
return 0;
Output
Derived Function
Base Function
Example 2: C++ Program to Call Overridden Function Using Pointer
// C++ program to access overridden function using pointer
// of Base type that points to an object of Derived class
#include <iostream>
using namespace std;
class Parent {
public:
void BEC()
cout << "Base Function" << endl;
};
class Child : public Parent {
public:
void BEC()
cout << "Derived Function" << endl;
};
int main()
Child Child_Derived;
// pointer of Parent type that points to derived1
Parent* ptr = &Child_Derived;
// call function of Base class using ptr
ptr->BEC();
return 0;
Output
Base Function
Example 3: C++ Program to Access of Overridden Function using Child
Class Object
// C++ program to access overridden function
// in main() using the scope resolution operator ::
#include <iostream>
using namespace std;
class Parent {
public:
void BEC()
cout << "Base Function" << endl;
};
class Child : public Parent {
public:
void BEC()
cout << "Derived Function" << endl;
};
int main()
Child Child_Derived;
Child_Derived.BEC();
// access BEC() function of the Base class
Child_Derived.Parent::BEC();
return 0;
Output
Derived Function
Base Function
Overriding without Virtual Function
Virtual function is essential for function overriding. If we don't use virtual
function, the function call and the definition is bound at the compilation of the
program. This is not overriding but instead, the redefined function only hides the
base class function with the same name in the derived class making it behave like
an overridden function in some cases.
class Parent {
access_modifier :
// overridden function
return_type name_of_the_function() {}
};
class child : public Parent {
access_modifier :
// overriding function
return_type name_of_the_function() {}
};
// C++ program to demonstrate compile time function overriding
#include <iostream>
using namespace std;
class Parent {
public:
void BEC_Print()
{
cout << "Base Function" << endl;
};
class Child : public Parent {
public:
void BEC_Print()
cout << "Derived Function" << endl;
};
int main()
Child Child_Derived;
Child_Derived.BEC_Print();
return 0;
Output
Derived Function
Function Overloading Vs Function Overriding
Function Overloading Function Overriding
It falls under Compile-Time It can be both Compile Time or
polymorphism Runtime Polymorphism
A function cannot be overridden
A function can be overloaded multiple
multiple times as it is resolved at Run
times as it is resolved at Compile time
time
Cannot be executed without
Can be executed without inheritance
inheritance
They are in the same scope They are of different scopes.
Inheritance in C++
The capability of a class to derive properties and characteristics from another class
is called Inheritance. Inheritance is one of the most important features of Object-
Oriented Programming in C++. In this article, we will learn about inheritance in
C++, its modes and types along with the information about how it affects different
properties of the class.
Syntax
class DerivedClass : mode_of_inheritance BaseClass {
// Body of the Derived Class
};
where mode of inheritance controls the access level of the inherited members of
the base class in the derived class. In C++, there are 3 modes of inheritance:
Mode Description
Public Public member of the base class will become public in the
Inheritance derived class and protected members of the base class will
Mode become protected in the derived class.
Protected
Both public and protected members of the base class will
Inheritance
become protected in the derived class.
Mode
Both public members and protected members of the base class
Private
will become private in the derived class. Private mode is
Inheritance
the default mode that is applied when we don't specify any
Mode
mode.
Access Base Class Members
Members of the base class can be accessed in the derived class by simply using
their name.
class Base {
public:
int n;
void printN() {
cout << n << endl;
};
// Inheriting Base class publicly
class Derived : public Base {
public:
void func () {
// Accessing Base class members
n = 22;
};
The public members of the Base class can be accessed through the objects of
the Derived class if the Base class is inherited publicly as in the above example.
#include <bits/stdc++.h>
using namespace std;
class Base {
public:
int n;
void printN() {
cout << n << endl;
}
};
// Inheriting Base class publicly
class Derived : public Base {
public:
void func () {
// Accessing Base class members
n = 22;
};
int main() {
// Creating objects of derived
Derived d;
// Accessing Derived class member
d.func();
// Accessing Base class member
d.printN();
return 0;
}
Output
22
The private members in the base class cannot be directly accessed in the derived
class, while protected and public members can be directly accessed. To access or
update the private members of the base class in derived class, we have to use the
corresponding getter and setter functions of the base class or declare the derived
class as friend class.
#include <bits/stdc++.h>
using namespace std;
// Base class that is to be inherited
class Parent {
public:
int id_p;
Parent(int x = 22) : id_p(x) {}
void printID_p() {
cout << "Base ID: " << id_p << endl;
};
// Derived publicly inheriting from Base
// Class
class Child : public Parent {
public:
int id_c;
Child(int x = 22) : id_c(x) {}
void printID_c() {
cout << "Child ID: " << id_c << endl;
};
int main() {
Child obj1;
// An object of class child has all data members
// and member functions of class parent
// so we try accessing the parents method and data from
// the child class object.
obj1.id_p = 7;
obj1.printID_p();
// finally accessing the child class methods and data
// too
obj1.id_c = 91;
obj1.printID_c();
return 0;
Output
Base ID: 7
Child ID: 91
Explanation: In the above program, the 'Child' class is publicly inherited from
the 'Parent' class so the public data members of the class 'Parent' will also be
inherited by the class 'Child'.
Types Of Inheritance in C++
The inheritance can be classified on the basis of the relationship between the
derived class and the base class. In C++, we have 5 types of inheritances:
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
1. Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e. one
base class is inherited by one derived class only.
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
};
// Sub class derived from a single base classes
class Car : public Vehicle {
public:
Car() {
cout << "This Vehicle is Car"<< endl;
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
Output
This is a Vehicle
This Vehicle is Car
2. Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than
one class. i.e one subclass is inherited from more than one base class.
#include <bits/stdc++.h>
using namespace std;
class LandVehicle {
public:
LandVehicle() {
cout << "This is a LandVehicle"<< endl;
};
class WaterVehicle {
public:
WaterVehicle() {
cout << "This is a WaterVehicle"<< endl;
};
// sub class derived from two base classes
class AmphibiousVehicle : public WaterVehicle, public LandVehicle {
public:
AmphibiousVehicle() {
cout << "This is an AmphibiousVehicle"<< endl;
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes.
AmphibiousVehicle obj;
return 0;
Output
This is a WaterVehicle
This is a LandVehicle
This is an AmphibiousVehicle
3. Multilevel Inheritance
In multilevel inheritance , a derived class is created from another derived class
and that derived class can be derived from a base class or any other derived class.
There can be any number of levels. For example, a vehicle can be a four-wheeler,
and a four-wheeler vehicle can be a car.
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
};
class fourWheeler : public Vehicle {
public:
fourWheeler() {
cout << "4 Wheeler Vehicles"<< endl;
}
};
class Car : public fourWheeler {
public:
Car() {
cout << "This 4 Wheeler Vehical is a Car";
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
Output
This is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehical is a Car
4. Hierarchical Inheritance
In hierarchical inheritance , more than one subclass is inherited from a single base
class. i.e. more than one derived class is created from a single base class. For
example, cars and buses both are vehicle.
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
};
class Car : public Vehicle {
public:
Car() {
cout << "This Vehicle is Car"<< endl;
};
class Bus : public Vehicle {
public:
Bus() {
cout << "This Vehicle is Bus"<< endl;
};
int main() {
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus
5. Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance will create hybrid inheritance in C++.
There is no particular syntax of hybrid inheritance. We can just combine two of
the above inheritance types. Below image shows one of the combinations of
hierarchical and multiple inheritances:
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
};
class Fare {
public:
Fare() {
cout << "Fare of Vehicle"<< endl;
};
class Car : public Vehicle {
public:
Car() {
cout << "This Vehical is a Car"<< endl;
};
class Bus : public Vehicle, public Fare {
public:
Bus() {
cout << "This Vehicle is a Bus with Fare";
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
Output
This is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare
Multipath Inheritance
This is the special case of special case of hybrid inheritance. In multipath
inheritance, a class is derived from two base classes and these two base classes in
turn are derived from one common base class. An ambiguity can arise in this type
of inheritance in the most derived class. This problem is also called diamond
problem due to the diamond shape formed in the UML inheritance diagram.
Effects of Inheritance
Let's see how different components of class are affected in inheritance:
Static Members and Inheritance
In C++, static members belong to the class itself, not to any object. This means
static variables and methods are shared across all instances of the class. When it
comes to inheritance, static members from the base class are not inherited by the
derived class in the traditional way. However, they can still be accessed using the
class name like className::staticMember.
Friend Function and Class in Inheritance
Friend functions and classes in inheritance provides functions or classes to access
private and protected members of a class, providing flexibility and better control
over class interactions. In inheritance, friend function and classes are not
inherited by the base class. It means that the classes and functions declared as
friends for the base class does not automatically become a friend for derived
class.
Constructors and Destructors in Inheritance
Constructors and Destructors are generally defined by the programmer and if not,
the compiler automatically creates them, so they are present in every class in C+
+. Now, the question arises what happens to the constructor and destructor when
a class is inherited by another class.
In C++ inheritance, the constructors and destructors are not inherited by the
derived class, but we can call the constructor of the base class in derived class.
The constructors will be called by the complier in the order in which they are
inherited. It means that base class constructors will be called first, then derived
class constructors will be called.
The destructors will be called in reverse order in which the compiler is
declared.
We can also call the constructors and destructors manually in the derived class.
Example:
#include <iostream>
using namespace std;
class Parent {
public:
// base class constructor
Parent() { cout << "Inside base class" << endl; }
};
// sub class
class Child : public Parent {
public:
// sub class constructor
Child() { cout << "Inside sub class" << endl; }
};
int main() {
// creating object of sub class
Child obj;
return 0;
Output
Inside base class
Inside sub class
Polymorphism in Inheritance
In Inheritance, we can redefine the base class member function in the derived
class. This type of inheritance is called Function Overriding. Generally, in other
programming languages, function overriding is runtime polymorphism but in C+
+, we can do it at both runtime and compile time. For runtime polymorphism, we
have to use the virtual functions.
Example:
#include <bits/stdc++.h>
using namespace std;
class Parent {
public:
void BEC_Print() {
cout << "Base Function" << endl;
};
class Child : public Parent {
public:
void BEC_Print() {
cout << "Derived Function";
};
int main() {
Child Child_Derived;
Child_Derived.BEC_Print();
return 0;
}
Output
Derived Function
Inheritance vs Polymorphism
Inheritance and Polymorphism both works differently. Inheritance allows a new
class to inherit properties from an existing class, promoting code reuse, while
polymorphism enables a class to perform tasks in different ways, depending on
the method used. Inheritance focuses on class relationships, and polymorphism
focuses on method behaviour.