Skip to main content

C++ Overloading

There are two type of overloading is defined by c++.

Function Overloading

function overloading are exist in c++. This are not allowed in c programming. overloading of function that means function name are same but it will perform different different task. important point when we overload a function there parameter( arguments) are different. See the example.

/*Example of function Overloading*/
void employee(){/*code*/}
int employee(int id){/*code*/}
float employee(int id,float amount){/*code*/}
double employee(int key, int status){/*code*/}

In this scenario employee 4 function are overloaded. there are name same. but note that here parameters are not same. similar way we can declare multiple overload function in our program. see the example.

/*
  Example of function overloading in c++
*/
#include<iostream>
using namespace std;
/*
  function : sum
  parameter : accept two integer value
  return : integer value
*/
int sum(int num1, int num2){
  cout<<"\n First sum function call :";
  return (num1+num2);
}
/*
  function  : sum
  parameter : 3 arguments (float data)
  return    : sum of 3 float value
*/
float sum(float num1,float num2,float num3){
  cout<<"\n Second sum function call :";
  return (num1+num2+num3);
}
/*
  function  : sum
  parameter : 2 arguments (float ,int)
  return    : none
*/
void sum(float num1,int num2){
  cout<<"\n Third sum function call :";
  float result=num1+num2;
  cout<<result ;
}
int main(){
  /*Execute function*/
  cout<<sum(10,20);//first sum function execute
  sum(15.5f,80); // third sum function execute
  cout<<sum(10.0f,20.50f,30.50f); //Second sum function execute
}
Output

 First sum function call :30
 Third sum function call :95.5
 Second sum function call :61

Note that compiler are understand the behaviour of function using of function argument and execute those function.

Function overloading are based on parameter of function. Not based on return type. look at this example.


/*Not function overloading*/

void data(int type){ 
/*code*/
}
int data(int id){
/*code*/
return 0;}

In this two function same name and same arguments. but different return type. So this is not a part of function overloading. When compile this program are produced following error.

/*
Return type not a part of function overloading
*/
#include<iostream>
void data(int type){ 
/*code*/
}
int data(int id){
/*code*/
return 0;}
int main(){
}
Output
error: ambiguating new declaration of 'int data(int)'
 int data(int id){
     ^~~~
note: old declaration 'void data(int)'
 void data(int type){ /**/ }
      ^~~~

Operator Overloading

Operator overload are features of c++ programming. help of operator overloading we can solve complex task in very easy manner. or we can say that given a new and own special meaning to existing operator. Operator overloading is provide a flexibility. this operator are never lost its originality in our source code. In this post we are learn about all the basic of operator overloading.

Definition of operator overloading

There are similar as normal member function but here small changes. name of function are made using operator keyword and after that used specific symbol which are overloaded by programmer (like +,- etc).

return_type class_name::operator op(parameter_list){
  //code
  //body of function
}
/*
return_type: return value
class_name : name of class
operator   : keyword
op         : operator (like +,-,* etc)
operator op : same as name of function
*/

And function declaration of inside a class like this.

return_type operator op(parameter_list);

Before given example there are given a list which are can not overloaded in c++.

Operator Name
:: Scope Resolution
. (dot symbol) Membershipm Operator
.* pointer to member operator
:? Conditional operator

Overload Binary Operators like +,-,*,/ (division)

We now about that binary operator. there are two operands. look at this example.

int sum=89+90;
//89,90 : is operands
//+ : binary operator

Similarly assume that we are need two perform mathmetical operation of given objects using operators. like this.


//e1 e2 and e3 objects of class Example  
Example e1,e2,e3;
e3=e1+e2; //addition of two object data member
e3=e1/e2; //division of two object data member
e1=e2*e3; //so on

//same as previous instruction
e3=e1.operator+(e2);
e3=e1.operator/(e2);
e1=e2.operator*(e3);

so c++ are providing the facilities. we make a logic like implementation detail and overload operators. look at view example.

/*
  Binary operator overloading in c++
*/
#include<iostream>
using namespace std;

class Calculator{
  int bit1,bit2;
  public:
    /*-------
    Member Function
    Function prototype 
    ----------*/
    Calculator();
    Calculator(int,int);
    Calculator operator +(Calculator);
    Calculator operator -(Calculator);
    Calculator operator *(Calculator);
    float operator /(Calculator);
    void result();

};
//Parameterless constructor
Calculator :: Calculator(){ bit1=bit2=0;}
//Constructor with two integer
Calculator :: Calculator(int value1,int value2){
  //Assign Data member values
  bit1=value1;
  bit2=value2;
}
Calculator Calculator:: operator +(Calculator obj){
  //temporary pointer
  Calculator auxiliary;
  auxiliary.bit1=bit1+obj.bit1;
  auxiliary.bit2=bit2+obj.bit2;
  //Return As Object
  return auxiliary;
}
Calculator Calculator:: operator -(Calculator obj){
  //temporary pointer
  Calculator auxiliary;
  auxiliary.bit1=bit1-obj.bit1;
  auxiliary.bit2=bit2-obj.bit2;
  //Return As Object
  return auxiliary;
}
Calculator Calculator:: operator *(Calculator obj){
  //temporary pointer
  Calculator auxiliary;
  auxiliary.bit1=bit1*obj.bit1;
  auxiliary.bit2=bit2*obj.bit2;
  //Return As Object
  return auxiliary;
}
float Calculator:: operator /(Calculator obj){
    float temp=0.f; //assign default value
    if(obj.bit1!=0){
      temp=bit1/obj.bit1;
    }
  return temp;
}
void Calculator::result(){
  //display data member values
  cout<<"bit1 :"<<bit1<<endl;
  cout<<"bit2 :"<<bit2<<endl;

}
int main(){
 //create object
 Calculator c1(12,6);
 Calculator c2(7,3);

  //display result
 cout<<"Initial Data Member"<<endl;
 cout<<"object c1 "<<endl;
 c1.result();
 cout<<"object c2 "<<endl;
 c2.result();

 //Addition operation
 //same as  addition=c1.operator+(c2);
 Calculator addition=c1+c2;

 //Subtract operation
 //same as  subtract=c1.operator-(c2);
 Calculator subtract=c1+c2;

 //multiply operation
 //same as  multiply=c1.operator-(c2);
 Calculator multiply=c1*c2;

 //divide=c1.operator/(c2);
  float divide=c1/c2;
 
 //display output
 cout<<"addition object result"<<endl;
 addition.result();
 cout<<"multiply object result"<<endl;
 multiply.result();
 cout<<"subtract object result"<<endl;
 subtract.result();
 cout<<"divide result :"<<divide;
}
Outout
Initial Data Member
object c1
bit1 :12
bit2 :6
object c2
bit1 :7
bit2 :3
addition object result
bit1 :19
bit2 :9
multiply object result
bit1 :84
bit2 :18
subtract object result
bit1 :19
bit2 :9
divide result :1

In this class are overload 4 binary operators (+,-,*,/). Note that here defined 4 function they are overload by Calculator class. there are important points of this program is Function definition and body of function. look at example.

Calculator Calculator:: operator +(Calculator obj){
  //temporary pointer
  Calculator auxiliary;
  auxiliary.bit1=bit1+obj.bit1;
  auxiliary.bit2=bit2+obj.bit2;
  //Return As Object
  return auxiliary;
}
int main(){
 //create object
 Calculator c1(12,6);
 Calculator c2(7,3);
 Calculator addition=c1+c2;
}

Observe that this (Calculator addition=c1+c2;) statement are equivalent to this (Calculator addition=c1.operator +(c2);). and inside body of function. and both object c1 and c2 are present inside a function.

//most important
bit1+obj.bit1;  //bit1 are data member of c1 class
//and obj are copy of c2 data member


Unary minus operator overloading

unary operator are only used in single operands. how to overload unary operator in c++. see this example.

//Overloading unary minus operators
#include<iostream>
using namespace std;
class Transform{
  /*
    Private data member of class
    Exchange.
    declaring of integer variables
  */
  int a,b;
  public:
    //member function 
    //class constructor
    Transform(int,int);
    //overload unary minus 
    void operator-();
    //display value of data member
    void display();
};
//parameterized constructors 
Transform :: Transform (int a,int b){
  //assign value to data member
  this->a=a;
  this->b=b;
}
void Transform::operator-(){
  //change sign of data member
  a=-a;
  b=-b;
}

void Transform::display(){
  //display data member value
  cout<<" a :"<<a<<endl;
  cout<<" b :"<<b<<endl;
}

int main(){
  //create object of class
  Transform t1(5,7);
  Transform t2(-2,-1);

  //display initial value
  cout<<" Before value "<<endl;
  t1.display();
  t2.display();

  //overload operator
  -t1;
  -t2;

  cout<<" After value "<<endl;
  t1.display();
  t2.display();

  return 0; 
}
Output
 Before value
 a :5
 b :7
 a :-2
 b :-1
 After value
 a :-5
 b :-7
 a :2
 b :1

This is an simple example to change the sign of data member value.


Overloading Increment (++) operators

//Overloading Increment (++) operators
#include<iostream>
using namespace std;
class Increment{
  /*
    Private data member of class
    Exchange.
    declaring of integer variables
  */
  int a,b;
  public:
    //member function 
    //class constructor
    Increment(int,int);
    //overload unary minus 
    void operator++();
    //display value of data member
    void display();
};
//parameterized constructors 
Increment :: Increment (int a,int b){
  //assign value to data member
  this->a=a;
  this->b=b;
}
void Increment::operator++(){
  //value increment by one
  a=++a;
  b=++b;
}

void Increment::display(){
  //display data member value
  cout<<" a :"<<a<<endl;
  cout<<" b :"<<b<<endl;
}

int main(){
  //create object of class
  Increment t1(7,9);

  //display initial value
  cout<<" Before value "<<endl;
  t1.display();

  //overload operator
  ++t1;

  cout<<" After value "<<endl;
  t1.display();
  return 0; 
}
Output
 Before value
 a :7
 b :9
 After value
 a :8
 b :10




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment