OOP – ASSIGNMENT II

LAB MANUAL DOWNLOAD: Write a program in C++ to perform following operations on complex numbers Add, Subtract, Multiply, Divide, Complex conjugate. Design the class for complex number representation and the operations to be performed. The objective of this assignment is to learn the concepts classes and objects.

ASSIGNMENT NO 2

Title: Demonstrate use of operator overloading for Complex class.

Objectives:  To understand operations of complex number.

Problem Statement:

Write a program in C++ to perform following operations on complex numbers Add, Subtract, Multiply, Divide, Complex conjugate. Design the class for complex number representation and the operations to be performed. The objective of this assignment is to learn the concepts classes and objects

Outcomes:  Students will be able to demonstrate operations on complex number.
Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

Software requirements: 64 bit Linux/Windows Operating System, G++ compiler

Theory:

C++ Class Definitions: When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations.

The keyword public determines the access attributes of the members of the class that follow it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section.

Define C++ Objects:

A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types.

Accessing the Data Members:

The public data members of objects of a class can be accessed using the direct member access operator (.).

Complex Number: Complex numbers are “binomials” of a sort, and are added, subtracted, and multiplied in a similar way. (Division, which is further down the page, is a bit different.)

Addition of Complex Numbers: Add real parts, add imaginary parts.

Subtraction of Complex Numbers: Subtract real parts, subtract imaginary parts.

To multiply complex numbers: Each part of the first complex number gets multiplied by each part of the second complex number

 Example: (3 + 2i)(1 + 7i)
(3 + 2i)(1 + 7i)= 3×1 + 3×7i + 2i×1+ 2i×7i
= 3 + 21i + 2i + 14i2
= 3 + 21i + 2i − 14
(because i2 = −1)
= −11 + 23i
A conjugate is where we change the sign in the middle like this:
A conjugate is often written with a bar over it:
Example:
5 − 3i   =   5 + 3i
Dividing: The conjugate is used to help complex division. The trick is to multiply both top and bottom by the conjugate of the bottom.
Example:
2 + 3i
4 − 5i
Multiply top and bottom by the conjugate of 4 − 5i :
2 + 3i
×
4 + 5i
=
8 + 10i + 12i + 15i2
4 − 5i
4 + 5i
16 + 20i − 20i − 25i2
Now remember that i2 = −1, so:

=
8 + 10i + 12i − 15
16 + 20i − 20i + 25
Add Like Terms (and notice how on the bottom 20i − 20i cancels out!):

−7 + 22i
We should then put the answer back into a + bi form:

=−7+22i

4141

 

Multiplying By the Conjugate: There is a faster way though. In the previous example, what happened on the bottom was interesting:

(4 − 5i)(4 + 5i) = 16 + 20i − 20i − 25i2

The middle terms cancel out!
And since i2 = −1 we ended up with this:

(4 − 5i)(4 + 5i) = 42 + 52

Which is really quite a simple result

Conclusion: Hence, we have studied operations on complex number.

PROGRAM

#include <iostream.h>
//Program to demonstrate various operations on Complex class

class Complex
{
 float real,img;
public:
//Constructor
Complex()
{
 real=0;
 img=0;
}
Complex(float a,float b)
{
 real=a;
 img=b;
}
//Addition of two complex numbers
Complex add(Complex c1)
{
 Complex temp;
 temp.real=real+c1.real;
 temp.img=img+c1.img;
 return temp;
}
//Subtraction of two complex numbers
Complex sub(Complex c1)
{
 Complex temp;
 temp.real=real-c1.real;
 temp.img=img-c1.img;
 return temp;
}
//Multiplication of two complex numbers
Complex mul(Complex c1)
{
 Complex temp;
 temp.real=(real*c1.real)-(img*c1.img);
 temp.img=(img*c1.real)+(real*c1.img);
 return temp;
}
//Division of two complex numbers
Complex div(Complex c1)
{
 Complex temp,c2;
 c2.img=-c1.img;
 float x;
 temp.real=(real*c1.real)-(img*(c2.img));
 temp.img=(real*c1.real)+(real*(c2.img));
 x=(c1.real)*(c1.real)+(c1.img)*(c1.img);
 temp.real=temp.real/x;
 temp.img=temp.img/x;
 return temp;
} 
void read()
{
 cout<<"Enter real and imaginary part of complex number";
 cin>>real>>img;
}
void show()
{
 cout<<real<<" + "<<img<<"i";
}
};


int main()
{
Complex c1,c2,c3;
int choice;
char ans;
do
{
cout<<"\n************* MENU ************\n";
cout<<"\n\t1.Addition\n\t2.Subtraction\n\t3.Multiplication\n\t4.Division";
cout<<"\n\nEnter your choice: ";
cin>>choice;
c1.read();
c2.read();
switch(choice)
{
case 1:
 c3=c1.add(c2);
 cout<<"\n\nAddition is: ";
 c3.show();
break;
case 2:
 c3=c1.sub(c2);
 cout<<"\n\nSubtraction is: ";
 c3.show();
break;
case 3:
 c3=c1.mul(c2);
 cout<<"\n\nMultiplication is: ";
 c3.show();
break;
case 4:
 c3=c1.div(c2);
 cout<<"\n\nDivision is: ";
 c3.show();
break;
default: 
 cout<<"\nWrong choice";
} cout<<"\nDo you want to continue?(y/n): ";
 cin>>ans;
 }while(ans=='y' || ans=='Y');
return 0;
}

OUTPUT:

************* MENU ************

1.Addition
2.Subtraction
3.Multiplication
4.Division

Enter your choice: 1
Enter real and imaginary part of complex number 2 3
Enter real and imaginary part of complex number 3 3

Addition is: 5 + 6i
Do you want to continue?(y/n): y
************* MENU ************

1.Addition
2.Subtraction
3.Multiplication
4.Division

Enter your choice:2
Enter real and imaginary part of complex number 2 4
Enter real and imaginary part of complex number 3 2

Subtraction is: -1 + 2i
Do you want to continue?(y/n): y
************* MENU ************

1.Addition
2.Subtraction
3.Multiplication
4.Division

Enter your choice: 3
Enter real and imaginary part of complex number 12 3
Enter real and imaginary part of complex number 3 7

Multiplication is: 15 + 93i
Do you want to continue?(y/n): n

Leave a Reply

Your email address will not be published. Required fields are marked *