Create and Implement a C++ program to overload >, <, == operators for the class Complex
numbers. Take two objects of class and compare both objects. Write a driver program to test
your class.
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imaginary;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imaginary = i;
}
friend ostream &operator<<(ostream &output, const Complex &c);
friend istream &operator>>(istream &in, Complex &c);
};
ostream &operator<<(ostream &output, const Complex &c)
{
output << c.real;
output << "+i" << c.imaginary << endl;
return output;
}
istream &operator>>(istream &in, Complex &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter imaginary Part ";
in >> c.imaginary;
return in;
}
Comments
Leave a comment