#include<iostream.h>
#include<conio.h>
class addition
{
private:
                int a,b;
public:
                addition()
                                {
                                }
                addition(int p,int q)
                                {
                                a=p;
                                b=q;
                                }
                void display()
                                {
                                cout<<"A = "<<a<<"\tB = "<<b<<endl;
                                }
                friend addition operator + (addition obj1,addition obj2);


;
//Operator function as friend function which is called by compiler internally and both objects "X & Y" are passed as actual argument
addition operator + (addition obj1,addition obj2)
{
                addition t;
                t.a=obj1.a+obj2.a;
                t.b=obj1.b+obj2.b;
                return t;
}
void main()
{
                clrscr();
                addition x(5,3),y(8,6),z;
                z=x+y;   //Operator overloading
                cout<<"After Operator Overloading \n";
                z.display();
                getch();
}

176.PNG

Advertisements