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

Oops Practicals 1

Uploaded by

patilashish8080
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)
22 views

Oops Practicals 1

Uploaded by

patilashish8080
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/ 6

PRACTICAL NO :- 1

/*

Title: 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.

Name: Sahil Bhaskar Gaikwad


Roll no: 69
Batch: S3
Branch: AI&DS
Subject: Object Oriented Programming

*/

#include<iostream>

#include<stdio.h>

//#include<conio.h>

//#include<process>

using namespace std;

class complex

int real;

float imag;

public:

complex()

real=0;

imag=0;

complex operator+ (complex);

complex operator* (complex);

friend istream &operator>>(istream &in, complex &a)


{

cout<<"\n\t Enter real part= ";

in>>a.real;

cout<<"\n\t Enter imaginary part=";

in>>a.imag;

return in;
}

friend ostream &operator<<(ostream &out, complex &a)

if (a.imag>=0)

out<<a.real<<"+"<<a.imag<<"i";

else

out<<a.real<<a.imag<<"i";

return out;

};

complex complex::operator+ (complex obj)

complex temp;

temp.real=real+obj.real;

temp.imag=imag+obj.imag;

return(temp);

complex complex::operator* (complex obj)

complex temp;
temp.real=real*obj.real-imag*obj.imag;

temp.imag=imag*obj.real+real*obj.imag;

return(temp);

int main()

complex c1, c2,c3;

int n;

do

cout<<"\n 1. Get two complex no. ";

cout<<"\n 2. Addition of two complex no. ";

cout<<"\n 3. Multiplication of two complex no. ";

cout<<"\n 4. Quit";

cout<<"\n Enter your choice=";

cin>>n;

switch(n)

case 1: //take two nos. from user

cout<<"\n Enter First Complex Number";

cin>>c1;

cout<<"\n Enter Seconds Complex No .... ";

cin>>c2;

//Display two complex nos.

cout<<"\n\n First Complex No=";

cout<<c1;

cout<<"\n Seconds Complex No=";

cout<<c2;
cout<<endl;

break;

case 2:

c3=c1+c2;

cout<<"\n complex no. after Addition is= ";

cout<<c3;

cout<<endl;

break;

case 3:

c3=c1*c2;

cout<<"\n complex no. after Multiplication is= ";

cout<<c3;

cout<<endl;

break;

case 4: return 1;

break;

default:cout<<"\n Wronng Choice";

cout<<endl;

break;

} while (n!=4);

return 0;

***************************************************************************************************
/*
Output:
student@test:~$ gedit complex.cpp
^C

student@test:~$ g++ -o complex complex.cpp

student@test:~$ ./complex

1. Get two complex no.

2. Addition of two complex no.

3. Multiplication of two complex no.

4. Quit

Enter your choice=1

Enter First Complex Number

Enter real part= 10

Enter imaginary part=5

Enter Seconds Complex No.....

Enter real part= 6

Enter imaginary part=7

First Complex No=10+5i

Seconds Complex No=6+7i

1. Get two complex no.

2. Addition of two complex no.

3. Multiplication of two complex no.

4. Quit

Enter your choice=2

complex no. after Addition is= 16+12i

1. Get two complex no.

2. Addition of two complex no.

3. Multiplication of two complex no.

4. Quit
Enter your choice=3

complex no. after Multiplication is= 25+100i

1. Get two complex no.

2. Addition of two complex no.

3. Multiplication of two complex no.

4. Quit

Enter your choice=5

Wronng Choice

1. Get two complex no.

2. Addition of two complex no.

3. Multiplication of two complex no.

4. Quit

Enter your choice=4

student@test:~$

*/

You might also like