Answer to Question #224232 in C++ for Shank

Question #224232
Write a program to define a class Complex that will contain real and imaginary as the data members. Define appropriate constructors and a display functions. Overload the binary + and the * operator to add and multiply two complex numbers respectively.
1
Expert's answer
2021-08-09T02:34:51-0400
#include <iostream>
using namespace std;
class Complex{
    float real, imaginary;
    public:
        Complex(){}
        Complex(float a, float b): real(a), imaginary(b){}
        void Display(){
            cout<<real;
            if(imaginary != 0) cout<<" + "<<"i";
            if(imaginary < 0) cout<<"("<<imaginary<<")";
            else cout<<imaginary;
        }
        Complex operator+(const Complex &other){
            return Complex(this->real + other.real, this->imaginary + other.imaginary);
        }
        Complex operator*(const Complex &other){
            return Complex(this->real * other.real + this->imaginary * other.imaginary, this->real * other.imaginary + this->imaginary * other.real);
        }
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
09.08.21, 23:45

Dear Shashank Tirkey, there is no main()


Shashank Tirkey
09.08.21, 15:38

Undefined reference to 'winmain' Error is showing with Id returned 1 exit status

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS