C++ Operator Overloading

Advertisements

C++ Operator Overloading

An operator is a symbol that tells the compiler to perform specific task. Every operator have their own functionality to work with built-in data types. Class is user-defined data type and compiler doesn't understand, how to use operators with user-defined data types. To use operators with user-defined data types, they need to be overload according to a programmer's requirement.

Operator overloading is a way of providing new implementation of existing operators to work with user-defined data types.

An operator can be overloaded by defining a function to it. The function for operator is declared by using the operator keyword followed by the operator.

There are two types of operator overloading in C++

  • Binary Operator Overloading
  • Unary Operator Overloading

Overloading Binary Operator

Binary operator is an operator that takes two operand(variable). Binary operator overloading is similar to unary operator overloading except that a binary operator overloading requires an additional parameter.

Binary Operators

  • Arithmetic operators (+, -, *, /, %)
  • Arithmetic assignment operators (+=, -=, *=, /=, %=)
  • Relational operators (>, <, >=, <=, !=, ==)

Example of Binary Operator Overloading


	#include<iostream.h>
	#include<conio.h>

	class Rectangle
	{

		int L,B;

		public:

		Rectangle()           //Default Constructor
		{
			L = 0;
			B = 0;
		}

		Rectangle(int x,int y)           //Parameterize Constructor
		{
			L = x;
			B = y;
		}

		Rectangle operator+(Rectangle Rec)      //Binary operator overloading func.
		{
			Rectangle R;

				R.L = L + Rec.L;
				R.B = B + Rec.B;

			return R;
		}

		void Display()
		{
			cout<<"\n\tLength : "<<L;
			cout<<"\n\tBreadth : "<<B;
		}

	};

	void main()
	{

		Rectangle R1(2,5),R2(3,4),R3;
		//Creating Objects


		cout<<"\n\tRectangle 1 : ";
		R1.Display();

		cout<<"\n\n\tRectangle 2 : ";
		R2.Display();

		R3 = R1 + R2;       Statement 1

		cout<<"\n\n\tRectangle 3 : ";
		R3.Display();
	}


	Output :

	  Rectangle 1 :
	  L : 2
	  B : 5

	  Rectangle 2 :
	  L : 3
	  B : 4

	  Rectangle 3 :
	  L : 5
	  B : 9

In statement 1, Left object R1 will invoke operator+() function and right object R2 is passing as argument.

Another way of calling binary operator overloading function is to call like a normal member function as follows,


				R3 = R1.operator+ ( R2 );

Overloading Unary Operator

Unary operator is an operator that takes single operand(variable). Both increment(++) and decrement(--) operators are unary operators.

Example of Unary Operator Overloading


	#include<iostream.h>
	#include<conio.h>

	class Rectangle
	{

			int L,B;

			public:

			Rectangle()           //Default Constructor
			{
				L = 0;
				B = 0;
			}

			void operator++()      Unary operator overloading func.
			{
				L+=2;
				B+=2;
			}

			void Display()
			{
				cout<<"\n\tLength : "<<L;
				cout<<"\n\tBreadth : "<<B;
			}

	};


	void main()
	{

			Rectangle R;
			//Creating Object


			cout<<"\n\tLength Breadth before increment";
			R.Display();


			R++;

			cout<<"\n\n\tLength Breadth after increment";
			R.Display();
	}


	Output :

	  Length Breadth after increment
	  L : 0
	  B : 0

	  Length Breadth after increment
	  L : 2
	  B : 2

Advertisement