This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Saturday, February 11, 2023

write a program in C++ to overload '-' operator to find difference of two complex object.

write a program to overload '-' operator to find difference of two complex object


/* program in C++ to overload '-' operator to find difference of two complex object */

#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;
    foat y= com.b - comp.b;
    return Complex(x,y);
}
int main(){
    Complex a(1,7), b(6,9);
    cout<<"A = ";a.display();
    cout<<"B = ";b.display();
    cout<<"A - B = ";(a-b).display();
    cout<<"B - A = ";(b-a).display();
    return 0;
}

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget