Wednesday, 13 July 2011

Program OverLoading

#include <iostream.h>
#include <stdlib.h>

class complex
{
    double real;
    double imaginer;
public:
    complex(double re=0, double im=0)
    {
        real = re;
        imaginer = im;
    }
    complex operator-()
    {
        complex result;
        result.real = -real;

        result.imaginer = -imaginer;
        return result;
    }
    complex operator+(complex c)
    {
        complex result;
        result.real = real + c.real;
        result.imaginer = imaginer + c.imaginer;
        return result;
    }
    complex operator-(complex c)
    {
        complex result;
        result.real = real - c.real;
        result.imaginer = imaginer-c.imaginer;
        return result;
    }
        complex operator*(complex c)
    {
        complex result;
        result.real = real * c.real;
        result.imaginer = imaginer*c.imaginer;
        return result;
    }
    void display()
    {
        cout <<'(' << real << ',' << imaginer << ')' << endl;
    }
};

int main()
{
    complex z (1,2), w(3,4), b;
    complex a = z + w;
    a.display();
    a = z - w;
    a.display();
    b=-a;
    b.display();
    a = z * w;
    a.display();
    system ("pause");
    return 0;
}

No comments: