Write a program to define a class Complex that enables operations on complex num
ID: 3568923 • Letter: W
Question
Write a program to define a class Complex that enables operations on complex numbers. Complex numbers are numbers of the form a + bi, where a is the real part of the complex number, b is the imaginary part and i has the value sqrt(-1).
1) Write a default constructor to initialize a and b to 0.
2) Write an overload constructor to initialize a and b with user?specified values.
3) Write function print to output the real part and imaginary part of the complex number in the form (a + bi) or (a, bi).
4) Overload the addition(+), subtraction(?), and multiplication(*) operators to enable addition,
subtraction, and multiplication of two complex numbers as in algebra.
5) Overload the == and!= operators to allow comparisons of two complex numbers.
6) Overload >> and << operators to enable input and output of complex numbers, respectively.
7) Write a driver program to test all the overloaded operators, print function and constructors.
The following is an example of overloading addition operator (+).
Function prototype in class definition in the header file:
class Complex{
public:
Complex operator+(const Complex &y);
private:
double a; //real part of complex number
double b; //imaginary part of complex number
};
Function implementation in class implementation file:
Complex Complex::operator+(const Complex &y)
{
return Complex(a + y.a, b + y.b);
}
Submit three files in the following names: Complex.h, Complex.cpp, and
main.cpp.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
class Complex{
public:
Complex operator+(const Complex &y);
Complex operator-(const Complex &y);
Complex operator*(const Complex &y);
int operator==(const Complex &y);
int operator!=(const Complex &y);
friend ostream &operator<<( ostream &output,
const Complex &y )
{
output << y.a << " + i" << y.b;
return output;
}
friend istream &operator>>( istream &input, Complex &y )
{
input >> y.a >> y.b;
return input;
}
private:
double a; //real part of complex number
double b; //imaginary part of complex number
public :
void setA(double x){this->a = x;}
void setB(double y){this->b = y;}
double getA(){return this->a;}
double getB(){return this->b;}
void display(){cout<<this->a<<" + i"<<this->b;}
Complex(){
this->a = 0;
this->b = 0;
}
Complex(double a, double b){
this->a = a;
this->b = b;
}
};
//Function implementation in class implementation file:
Complex Complex::operator+(const Complex &y)
{
return Complex(a + y.a, b + y.b);
}
Complex Complex::operator-(const Complex &y)
{
return Complex(a - y.a, b - y.b);
}
Complex Complex::operator*(const Complex &y)
{
return Complex(a*y.a-b*y.b, a*y.b+b*y.a);
}
int Complex::operator==(const Complex &y)
{
if((this->a==y.a)&&(this->b==y.b)){return 1;}
else{return 0;}
}
int Complex::operator!=(const Complex &y)
{
if((this->a==y.a)&&(this->b==y.b)){return 0;}
else{return 1;}
}
int main(){
Complex x, y;
cout<<"Please enter the real part and imaginary part : ";
cin>>x;
cout<<"Please enter the real and imaginary part : ";
cin>>y;
Complex z = x+y;
cout<<" SUM : ";
cout<<z;
z = x-y;
cout <<" DIFFERENCE : ";
cout<<z;
z = x*y;
cout<<" PRODUCT : ";
cout<<z;
if(x==y){cout<<" Entered two numbers are equal.";}
if(x!=y){cout<<" Entered two inputs are unequal.";}
getchar();
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.