C++ class program to perform complex arithmetic using operator overloading

Write a program to perform complex arithmetic using operator overloading

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class complex
{
int real;
float image;
public:
void getdata()
{
cout<<"\n enter the real part of the complex";
cin>>real;
cout<<"\n enter the imaginary part of the complex";
cin>>image;
}
void operator + (complex);
void operator - (complex);
};
 
void complex :: operator +  (complex c1)
{
complex temp;
temp.real=real+c1.real;
temp.image=image+c1.image;
if (temp.image>=0);
{
cout<<"\n complex no. after addition:";
cout<<temp.real<<"+"<<temp.image<<"i";
}
else
{
cout<<"\n complex no. after addition ";
cout<<temp.real<<temp.image<<"i";
}
}
void complex ::operator-(complex c1)
{
complex temp;
temp.real = real-c1.image;
temp.image= image-c1.image;
if (temp.image>=0)
{
cout<<"\n complex no. after subtraction";
cout<<"\n temp.real<<"+"<<temp.image<<"i";
}
else
{
cout<<"\n complex no. after subtraction";
cout<<temp.real<<temp.image<<"i"
}
}
void main()
{
clrscr();
comp.ex c1, c2;
int n;
do 
{
cout<<"\n 1. Input data for complex no. ";
cout<<"\n 2. Addition of complex no. ";
cout<<"\n 3. Subtraction of complex no. ";
cout<<"\n 4. Quit";
cout<<"\n Enter your choice";
cin>>n;
switch(n)
 
{ 
case1:
cout<<endl<<"\n Enter the data for First Complex No......";
cl.getdata();
cout<<endl<<"\n Enter the data for seconds Complex No.....";
c2.getdata();
clrscr();
break;
 
case 2;
cl+c2;
getch();
clrscr();
break;
 
case 3:
cl-c2;
getch();
clrscr();
brak;
 
case 4:
exit91);
break;
}
} while (n!=4);
getch();
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

3 thoughts on “C++ class program to perform complex arithmetic using operator overloading

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in