Sunday, November 6, 2011

complex addition using + operator overloading

//Complex addition using Overloading + operator

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

using namespace std;

class complex
{
    float x;
    float y;
public:
    complex(){};
    complex(float real,float i){
    x=real;
    y=i;
    }
    complex operator+(complex);
    void display();
};

complex complex :: operator+(complex c)
{
    complex temp;
    temp.x=x+c.x;
    temp.y=y+c.y;
    return(temp);
}

void complex :: display()
{

    cout << x << "+" <<y<<"i"<<endl;
}

main(){
    complex c1(2.5,3.5),c2(4.7,3.4),c3;
    c3=c1+c2;
   
    cout <<"C1=";c1.display();
    cout <<"c2=";c2.display();
    cout <<"C3=";c3.display();
   
    getch();
    return 0;
}
Output of above program

No comments:

Post a Comment