Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

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

 

error: Content is protected !!