Sunday, 15 September 2013

PROGRAM TO IMPLEMENT OPERATOR OVERLOADING

/*MADE BY KRISHNA AGARWAL*/
#include<iostream>
using namespace std;
class A
{
    int a;
public:
    A()
    {
        a=0;
    }
    A(int c)
    {
        a=c;
    }
    A operator-()
    {
        A temp;
        temp.a=-a;
        return temp;
    }
    A operator-(A a1)
    {
        A temp;
        temp.a=a-a1.a;
        return temp;
    }
    A operator+(A a1)
    {
        A temp;
        temp.a=a+a1.a;
        return temp;
    }
    A operator*(A a1)
    {
        A temp;
        temp.a=a*a1.a;
        return temp;
    }
    void display()
    {
        cout<<"\nthe value of the object is "<<a;
    }
};
int main()
{
    A a1,a2(2),a3(3),a4(4),a5(5),a6,a7(7);
    a1.display();
    a2.display();
    a3.display();
    a4.display();
    a5.display();
    a6.display();
    cout<<"\n\n";
    a1=a2+a3;
    a6=a4+a5;
    a1.display();
    a6.display();
    return 0;
}

No comments:

Post a Comment