Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ program to implement a complex number class and its basic functions :
#include<iostream>
using namespace std;
class cmplx{
int real,img;
public:
cmplx(int x,int y);
void add(cmplx a,cmplx b);
void display();
};
cmplx::cmplx(int x=0,int y=0){
real=x;
img=y;
}
void cmplx::add(cmplx a,cmplx b){
real=a.real+b.real;
img=a.img+b.img;
}
void cmplx::display(){
cout<<"The addition of two complex numbers is "<<real<<" + "<<img<<" i\n";
}
int main()
{
int x,y;
cout<<"Enter the real and imaginary part of the first complex number..";
cin>>x>>y;
cmplx a(x,y);
cout<<"Enter the real and imaginary part of the second complex number..";
cin>>x>>y;
cmplx b(x,y);
cmplx c;
c.add(a,b);
c.display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.