WAP to Create a class Fraction containing data members as
Numerator and Denominator.
Write a C++ program to overload operators ++ , -- and * to
increment , decrement a Fraction and multiply two Fraction respectively.(Use
constructor to initialize values of an object).
#include<iostream.h>
#include<conio.h>
class fraction
{
int num;
int deno;
public:
fraction()
{
}
fraction(int a,int b)
{
num=a;
deno=b;
}
void display()
{
cout<<"\n The numerator is-"<<num;
cout<<"\n The denominator is-"<<deno;
cout<<"\nFraction
is-"<<num<<"/"<<deno;
}
void operator ++()
{
num=++num;
deno=++deno;
}
void operator --()
{
num=--num;
deno=--deno;
}
friend fraction operator *(fraction &, fraction &)
};
fraction operator *(fraction &p, fraction &q)
{
fraction a;
a.num=p.num * q.num;
a.deno=p.deno * p.deno;
return a;
}
void main()
{
fraction f1(2,1);
fraction f2(3,2);
fraction f3;
clrscr();
f1.display();
f2.display();
cout<<"\nAfter decrement-";
--f1;
f1.display();
--f2;
f2.display();
cout<<"\nAfter increment-";
++f1;
f1.display();
++f2;
f2.display();
f3=f1*f2;
cout<<"\nAfter multiplication-";
f3.display();
getch();
}