Programs

Function Overriding in C++ [Function Overloading vs Overriding with Examples]

Function overriding in C++ is a feature that allows us to use a function in the child class that is already present in its parent class.  The child class inherits all the data members, and the member functions present in the parent class. If you wish to override any functionality in the child class, then you can implement function overriding. Function overriding means creating a newer version of the parent class function in the child class.  

C++ Functions

A C++ function is a group of statements that come together to perform a task. Every program has at least a main() function and certain programs can have additional functions. 

The function declaration conveys to the compiler the function name, parameters and return type. A function definition determines the function body. The C++ standard library consists of many built-in functions that the program can call. A function can be a method, a procedure, or a subroutine.

Defining a Function

The general syntax of a C++ function is:

return_type function_name(parameter list) {

function body

}

Any C++ function comprises a function header and a body. The components of a C++ function are:

Return Type –The return type represents the type of the function’s return value.

Function Name- This denotes the name of the function. The function name along with the parameter list make up the function signature.

Parameters – A parameter is a placeholder for the value returned by the function. When a function is called, a value is passed to the parameter which is called the actual parameter. 

Function Body- The function body constitutes the list of statements that define what the function will do.

Calling a Function

To use a function in C++, you must invoke or call the function. The control is transferred to the function, the function then performs the desired task, and the return statement returns the control back to the main program.

You need to pass the required parameters including the function name to call a function. If the function returns a value, then the value can be stored. There are a few ways in which arguments can be passed to a function while calling it. The call types are Call by Value, Call by Reference, and Call by Pointer. 

Function Overriding in C++

When a derived class or child class defines a function that is already defined in the base class or parent class, it is called function overriding in C++. Function overriding helps us achieve runtime polymorphism. It enables programmers to perform the specific implementation of a function already used in the base class.

Example: 

Source

Study the example given above. Here the parent class is “Base” and the child class is “Derived”.

The output of the above program will be:

Derived Function

The function print() is declared in both the Base and Derived classes. When we call the function print() through the Derived class object, “derived1”, the print() from the Derived class is invoked and executed by overriding the same function of the Base class.

Working of the Function Overriding Principle

Source

As you can see from the above image, the Base class function was overridden because we called the same function through the object of the Derived class.

If we call the print() function through an object of the Base class, the function will not be overridden. For e.g.:

//Call function of Base class

Base base1;

base1.print(); // Output: Base Function

The output of the above code will be:

Base Function

How to access Overridden Functions in C++

You must use the scope resolution operator, “::” to access the overridden function. Another way to access the overridden function is by using the pointer of the base class to point to an object of the derived class and calling the function through the pointer.

Example:

Source

The output of the above program will be:

Derived Function

Base Function

Working of the Access of overridden function

Source

Here the statement derived 1.print() accesses the print() function of the Derived class and the statement derived2.Base::print() accesses the print() function of the Base class. 

Calling a C++ overridden function from the derived class

In this code, we call the overridden function from within the Derived class itself.

Working of the overridden function call from the Derived class

Source

 The Base::print() command calls the overridden function from inside the Derived class. 

Function Overloading vs. Function Overriding

Function overloading is achieved at compile time and can be done in the base class and derived class. It helps to provide multiple definitions of the functions by changing the signature of the functions such as data type of parameters or return types. 

Function overriding is achieved at runtime. In overriding, the base class is redefined in the derived class with the same return type and parameters. Other differences between function overriding and function overloading in C++ are:

1. Inheritance

Function overriding can be used only with class inheritance while function overloading does not require class inheritance. 

2. Function Signature

Overloaded functions differ in signature either in the number of parameters or the type of parameters. In function overriding, the function signatures remain the same.

3. Function Scope

Overridden functions vary in scope while overloaded functions have the same scope.

4. Function Behavior

Function overriding is essential when a derived class function must perform differently or with added functionality than the base class function. Function overloading is implemented when functions with the same name need to have different behaviours depending upon the parameters passed to them. 

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Prepare for a Career of the Future

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT
LEARN MORE

0 replies on “Function Overriding in C++ [Function Overloading vs Overriding with Examples]”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×