Operator Overloading in C++

In this tutorial we will study and understand the concept and application of Operator Overloading in C++

Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type. For example ‘+’ operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) or even a user defined class object.

Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.

Operator Overloading in C++ Syntax
ReturnType classname:: Operator OperatorSymbol(argument list)
{
   // function body
}
Implementing Operator Overloading

Operator overloading can be done by implementing a function which can be :

Operator overloading function can be a member function if the Left operand is an Object of that class, but if the Left operand is different, then Operator overloading function must be a non-member function. Operator overloading function can be made friend function if it needs access to the private and protected members of class.

Operator Overloading using Member Function Example Program

Here an object is passed as an argument whose properties will be accessed using this object, the object which will call this operator can be accessed using this operator as explained below

Run Online

#include <iostream>
using namespace std;

class Box {
   public:

      double getVolume(void) {
         return length * breadth * height;
      }
		
      void setLength( double len ) {
         length = len;
      }

      void setBreadth( double bre ) {
         breadth = bre;
      }

      void setHeight( double hei ) {
         height = hei;
      }
		
      // Overload + operator to add two Box objects.
      Box operator+(const Box& b) {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
		
   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};

// Main function for the program
int main( ) {
   Box Box1;                // Declare Box1 of type Box
   Box Box2;                // Declare Box2 of type Box
   Box Box3;                // Declare Box3 of type Box
   double volume = 0.0;     // Store the volume of a box here
 
   // box 1 specification
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // box 2 specification
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   // Add two object as follows:
   Box3 = Box1 + Box2;

   // volume of box 3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   return 0;
}
Output
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
Operator Overloading using Friend Function Example Program

In this program we are overloading the negation operator to work on our class object named xyz. It will perform negation of all data member values of our class just like it works on basic data types.

Run Online

#include<iostream>
using namespace std;
class xyz{
  int x;
	int y;
	int z;
	public:
		void get(){
			cout<<"XYZ please\n";
			cin>>x>>y>>z;
		}
		void disp(){
			cout<<x<<endl<<y<<endl<<z<<endl;
		}
                //friend function declaration
		void friend operator-(xyz &S);//pass by reference 
}s1;
// friend function definition
void operator-(xyz &S){
S.x=-S.x;//object name must be used as it is a friend function
S.y=-S.y;
S.z=-S.z;
}
int main(){
	s1.get();
	cout<<"BEFORE OVERLOADING\n";
s1.disp();
	cout<<"AFTER OVERLOADING \n";
	-s1;
s1.disp();
return 0;
}
//more codes www.technobuzzit.blogspot.com
Output
XYZ please
3 4 5
BEFORE OVERLOADING
3
4
5
AFTER OVERLOADING
-3
-4
-5
Operators That Cannot be overloaded
  • scope operator – ::
  • sizeof
  • member selector – .
  • member pointer selector – *
  • ternary operator – ?:
Watch it on YouTube

Leave a Reply

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