Answer to Question #186399 in C++ for Jingly

Question #186399

Develop a C++ program to Subtract two complex number by overloading - operator using Friend function.


1
Expert's answer
2021-04-27T14:11:19-0400
#include <iostream>
using namespace std;
class Complex{
    public:
    float a, b;
    Complex(): a(0), b(0) {}
    Complex(float x, float y): a(x), b(y){}
    void display(){
        cout<<this->a<<" + "<<this->b<<"i"<<endl;
    }
    friend Complex operator-(const Complex&, const Complex&);
};
Complex operator-(const Complex& com, const Complex& comp){
        float x = com.a - comp.a;
        float y = com.b - comp.b;
        return Complex(x, y);
    }
int main(){
    Complex a(3, 5), b(4, 7);
    cout<<"A = "; a.display();
    cout<<"B = "; b.display();
    cout<<"A - B = "; (a - b).display();
    cout<<"B - A = "; (b - a).display();
    return 0;
}

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