Beruflich Dokumente
Kultur Dokumente
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.
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;
}
int main()
{
complex c1,c2,c3,c4,c5;
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