Answer to Question #225290 in C++ for Sharky

Question #225290
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-11T14:16:50-0400

Answer:-

#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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS