0% found this document useful (0 votes)
12 views5 pages

22261_assignment4 (1)

The document outlines an assignment for implementing a Complex class in C++ that represents complex numbers. It includes functionalities such as addition and multiplication through operator overloading, as well as input and output operations. The implementation demonstrates the use of constructors and friend functions for efficient programming using the Standard Template Library.
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)
12 views5 pages

22261_assignment4 (1)

The document outlines an assignment for implementing a Complex class in C++ that represents complex numbers. It includes functionalities such as addition and multiplication through operator overloading, as well as input and output operations. The implementation demonstrates the use of constructors and friend functions for efficient programming using the Standard Template Library.
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

Progressive Education Society's

Modern College of Engineering, Pune-05.


(An Autonomous Institute Affiliated to Savitribai Phule Pune University)
Department of Computer Engineering

Assignment No: 4
Roll No: 22261 Name: Saniya Sheikh

Div: B Batch: S3(c)

Course Outcome: CO203L.2: Apply operator overloading and Standard Template Library (STL)
for efficient programming and algorithm development.

Problem Statement: Implement a class Complex which represents the Complex Number
data

type. Implement the following

a) Constructor (including a default constructor which creates the

complex number 0+0i).

b) Overload operator+ to add two complex numbers.

c) Overload operator* to multiply two complex numbers.

d) Overload operators << and >> to print and read Complex Numbers.

program:

#include<iostream>

using namespace std;

class complex

public:

int real,imag;

complex()

{
real=0;

imag=0;

friend complex operator +(complex,complex);

friend complex operator *(complex,complex);

friend istream & operator >>(istream &,complex &);

friend ostream & operator <<(ostream &,complex &);

};

complex operator +(complex c1,complex c2)

complex temp1;

temp1.real=c1.real+c2.real;

temp1.imag=c1.imag+c2.imag;

return temp1;

complex operator *(complex c3,complex c4)

complex temp2;

temp2.real=c3.real*c4.real-c3.imag*c4.imag;

temp2.imag=c3.imag*c4.real+c3.real*c4.imag;

return temp2;

istream & operator >>(istream &in,complex &obj1)

cout<<"Enter Real Part:";

in>>obj1.real;
cout<<"Enter Imaginary Part:";

in>>obj1.imag;

return in;

ostream & operator <<(ostream &out,complex &obj2)

if(obj2.imag>=0)

out<<obj2.real<<"+"<<obj2.imag<<"i\n";

else

out<<obj2.real<<obj2.imag<<"i\n";

return out;

int main()

complex c1,c2;

cout<<"\nEnter First Complex Number:"<<endl;

cin>>c1;

cout<<"\nEnter Second Complex Number:"<<endl;

cin>>c2;

complex sum;

sum=c1+c2;
complex multi;

multi=c1*c2;

cout<<"\n------------------------------";

cout<<"\nCOMPLEX NUMBER 1:"<<c1;

cout<<"\nCOMPLEX NUMBER 2:"<<c2;

cout<<"\nADDITION:"<<sum;

cout<<"\nMULTIPLICATION:"<<multi;

return 0;

output:

Inference: This complex class models complex numbers with real and imaginary parts. It supports
addition and multiplication through operator overloading, allowing natural arithmetic operations
on complex objects. The << and >>operators are overloaded for easy input and output, enhancing
usability and readability in I/O operations.

You might also like