January 13, 2021

C++ OOP – Function Overriding

Overriding is yet another importance feature in OOP. Although its application is not restricted to OOP, we’ll use OOP as a foundation.

  1. Overview of Function Overriding
  2. Accessing  Overridden Functions
  3. Using a Pointer to Access Overridden Functions

 

1. Overview of Function Overriding

A you already know, inheritance allows you to create a class by deriving an existing(base) class. The derived class then inherits members of the base class.

Assuming we have the same member function(with same name) defined in both the derived class and the base class. Now if we call the function from an object of the derived class, then the function in the derived class is executed. Not the parent.

This is what is called function overriding. In this case, the function in the derived class overrides the same function in the base class.

Let’s illustrate using an example:

// Program to demonstrate function Overriding
#include <iostream>

using namespace std;

// declare a class, shape
class  Shape {

  protected:
       double width = 9.2;

       void showWidth(){
    	   cout<<"Shape's width is: "<< width;
       }
};

// create a derived class Circle
class Circle: public Shape {

	public:
		// Overrides showWidth in Shape
		void showWidth(){
			cout<<"Circle's width is "<< width;
		}
};

int main() {

	Circle circle1;

	circle1.showWidth();

    return 0;
}

Output

Circle's width is 9.2

In the above code, the showWidth() function in the child overrides the showWidth() in the base class. Therefore, what executes is the child’s function.

Also note the the width used in the child comes from the base. It’s a protected data member and therefore available to derived class.

 

2. Accessing Overridden Functions

A function that has been overridden in a derived class can still be accessed. You can do this using the scope resolution operator(::).

The program below shows this:

<!– HTML generated using hilite.me —

//Accessing an Overridden Method
#include <iostream>

using namespace std;

// declare a class, shape
class  Shape {

  public:
       double width = 9.2;

       void showWidth(){
    	   cout<<"(base)Shape's width is: "<< width;
       }
};

// create a derived class Circle
class Circle: public Shape {

	public:
		// Overrides showWidth in Shape
		void showWidth(){
			cout<<"(derived)Circle's width is "<< width <<endl;
		}
};


int main() {

	Circle circle1;

	circle1.showWidth();

	circle1.Shape::showWidth();

    return 0;
}

The output is:

(derived)Circle's width is 9.2
(base)Shape's width is: 9.2

 

3. Calling an Overridden Function Using a Pointer.

In this case, we use a pointer of the base class to point to an object of the derived class. Then we call the function from that pointer.

I show only the main method here.

int main() {

Circle circle1;

//pointer of the base class pointing to circle1
Shape* shapePointer = &circle1;

//call the function of the base class using shapePointer
shapePointer->showWidth();

return 0;
}

Output

(base)Shape's width is: 9.2

So here, we first created an object of the derived class, circle1. Then we created a pointer, shapePointer. This pointer holds the address of the derived class. But we can then use it to call the showWidth() function, it calls the function from the base class.

 

Leave a Reply

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