Types of operators in C++.

What is Operator Overloading?

Typically in C language, the above operators work with only built-in datatypes. C++ gives us the facility to make this operator works on User Define Data types that is an object of the user-defined class. This mechanism is known as Operator Overloading.

Consider below program.

Example of Operator Overloading


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

class Vector
{
	public:
	int a;
	int b;
	void display ()
	{
		cout<< "a: "<<a<<endl;
		cout<< "b: "<<b<<endl;
	}
	void operator -()       //Operator function overloading - operator
	{
		a=-a;
		b=-b;
	}
};

void main()
{
	clrscr();
	Vector obj;
	obj.a=10;
	obj.b=3;
	obj.display();
	-obj;
	cout<<"Afer applying - operator "<<endl;
	obj.display();
	getch();
}

Output of the above program :

Operator Overloading Basics in C++

Leave a Reply

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