The goal of this assignment is to implement an overrided operator that can add a
ID: 3813788 • 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 c1 and c2 4. add points c1 and c2 5. multiply points c1 and c2Explanation / Answer
#include <iostream>
#include <cmath>
#include <iomanip>
#include<stdlib.h>
using namespace std;
class Complex {
public:
Complex(): real(0), img(0){ }
Complex(double _real,double _img){
real=_real;
img=_img;}
double get_real(){return real;}
double get_img(){return img;}
double calc_phase(){
return atan(img/real);}
Complex operator +( Complex& num)
{
Complex c;
c.real=real+ num.real;
c.img=img+num.img;
return c;
}
Complex operator *(Complex& num)
{
Complex c;
c.real=(real* num.real)-(img* num.img);
c.img=(img*num.real)+(real*num.img);
return c;
}
friend void print( Complex _complex ){
cout<<"The formatted complex number is: ";
if(_complex.img<0){
cout<<_complex.real<<_complex.img<<"i"<<endl;
}else{
cout<<_complex.real<<"+"<<_complex.img<<"i"<<endl;
}
}
private:
double real;
double img;
};
// Main function for the program
int main( ) {
Complex c1(4,-5);
Complex c2(2,9);
Complex c3(0,0),c4(0,0);
// Add two object as follows:
c3 = c1 + c2;//overloadeed plus
c4=c1*c2;//overloaded * operator
//calling friend function
print(c1);
print(c2);
//calculating phase
cout<<"Phase of c1 and c2 is "<<c1.calc_phase()<<" and "<<c2.calc_phase()<<" respectively! "<<endl;
//using getmethods of complex class
cout<<"Addition of complex numbers: "<< c3.get_real()<<"+"<<c3.get_img()<<"i"<<endl;
cout<<"Multiplication of complex numbers: "<<c4.get_real()<<"+"<<c4.get_img()<<"i"<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.