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.

Class:-SE Computer  OOP & CG Practical

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.

 Solution :- 

#include<iostream>

using namespace std;

class complex

{

float x;

float y;

public:

 complex()

{

x=0;

y=0;

}

complex operator+(complex);

complex operator*(complex);

friend istream &operator >>(istream &input,complex &t)

{

cout<<"Enter the real part";

input>>t.x;

cout<<"Enter the imaginary part";

input>>t.y;

}

friend ostream &operator <<(ostream &output,complex &t)

{

output<<t.x<<"+"<<t.y<<"i\n";

}

};

complex complex::operator+(complex c)

{

complex temp;

temp.x=x+c.x;

temp.y=y+c.y;

return(temp);

}

complex complex::operator*(complex c)

{

complex temp2;

temp2.x=(x*c.x)-(y*c.y);

temp2.y=(y*c.x)+(x*c.y);

return (temp2);

}

int main()

{

complex c1,c2,c3,c4;

 cout<<"Default constructor value=\n";

 cout<<c1;

cout<<"\nEnter the 1st number\n";

cin>>c1;

cout<<"\nEnter the 2nd number\n";

cin>>c2;

c3=c1+c2;

c4=c1*c2;

cout<<"\nThe first number is ";

cout<<c1;

cout<<"\nThe second number is ";

cout<<c2;

cout<<"\nThe addition is ";

cout<<c3;

cout<<"\nThe multiplication is ";

cout<<c4;

return 0;

}

Popular posts from this blog

Develop an object oriented program in C++ to create a database of student information system containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the database with suitable member functions for initializing and destroying the data viz constructor, default constructor, Copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete.

Calculate the Nth term - Hacker Rank Solution .

Imagine a publishing company which does marketing for book and audiocassette versions. Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float).Write a program that instantiates the book and tape classes, allows user to enter data and displays the data members. If an exception is caught, replace all the data member values with zero values.