0% found this document useful (0 votes)
4 views

Operator Overloading.cpp

Uploaded by

Tej Tej
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Operator Overloading.cpp

Uploaded by

Tej Tej
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

//* Name-Gunjal Kunal

Div-SE-1

Roll Number-38

Assignment number - 6

Implement a class Complex which represents the Complex Number data type. Implement the
following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers

#include<iostream>

using namespace std;

class Complex

float i;

float j;

public:

Complex()

i=0;

j=0;

Complex operator +(Complex);

Complex operator *(Complex);

friend istream &operator>>(istream &input,Complex &k)


{

cout<<"Enter Real Part:";

input>>k.i;

cout<<"Enter Imaginary Part:";

input>>k.j;

friend ostream &operator<<(ostream &output,Complex &k)

output<<k.i<<"+"<<k.j<<"i\n";

};

Complex Complex::operator+(Complex c)

Complex temp1;

temp1.i=i+c.i;

temp1.j=j+c.j;

return(temp1);

Complex Complex::operator*(Complex c)

Complex temp2;

temp2.i=(i*c.i)-(j*c.j);

temp2.j=(j*c.i)+(i*c.j);

return(temp2);

}
int main()

Complex c1,c2,c3,c4;

cout<<"Default Constructor Value is:\n";

cout<<c1;

cout<<"Enter the first number:\n";

cin>>c1;

cout<<"Enter the Second number:\n";

cin>>c2;

c3=c1+c2;

c4=c1*c2;

cout<<"\nYour First Number is:\n";

cout<<c1;

cout<<"\nYour Second number is:\n";

cout<<c2;

cout<<"The Addition is:";

cout<<c3;

cout<<"The Multiplication is:";

cout<<c4;

return 0;

You might also like