How To Implement Function Overloading And Overriding In C++?

Last updated on Aug 08,2019 11.7K Views

How To Implement Function Overloading And Overriding In C++?

edureka.co

C++ is an Object Oriented Programming Language in nature and it boasts off various features. In this session we would be discussing how to implement Function Overloading And Function Overriding in C++.

Following Pointers will be covered in this article,

Moving on with this article on Function overloading and overriding in C++

Function Overloading

Functions having the same name but different parameters is allowed in C++ and is called Function Overloading. It is also called compile-time Polymorphism.

For example:


sum( int a, float b)
sum(int a, int b)
sum(int a, int b, int c)

Here, there are three functions with the same name but the only thing that differentiates them is that the parameters are different on each. So, depending on the parameters passed, a function is called.

If the return types of the functions are different then it is considered invalid.

Moving on with this article on Function overloading and overriding in C++

Sample Code For Function Over Loading

include <iostream>
using namespace std;
class Addition {
public:
int add(int n1,int n2) {
return n1+n2;
}
int add(int n1,int n2,int n3) {
return n1+n2;
}
};
int main(void) {
Addition a;
cout<<a.add(20, 15)<<endl;
cout<<a.add(81, 162,21);
return 0;
}

Output

Explanation

In the program above, we have two functions in the addition class. Both named add. One has 2 parameters and the other has 3 parameters.

In the main function, we create an object of class addition called a. We call the add functions with 2 and 3 parameters respectively and the functions add are called and they perform addition.

This is how the function overloading takes place.

Moving on with this article on Function overloading and overriding in C++

Function Overriding

When a derived class has a function with the same name as a function of the base class, it is called Function Overriding. Both functions must have the same parameters in both classes.

Sample Code For Function Overriding

#include <iostream>
using namespace std;
class BaseClass {
public:
void disp(){
cout<<"Parent Class Function";
}
};
class DerivedClass: public BaseClass{
public:
void disp() {
cout<<"Child Class Function";
}
};
int main() {
DerivedClass obj = DerivedClass();
obj.disp();
return 0;
}

Output:

Explanation:

In the program above, we show basic function, with the same name in derived and base class. Here the object is created of the derived class so when we call display only the child class object is displayed.

Moving on with this article on Function overloading and overriding in C++

Order to Perform Overriding

Consider the code:

#include <iostream>
using namespace std;
class BaseClass {
public:
void disp(){
cout<<"Function of Parent Class";
}
};
class DerivedClass: public BaseClass{
public:
void disp() {
cout<<"Function of Child Class";
}
};
int main() {
BaseClass obj = DerivedClass();
obj.disp();
return 0;
}

Output:

Explanation:

In the program above, we show basic function, with same name in derived and base class. Here, the only difference from the previous program is that. We create the object of the child class. The child class object is given the reference of base class. This can also be done by using another method,

Syntax

Parent_class_name::function()

In the above example, we use it as,

BaseClass::disp()

This is another way of overriding.

Function Overloading VS Function Overriding

Function OverloadFunction Override
The scope is the sameThe scope is different
Signatures must differ (ex: parameter)Signatures must be same
Number of overloading functions possibleOnly one overriding function possible
May occur without inheritanceIt mainly occurs due to inheritance

Thus we have come to an end of this article on ‘Function Overloading and Overriding in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

BROWSE COURSES