Last active
July 30, 2016 17:25
-
-
Save ravikiran0606/2a5d87e6cde2d9ae991188d470af69d2 to your computer and use it in GitHub Desktop.
C++ program to implement a Complex Class and make use of operator overloading:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cmath> | |
using namespace std; | |
class cmplx{ | |
float real,img; | |
public: | |
cmplx(); | |
cmplx operator +(cmplx a); | |
cmplx operator -(cmplx a); | |
void operator =(cmplx a); | |
cmplx* operator ->(); | |
float operator [](int index); | |
void operator ()(float a,float b); | |
void display(); | |
friend istream& operator >>(istream &inp,cmplx &a){ | |
inp>>a.real>>a.img; | |
return inp; | |
} | |
friend ostream& operator <<(ostream &out,cmplx &a){ | |
if(a.img>=0){ | |
out<<a.real<<" + "<<a.img<<" i"<<endl; | |
} | |
else{ | |
out<<a.real<<" - "<<abs(a.img)<<" i"<<endl; | |
} | |
} | |
}; | |
cmplx::cmplx(){ | |
real=0; | |
img=0; | |
} | |
cmplx cmplx:: operator +(cmplx a){ | |
cmplx t; | |
t.real=real+a.real; | |
t.img=img+a.img; | |
return t; | |
} | |
cmplx cmplx:: operator -(cmplx a){ | |
cmplx t; | |
t.real=real-a.real; | |
t.img=img-a.img; | |
return t; | |
} | |
float cmplx::operator[](int index){ | |
if(index==1){ | |
return real; | |
} | |
else if(index==2){ | |
return img; | |
} | |
} | |
void cmplx::operator ()(float a,float b){ | |
real=a; | |
img=b; | |
} | |
void cmplx::operator =(cmplx a){ | |
real=a.real; | |
img=a.img; | |
} | |
cmplx* cmplx::operator ->(){ | |
return this; | |
} | |
void cmplx::display(){ | |
if(img>=0){ | |
cout<<real<<" + "<<img<<" i"<<endl; | |
} | |
else{ | |
cout<<real<<" - "<<abs(img)<<" i"<<endl; | |
} | |
} | |
int main() | |
{ | |
cmplx a,b,c; | |
float x,y; | |
cout<<"Choice : 1) Addition 2) Subtraction 3) Initialization using function call operator 4) Exit"; | |
int ch; | |
while(1){ | |
cout<<"\nEnter your choice.."; | |
cin>>ch; | |
if(ch==1){ | |
cout<<"Enter the real and imaginary part of the first complex number.."; | |
cin>>a; | |
cout<<"Enter the real and imaginary part of the second complex number.."; | |
cin>>b; | |
c=a+b; | |
cout<<"The added complex number is "; | |
cout<<c; | |
} | |
else if(ch==2){ | |
cout<<"Enter the real and imaginary part of the first complex number.."; | |
cin>>a; | |
cout<<"Enter the real and imaginary part of the second complex number.."; | |
cin>>b; | |
c=a-b; | |
cout<<"The resultant complex number is "; | |
if(c[2]>=0){ | |
cout<<c[1]<<" + "<<c[2]<<" i"; | |
} | |
else{ | |
cout<<c[1]<<" - "<<abs(c[2])<<" i"; | |
} | |
cout<<endl; | |
} | |
else if(ch==3){ | |
cout<<"Enter the real and imaginary part of the complex number.."; | |
cin>>x>>y; | |
a(x,y); | |
b=a; | |
b->display(); | |
} | |
else{ | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment