0% found this document useful (0 votes)
2 views2 pages

3 - Complex Class Using Operator Overloading

The document presents a C++ program that implements a complex number class with operator overloading for addition, subtraction, multiplication, and equality comparison. It includes methods for reading and displaying complex numbers, as well as an overloaded operator for adding a double to a complex number. The main function demonstrates the usage of these features by creating complex number instances and performing various operations.

Uploaded by

sasikumarmc67
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

3 - Complex Class Using Operator Overloading

The document presents a C++ program that implements a complex number class with operator overloading for addition, subtraction, multiplication, and equality comparison. It includes methods for reading and displaying complex numbers, as well as an overloaded operator for adding a double to a complex number. The main function demonstrates the usage of these features by creating complex number instances and performing various operations.

Uploaded by

sasikumarmc67
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

19CSE201- Advanced Programming

Object Oriented Programming- Operator Overloading

#include <iostream>
#include <string>
using namespace std;

class complex{
private:
int real,imag;

public:
complex():real(0),imag(0){};
complex(int a, int b):real(a),imag(b){};

void read(){
cout<<"Enter the real part: ";
cin >> real;
cout<<"Enter the imaginary part: ";
cin >> imag;
}

void display(){
cout<<real<<"+"<<imag<<"i\n";
}

complex operator + (complex b){


complex c;
[Link]=real+[Link];
[Link]=imag+[Link];
return c;
}

complex operator - (complex b){


complex c;
[Link]=[Link];
[Link]=[Link];
return c;
}

complex operator * (complex b){


complex c;
[Link]=real*[Link]-imag*[Link];
[Link]=real*[Link]-imag*[Link];
return c;
}

bool operator == (complex b){


return(real==[Link] && imag*[Link]);
}

void operator + (double b){


real=real+b;
}
};

int main(){
complex p1,p2(-4,3),p3;
[Link]();
cout<<"p1 = ";
[Link]();
cout<<"p2 = ";
[Link]();

p3=p1+p2;
cout<<"p1+p2 = ";
[Link]();
cout<<"p1-p2 = ";
p3=p1-p2;
[Link]();
cout<<"p1*p2 = ";
p3=p1*p2;
[Link]();

if(p1==p2) cout<<"\np1 is equal to p2\n";


else cout<<"\np1 is not equal to p2\n";
cout<<"p1+6 = ";
p1+6;
[Link]();
}

You might also like