Sie sind auf Seite 1von 5

Assignment No: 9

Program Statement :
Design a class fraction having num and deno as data members. Use
suitable constructors to initiate the object of the class. Use suitable
member functions to add, subtract and multiply two objects of
complex class. Also use necessary auxiliary member functions.

Algorithm:
Here we have a class fraction having real and img as data members.
This class have add, subtract, and multiply operations of two objects
of the class. This operations are done by member functions.

Algorithm of Complex add() function:


This function is called by c1 and c2 is passed as parameter.
Step 1 : Start.
Step 2 : Create an object ‘c3’ for class complex.
Step 3 : c3.real← real+c2.real
Step 4 : c3.img←img+c2.img
Step 6 : Return c3.

Algorithm of Complex sub() function:


This function is called by c1 and c2 is passed as parameter.
Step 1 : Start.
Step 2 : Create an object ‘c4’ for class complex.
Step 3 : c4.real ← real-c2.real
Step 4: c4.img ← img-c2.img
Step 6 : Return c4.

Algorithm of Complex mul() function:


This function is called by c1 and c2 is passed as parameter.
Step 1 : Start.
Step 2 : Create an object ‘c5’ for class complex.
Step 3 : c5.real ← (real*c2.real)-(img*c2.img)
Step 4: c5.img ← (real*c2.img)+(c2.real*img)
Step 6 : Return c5.

Program Code:

#include<iostream>
using namespace std;

class complex
{
int real;
int img;
public:
complex(){
real=0;
img=0;}

void getdata();
void showdata();

complex add(complex);
complex sub(complex);
complex mul(complex);
};

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

}
void complex::getdata()
{
cout<<"Enter the real part: ";
cin>>real;
cout<<"Enter the imaginary part:";
cin>>img;
cout<<endl;
}

complex complex::add(complex c2)


{
complex c3;
c3.real=real+c2.real;
c3.img=img+c2.img;
return c3;
}

complex complex::sub(complex c2)


{
complex c4;
c4.real=real-c2.real;
c4.img=img-c2.img;
return c4;
}

complex complex::mul(complex c2)


{
complex c5;
c5.real=(real*c2.real)-(img*c2.img);
c5.img=(real*c2.img)+(c2.real*img);
return c5;
}

int main()
{
complex c1,c2,c3,c4,c5;

cout<<" Enter the first complex number: "<<endl<<endl;


c1.getdata();
cout<<" Enter the second complex number: "<<endl<<endl;
c2.getdata();

cout<<"The first complex number: ";


c1.showdata();
cout<<endl;

cout<<"The second complex number: " ;


c2.showdata();
cout<<endl;

c3=c1.add(c2);
cout<<"The result after addition: ";
c3.showdata();
cout<<endl;

c4=c1.sub(c2);
cout<<"The result after substraction: ";
c4.showdata();
cout<<endl;

c5=c1.mul(c2);
cout<<"The result after multiplication: ";
c5.showdata();
cout<<endl;
}
Input/output:
Set: 1

Set: 2

Das könnte Ihnen auch gefallen