in , ,

C++ Function Overriding

C++ Function Overriding
C++ Function Overriding

C++ Function Overriding

In this tutorial, we will find out about function overriding in C++ with the help of examples.

As we probably are aware, Inheritance is a feature of OOP that allows us to make got classes from a base class. The determined classes acquire highlights of the base class.

Assume, a similar capacity is characterized in both the inferred class and the based class. Presently if we call this capacity using the object of the inferred class, the capacity of the determined class is executed.

This is known as function overriding in C++. The function in the derived class overrides the function in the base class.


Example 1: C++ Function Overriding

// 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 derived1;
    derived1.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 derived1, the print() from Derived is executed by overriding the function in Base.


Access Overridden Function in C++

To get to the overridden capacity of the base class, we use the scope resolution operator

::.

We can likewise get to the overridden work by utilizing a pointer of the base class to highlight an object of the inferred class and afterward calling the capacity from that pointer.

Example 2: C++ Access Overridden Function to the Base Class

// 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

Derived Function
Base Function

Here, this statement

derived2.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 derived1;
    derived1.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

// 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 Base {
   public:
    void print() {
        cout << "Base Function" << endl;
    }
};

class Derived : public Base {
   public:
    void print() {
        cout << "Derived Function" << endl;
    }
};

int main() {
    Derived derived1;

    // pointer of Base type that points to derived1
    Base* ptr = &derived1;

    // call function of Base class using ptr
    ptr->print();

    return 0;
}

Output

Base Function

In this program, we have made a pointer of Base type named ptr. This pointer focuses on the Derived object derived1.

// pointer of Base type that points to derived1
Base* ptr = &derived1;

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 on the grounds that despite the fact that ptr focuses on a Derived object, it is really of the Base sort. In this way, it calls the part capacity of Base.

In order to override the Base function instead of accessing it, we need to use virtual functions in the Base class.


Thanks for watching! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

How To Make an Infographic in CorelDRAW

How To Make an Infographic in CorelDRAW

How to Make an eCard in CorelDRAW

How to Make an eCard in CorelDRAW