Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include <iostream>

using namespace std;


class Complex {
public:

// Constructor
Complex(double real = 0, double imaginary = 0) : real(real),
imaginary(imaginary) {}

// Copy Constructor
Complex(Complex &other){
real = other.real;
imaginary = other.imaginary;
}

// * 1. Overloading +
Complex operator+(Complex arg){
return Complex(real + arg.real, imaginary + arg.imaginary);
}

// * 2. Overloading less than <


bool operator<(Complex arg){
if(real < arg.real)
return true;
else
return false;
}

// * 3. Overloading Insertion <<


friend ostream& operator<<(ostream &ost,Complex &arg){
ost << arg.real << " + " << arg.imaginary << "i" << std::endl;
return ost;
}

private:
double real;
double imaginary;
};
int main(){
Complex C1(2,3);
Complex C2(12,14);

Complex *ptr = new Complex[4];

Complex C3 = C1 + C2; // Uses overloaded + operator

cout<<"C3: "<<C3; // Overloaded Extraction operator

Complex C4(C3); // By using Copy Constructor

cout<<endl<<"C4: "<<C4;

cout<<endl<<ptr[0];

You might also like