C++ : add complex number using class


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

using namespace std;

class complex
{
private:
int real;
int imaginary;
public:
void setdata(int r,int i)
{
real=r;
imaginary=i;
}
void display()
{
cout<<real<<"+i"<<imaginary;
}
void sum(complex c1,complex c2);
};

void complex::sum(complex c1,complex c2)
{
real=0;
imaginary=0;

real=c1.real+c2.real;
imaginary=c1.imaginary+c2.imaginary;
}

int main()
{
complex c1,c2,c3;
c1.setdata(2,3);
c2.setdata(1,2);
c3.sum(c1,c2);
cout<<"\nFirst complex number =  ";
c1.display();
cout<<"\n\nSecond complex number =  ";
c2.display();
cout<<"\n\n\nSum =  ";
c3.display();
getch();
return(0);
}

No comments:

Post a Comment