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.  

Check out our free courses to get an edge over the competition.

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.

Check out Java Bootcamp from upGrad.

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. 

Check out upGrad’s Full Stack Development Bootcamp

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

 

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

In-Demand Software Development Skills

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. 

Learn Software Engineering Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Explore our Popular Software Engineering Courses

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.

Explore Our Software Development Free Courses

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. 

Read our Popular Articles related to Software Development

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s Executive PG Program 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.

What is function overriding?

Function overriding is a concept in object-oriented programming which allows a function within a derived class to override a function in its base class, but with a different signature (and usually with a different implementation). A key aspect of this is that it is not possible for the derived class to “undo” the changes to the base class, or at least it is not possible without editing the base class further. A common use of function overriding is to provide a default implementation in the base class, and then overriding with a specific implementation in the derived class.

What are the differences between function overriding and overloading?

Function overriding occurs when you create function with the same name as a function that already exists in a base class. When this happens, the new function will replace the existing function and can be used in place of the original function. Overloading occurs when you create functions with the same name but different parameters. When this happens, the new function will be called in addition to the original function, and both functions can be used in any context without any problem.

What is inheritance in C++?

Want to share this article?

Prepare for a Career of the Future

Leave a comment

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

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

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

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks