Overriding Member Function In C++ - C++ Programming Tutorial
C++ Course / Functions / Overriding Member Function In C++

Overriding Member Function In C++

BLUF: Mastering Overriding Member Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Overriding Member Function In C++

C++ is renowned for its efficiency. Learn how Overriding Member Function In C++ enables low-level control and high-performance computing in the tutorial below.

In the realm of Object-oriented programming, Inheritance stands out as a fundamental and potent concept. It empowers a class to acquire traits and functionalities from another class. Overriding, a strategy employed in C++ programming, allows for the alteration of the behavior of an inherited member function within a derived class. This write-up delves into the intricacies of Overriding Member functions in C++ to provide a comprehensive understanding.

What is Overriding Member Function?

When a Derived class in C++ inherits a Member function from its Base class, it has the ability to redefine the functionality of that function within the Derived class. This action of redefining a Member Function from the Base class in a Derived class is known as "Overriding," and the modified function is commonly known as an "Overridden Member Function."

In simpler terms, if a Derived class creates a Member Function with identical name and parameters as a Member Function in its Base class, the Derived class's function will Replace the Base class function.

How to Override a Member Function in C++?

To overwrite a member function in C++, the following steps must be adhered to:

Step 1: Define a Base Class with a Virtual Function:

A Virtual Function is a specific kind of member function that is defined using the keyword "Virtual" within the base class. This signifies that the function has the capability to be redefined in the derived class. The format for specifying a Virtual Function is demonstrated below:

C++ code:

Example

class Base {

public:

    virtual void myFunction() {

        // Base class implementation

    }

};

Step 2: Define a Derived Class that Overrides the Virtual Function:

To replace a Virtual Function, it is necessary to create a subclass that extends the base class and offers a fresh implementation of the Virtual Function. The format for creating a subclass that overrides the Virtual Function is outlined below:

C++ Code:

Example

class Derived : public Base {

public:

    void myFunction() override {

        // Derived class implementation

    }

};

Step 3: Create Objects of the Derived Class and Call the Virtual Function:

We have the ability to instantiate instances of the subclass and invoke the Virtual Method by utilizing a Pointer to the superclass. The format for generating instances of the subclass and invoking the Virtual Method is outlined below:

C++ Code:

Example

Base* basePtr = new Derived();

basePtr->myFunction();

When a Virtual Function is invoked through a Pointer to the base class, it will execute the implementation defined in the derived class.

Example:

Let's consider an illustration showcasing the idea of Overriding Member Functions in C++. In the given scenario, there exists a base class or superclass referred to as, containing a virtual function. Additionally, there are two subclasses named Circle and Square, where they redefine the draw method to offer their specific implementation.

C++ Code:

Example

#include <iostream>

class Shape {

public:

    virtual void draw() {

        std::cout << "Drawing a shape" << std::endl;

    }

};

class Circle : public Shape {

public:

    void draw() override {

        std::cout << "Drawing a circle" << std::endl;

    }

};

class Square : public Shape {

public:

    void draw() override {

        std::cout << "Drawing a square" << std::endl;

    }

};

int main() {

    Shape* shapePtr = new Circle();

    shapePtr->draw();

    shapePtr = new Square();

    shapePtr->draw();

    return 0;

}

Output:

Output

Drawing a circle

Drawing a square

In this instance, we instantiate instances of the Circle and Square classes and invoke the render method through a reference to the parent class Shape. As the render method is polymorphic, the version in the child classes is executed, resulting in the display of "Drawing a circle" and "Drawing a square".

Advantages of Overriding Member Function

Polymorphism:

One key benefit of method overriding is its facilitation of Polymorphism. Polymorphism enables a subclass to exhibit various behaviors based on the specific situation in which it is employed.

Code Reusability:

Overriding enables the utilization of code from the parent class while granting the subclass the ability to alter the functionality of the parent class's methods.

Modularity:

Overriding makes it easier to maintain code because changes made in the derived class do not affect the base class.

Rules for Overriding Member Functions

  • The Overridden Function in the derived class must have the same name and arguments as the base class function.
  • The Overridden Function must have the same return type as the base class function or a covariant return type.
  • The access level of the Overridden Function in the derived class cannot be more restrictive than the access level of the base class function.
  • The Virtual keyword must be used in the base class function declaration.
  • The function in the base class must be declared with the same access level or higher than the derived class.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience