1 of 20

Operator Overloading

2 of 20

�C++ Overloading �(Function and Operator)

  • C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

3 of 20

Types of overloading in C++ �

4 of 20

C++ Operators Overloading�

  • Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type.
  • Operator overloading is used to overload or redefines most of the operators available in C++.
  • It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.

5 of 20

Operator that cannot be overloaded 

  • Scope resolution operator (::)
  • member selector(.)
  • member pointer selector(.*)
  • Conditional operator(?:)

6 of 20

Syntax of Operator Overloading�

  • return_type class_name  : : operator op(argument_list)  
  • {  
  •      // body of the function.  
  • }  

  • Where the return type is the type of value returned by the function.
  • class_name is the name of the class.
  • operator op is an operator function where op is the operator being overloaded, and the operator is the keyword.

7 of 20

Rules for Operator Overloading

  • When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
  • When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.

8 of 20

Why Operator Overloading??

  • When you use an expression like

‘2 +3’,

  • you know that the answer will be the sum of two integers.
  • This is because the compiler knows how to interpret the + operator when used with integers.
  • But, what if you want to do something like

‘obj1 = obj2 + obj3’

(where all these are objects of same class)

How + operator should work in this case?

  • The answer is through Operator Overloading.

9 of 20

Consider the following class:

  • class example
  • {
  • public:    
  • int a;    
  • int b;
  • };

10 of 20

  • When I say that I want to add objects, I want corresponding integer members to added. For example, something like this :

  • obj3.a = obj1.a + obj2.a;
  • obj3.b = obj1.b + obj2.b

11 of 20

  • What I want to do is to create three objects of this class and assign the sum of two of these objects to the third one i.e. something like this :
  • example obj1, obj2, obj3;    
  • obj1.a = 1;    
  • obj1.b = 1;    
  • obj2.a = 2;    
  • obj2.b = 2;    
  •     obj3 = obj1 + obj2;

12 of 20

  • Overloading decrement using friend function.

13 of 20

  • class A
  • {
  • int a, b;
  • public:
  • void accept()
  • {
  • cout<<"enter a and b";
  • cin>>a;
  • cin>>b;
  • }
  • friend void operator--(A &x);
  • void display()
  • {
  • cout<<"\n A : "<<a;
  • cout<<"\n B : "<<b;
  • }
  • };
  •  
  • void operator--(A &x)
  • {
  • x.a--;
  • x.b--;
  • }
  • int main()
  • {
  • A id;
  • id.accept();
  • --id;
  • id.display();
  • return 0;
  • }

14 of 20

  • class Distance
  • {
  • private:
  • int feet;
  • int inches;
  • public:
  • Distance()
  • {
  • feet = 0;
  • inches = 0;
  • }
  • Distance(int f, int i)
  • {
  • feet = f;
  • inches = i;
  • }
  • void displayDistance()
  • { cout << "F: " << feet << " I:" << inches <<endl;
  • }
  • Distance operator- ()
  • {
  • feet = -feet;
  • inches = -inches;
  • return Distance(feet, inches);

  • }
  • };
  • int main()
  • {
  • Distance D1(11, 10), D2(-5, 11);
  • -D1;
  • D1.displayDistance();
  • -D2;
  • D2.displayDistance();
  • return 0;
  • }

15 of 20

  • class A  
  • {  
  •     
  •     int x;  
  •       public:  
  •       A(){}  
  •     A(int i)  
  •     {  
  •        x=i;  
  •     }  
  •     void operator+(A);  
  •     void display();  
  • };  
  •   
  • void A :: operator+(A a)  
  • {  
  •      
  •     int m = x+a.x;  
  •     cout<<"The result of the addition of two objects is : "<<m;  
  •   
  • }  
  • int main()  
  • {  
  •     A a1(5);  
  •     A a2(4);  
  •     a1+a2;  
  •     return 0;  
  • }  

16 of 20

�Overloading stream insertion (<>) operators in C++

  • In C++, stream insertion operator “<<” is used for output and extraction operator “>>” is used for input. �We must know the following things before we start overloading these operators. 

1) cout is an object of ostream class and cin is an object of istream class .

2) These operators must be overloaded as a global function. And if we want to allow them to access private data members of the class, we must make them friend. 

17 of 20

Why these operators must be overloaded as global? 

  • In operator overloading, if an operator is overloaded as a member, then it must be a member of the object on the left side of the operator.
  • For example, consider the statement “ob1 + ob2” (let ob1 and ob2 be objects of two different classes).
  • To make this statement compile, we must overload ‘+’ in a class of ‘ob1’ or make ‘+’ a global function. 
  • The operators ‘<<‘ and ‘>>’ are called like ‘cout << ob1’ and ‘cin >> ob1’. So if we want to make them a member method, then they must be made members of ostream and istream classes, which is not a good option most of the time.

18 of 20

  • class Distance
  • {
  • private:
  • int feet;
  • int inches;
  • public:
  • Distance()
  • {
  • feet = 0; inches = 0;
  • }
  • Distance(int f, int i)
  • {
  • feet = f; inches = i;
  • }

  • friend ostream &operator<<( ostream &output, Distance &D )
  • {
  • output << "F : " << D.feet << " I : " << D.inches;
  • return output;
  • }

  • friend istream &operator>>( istream &input, Distance &D )
  • {
  • input >> D.feet >> D.inches; return input;
  • }

  • };

  • int main()
  • {
  • Distance D1(11, 10), D2(5, 11), D3;
  • cin >> D3;
  • Cout << D2 ;
  • Cout << D3 ;
  • return 0;
  • }

19 of 20

  • Using the concept of operator overloading, WAP to evaluate the eqn a=b*3 where a & b are the objects of same class.
  • Use friend function.

20 of 20

  • class A
  • {
  • public:
  • int x;
  • void setvalue(int j)
  • {
  • x=j;
  • }
  • friend A operator *(A a,int x1);
  • void display()
  • {
  • cout<<x;
  • }
  • };
  •  
  •  

  • A operator *(A b,int k)
  • {
  • A c;
  • c.x=b.x*k;
  • return c;
  • }

  • int main()
  • {
  • A y;
  • y.setvalue(10);
  • A ob;
  • ob=y*3;
  • ob.display();
  •  
  • return 0;
  • }