123 Eng

Engineering the engineers™


Latest Jobs   Forum Map

 


Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

   Computer Science / IT
   Electronics
   Electrical
   Mechanical
   Chemical
   Civil

   CAT / MBA

   GMAT / Foreign MBA
Latest Jobs

Engineering Jobs / Technical Jobs
Management Jobs

Sitemap
Terms of use

Displaying  Source Code(s)  
 

 
To perform operations on complex number using operator

--------------------------------------------------------------------------------

Description : In this programe i use the main feature of c++ i.e. operator overloading. By using operator overloading we can overload i.e. use one operator for more than one purpose.


:#include<iostream.h>
#include<math.h>
#include<process.h>
#include<conio.h>
class comp
{
private:
float real,image;
public:
comp operator +(comp a);
comp operator -(comp a);
comp operator *(comp a);
comp operator /(comp a);
void getdata();
void show();
};
void comp :: getdata()
{
cout<<"

Enter real part=";
cin>>real;
cout<<"

Enter imaginary part=";
cin>>image;
}
void comp :: show()
{
cout.precision(2);
if(image<0)
cout<<real<<image<<"i";
else
cout<<real<<"+"<<image<<"i";
}
comp comp :: operator +(comp a)
{
comp temp;
temp.real=real+a.real;
temp.image=image+a.image;
return temp;
}
comp comp :: operator -(comp a)
{
comp temp;
temp.real=a.real-real;
temp.image=a.image-image;
return temp;
}
comp comp :: operator *(comp a)
{
comp temp;
temp.real=(a.real*real)-(a.image*image);
temp.image=(a.real*image)+(real*a.image);
return temp;
}
comp comp :: operator /(comp a)
{
comp temp;
temp.real=((real*a.real)+(a.image*image))/((real*real)+(image*image));
temp.image=((real*a.image)-(image*a.image))/((real*real)+(image*image));
return temp;
}

void main()
{
comp d,e,f;
int ch;
char ans;
do
{
clrscr();
cout<<"

********** Menu **********<BR>;
cout<<"

1>Addition";
cout<<"

2>Subtraction";
cout<<"

3>Multiplication";
cout<<"

4>Division";
cout<<"

5>Exit";
d.getdata();
e.getdata();
cout<<"

first no=>";
d.show();
cout<<"

second no=>";
e.show();
cout<<"

enter the choice=>";
cin>>ch;
switch(ch)
{
case 1:
f=d-e;
cout<<"

addition of two no=>";
f.show();
break;
case 2:
f=d-e;
cout<<"

subtraction of two no=>";
f.show();
break;
case 3:
f=d*e;
cout<<"

multiplication of two no=>";
f.show();
break;
case 4:
f=d/e;
cout<<"

division of two no=>";
f.show();
break;
case 5:
exit(0);
break;
}
cout<<"

do you want to continue(y/n)?=";
cin>>ans;
}
while(ans=='y'||ans=='Y');
getch();
}

 

Contribute content or training reports / feedback / Comments
job placement papers
All rights reserved © copyright 123ENG