0% found this document useful (0 votes)
13 views3 pages

OOP Exp3

Uploaded by

rohitkadam25635
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)
13 views3 pages

OOP Exp3

Uploaded by

rohitkadam25635
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
You are on page 1/ 3

EXPERIMENT -03

➢ Operations on Complex Numbers:


(Using Operator Overloading)

#CODE
#include<iostream>
#include<math.h>
using namespace std;

class complex
{
float real,img;
public:
void read();
void display();

complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
complex operator~();
};

void complex::read()
{
cout<<"Enter The real and imaginary part:";
cin>>real>>img;
}

void complex::display()
{
if(img>=0)
cout<<real<<"+"<<img<<"i"<<endl;
else
cout<<real<<img<<"i"<<endl;
}

complex complex::operator+(complex c1)


{
complex sum;
sum.real=real+c1.real;
sum.img=img+c1.img;
return sum;
}

complex complex::operator-(complex c1)


{
complex sum;
sum.real=real-c1.real;
sum.img=img-c1.img;
return sum;
}

complex complex::operator*(complex c1)


{
complex mul;
mul.real=real*c1.real-img*c1.img;
mul.img=real*c1.img+img*c1.real;
return mul;
}

complex complex::operator/(complex c1)


{
complex dvd;
float den=c1.real*c1.real+c1.img*c1.img;
dvd.real=(real*c1.real+img*c1.img)/den;
dvd.img=(-real*c1.img+img*c1.real)/den;
return dvd;
}

complex complex::operator~()
{
complex con;
con.real=real;
con.img=-img;
return con;
}

int main()
{
complex c,c1,ans;
c.read();
c1.read();
ans=c+c1;
cout<<"Sum is: ";ans.display();
ans=c-c1;
cout<<"Difference is: ";ans.display();
ans=c*c1;
cout<<"Product is: ";ans.display();
ans=c/c1;
cout<<"Division is: ";ans.display();
ans=~c;
cout<<"Conjugate of 1st no: ";ans.display();
ans=~c1;
cout<<"Conjugate of 2nd no: ";ans.display();
return 0;
}

#OUTPUT
Enter The real and imaginary part:4
7
Enter The real and imaginary part:2
4
Sum is: 6+11i
Difference is: 2+3i
Product is: -20+30i
Division is: 1.8-0.1i
Conjugate of 1st no: 4-7i
Conjugate of 2nd no: 2-4i

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy