Answer to Question #262746 in C++ for Vius

Question #262746

Write a program to design a class

representing complex numbers and having functionality of performing addition

and multiplication of two complex numbers using operator overloading.


1
Expert's answer
2021-11-09T17:45:50-0500
#include <iostream>
using namespace std;
class Complex{
    float real, imaginary;
    public:
        Complex(){}
        Complex(float r, float i): real(r), imaginary(i){}
        void print(){
            cout<<this->real;
            if(imaginary != 0) cout<<" + "<<"i";
            if(imaginary < 0) cout<<"("<<imaginary<<")";
            else cout<<imaginary;
        }
        //multiplication
        Complex operator*(const Complex &other){
            return Complex(this->real * other.real + this->imaginary * other.imaginary, this->real * other.imaginary + this->imaginary * other.real);
        }
        //addition
        Complex operator+(const Complex &other){
            return Complex(this->real + other.real, this->imaginary + other.imaginary);
        }
};

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