Method or Function Overriding in C++

Method or Function Overriding in C++

Method or Function overriding means two functions having same name and parameters. But both the functions are declared in different classes. Inheritance allows us to derive classes from existing classes. This allows child (derived) class to access functions or data of parent (base) class. If we declare same function in both child as well as parent class then the function in base is know as overridden function. We will see an example of Function overriding in C++ to get more clear idea of method or function overriding.

Q. Write a program in C++ to demostrate method or function overriding.

Program :

//A function to demonstrate function overriding
#include<iostream>

using namespace std;

class Parent 
{
    public :
        void display()
        {
            cout<<"\n Hey I'm from parent or base class! I'm overridden";
        }
};

class Child : public Parent
{
    public :
      void display()
        {
            cout<<"\n Hey I'm from child or derived class! I'm overriding";
        }
};

int main()
{
    Child ch;
    ch.display();

    return 0;
}

Output :

function overriding in C++

So in this program we declared two functions with same name but in different classes. One in base class and one in child class. As we are using inheritance we create only one object that too of the child class. Child class object has access to both the classes. So when we call ch.display(); function compiler first enters Child class. Here it checks if the display function is available. As it is present it will execute steps in the display() function. After executing all the steps compiler will return to the main function. And program will terminate without going to the Parent class.

Now we will see how to access the overridden function declared in the parent class using object of child class.

Program :

//A function to demonstrate function overriding
#include<iostream>

using namespace std;

class Parent 
{
    public :
        void display()
        {
            cout<<"\n Hey I'm from parent or base class! I'm overridden";
        }
};

class Child : public Parent
{
    public :
      void display()
        {
            cout<<"\n Hey I'm from child or derived class! I'm overriding";
        }
};

int main()
{
    Child ch;
    ch.display();

    ch.Parent::display();  //Calling base class function using derived class object
   
    return 0;
}

Output :

function overriding in C++

That’s how we access overridden functions in C++ using object of base class. We simply use scope resolution operator (::). Can we access Parent class overridden functions from base class? Let’s find out.

Program to access Parent class overridden function from base class

//A function to demonstrate function overriding
#include<iostream>

using namespace std;

class Parent 
{
    public :
        void display()
        {
            cout<<"\n Now we are in base class!";
        }
};

class Child : public Parent
{
    public :
        void display()
        {
            Parent::display();  //Calling base class function in derived class
            cout<<"\n And now we are in base class again.";
        }
};

int main()
{
    Child ch;
    ch.display();

    return 0;
}

Output :

function overriding in C++

So this was the concept of function overriding in C++. And we have seen every way of accessing overridden and overriding functions.

Program Parts :

1. #include is a pre-processor directive. It is used to include header files.
2. iostream is header file which has certain commands that c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. class is a keyword to create classes.
5. int is data type. It indicates that the program returns a value.
6. main() is main function that indicates the compiler that the user written programs starts from here.
7. cout is the keyword use to print a message.
8. cin is the keyword use to store values in variables.
9. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion

This was the concept of function overriding. We hope it was clear to you. If yes then do share it with your friends and programming buddies. If you have any doubt regarding any concept or this post do tell us in the comment section. For daily updates and more such content do follow us on Instagram.

Keep programming!

ErrorFreeProgram.

1 COMMENT

Leave a Reply

Your email address will not be published. Required fields are marked *