Skip to content

Instantly share code, notes, and snippets.

@ravikiran0606
Last active July 30, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravikiran0606/be152b32d51fedab98d451fac1e00b29 to your computer and use it in GitHub Desktop.
Save ravikiran0606/be152b32d51fedab98d451fac1e00b29 to your computer and use it in GitHub Desktop.
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