The goal of this assignment is to implement an overrided operator that can add a
ID: 3810478 • Letter: T
Question
The goal of this assignment is to implement an overrided operator that can add and multiply two complex numbers. You will also implement a Friend class. Build and implement a class named Complex with the following interface: class complex {private: double real; double img; public: complex(double _real, double _img); double get_real(); double get_img(); double calc_phase(); friend void print(complex c); complex operator+ (const complex& num); complex operator* (const complex& num);}; Exercise: 1. Create the implementation for this above interface design. 2. Test your code by running the following exercises in your main function. Output your results to standard output. Implement your print function to print the complex number in the x+yi format as shown below in my output 1. create complex point c1 with 4-5i 2. create complex point c2 with 2+9i 3. determine phase of c_1 and c_2 4. add points c_1 and c_2 5. multiply ponts c_1 and c_2Explanation / Answer
#include<iostream>
using namespace std;
//Class complex definition
class complex
{
private:
//Data member
double real;
double img;
public:
//Member function
complex(double, double);
double get_real();
double get_img();
double calc_phase();
friend void print(complex c);
complex operator + (const complex &num);
complex operator * (const complex &num);
};//End of class
//Parameterized constructor
complex::complex(double double_real, double double_img)
{
real = double_real;
img = double_img;
}//End of constructor
//Returns real number
double complex::get_real()
{
return real;
}//End of function
//Returns imaginary
double complex::get_img()
{
return img;
}//End of function
//Calculates
double complex::calc_phase()
{
//Accepts data
cout<<" Enter First object real part: ";
cin>>real;
cout<<" Enter First object imaginary part: ";
cin>>img;
//Creates First complex object with value
complex c1(real, img);
//Accepts data
cout<<" Enter Second object real part: ";
cin>>real;
cout<<" Enter Second object imaginary part: ";
cin>>img;
//Creates Second complex object with value
complex c2(real, img);
//Temporary object
complex t(0,0);
//Addition
t = c1 + c2;
//Display data
cout<<" Creates complex point c1 with ";
print(c1);
cout<<" Creates complex point c2 with ";
print(c2);
//Display result
cout<<" Addition of c1 and c2 = ";
print(t);
//Multiplication
t = c1 * c2;
//Display data
cout<<" Creates complex point c1 with ";
print(c1);
cout<<" Creates complex point c2 with ";
print(c2);
//Display result
cout<<" Multiplication of c1 and c2 = ";
print(t);
}//End of function
//Displays data
void print(complex c)
{
//Checks for negative
if(c.img < 0)
cout<<c.real<<c.img<<"!";
//Otherwise position
else
cout<<c.real<<"+"<<c.img<<"!";
}//End of function
//Overloading + operator
complex complex::operator + (const complex &num)
{
//Creates temporary object
complex t(0,0);
//Addition operation
t.real = real + num.real;
t.img = img + num.img;
//Returns result
return t;
}//End of function
//Overloads * operator
complex complex::operator * (const complex &num)
{
//Creates temporary object
complex t(0,0);
//Multiplication operation
t.real = real * num.real;
t.img = img * num.img;
//Returns result
return t;
}//End of function
//Main function
int main()
{
//Creates an object
complex cc(0,0);
//Calls calculation phase function
cc.calc_phase();
}//End of main
Output 1:
Enter First object real part: 2
Enter First object imaginary part: -3
Enter Second object real part: 4
Enter Second object imaginary part: 3
Creates complex point c1 with 2-3!
Creates complex point c2 with 4+3!
Addition of c1 and c2 = 6+0!
Creates complex point c1 with 2-3!
Creates complex point c2 with 4+3!
Multiplication of c1 and c2 = 8-9!
Output 2:
Enter First object real part: 4
Enter First object imaginary part: -5
Enter Second object real part: 2
Enter Second object imaginary part: 9
Creates complex point c1 with 4-5!
Creates complex point c2 with 2+9!
Addition of c1 and c2 = 6+4!
Creates complex point c1 with 4-5!
Creates complex point c2 with 2+9!
Multiplication of c1 and c2 = 8-45!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.