pls write a program in c++. for; --> Design a new class to represent a fraction
ID: 3737809 • Letter: P
Question
pls write a program in c++.
for; -->
Design a new class to represent a fraction (a ration of two integer values) 15 22 - - Numerator Denominator The data members of the class Fraction are two integers top and bottom, denoting the numerator and denominator, respectively class Fraction private: // Numerator // Denominator 4 int top; int bottom; public: Part 1: Fundamental requirements for the class Fraction. 1. Fractional numbers can be declared with both a numerator and denominator, or simple numerator: Fraction a; Fraction b(3,4); Fraction c(5); // represents 0/1 // represents 3/4 // represents 5/1 2. Fractions should be able to act just like other numbers. Add, subtract, multiple and divide; Compare based on values; Input and output.Explanation / Answer
class Fraction
{
private:
int top;
int bottom;
public:
Fraction(){
top = 0;
bottom = 1;
}
Fraction(int a){
top = a;
bottom = 1;
}
Fraction(int a, int b){
top = a;
bottom = b;
}
Fraction add(Fraction b);
Fraction subtract(Fraction b);
Fraction multiple(Fraction b);
Fraction divide(Fraction b);
int compare(Fraction b);
void setFraction(int a, int b);
void setNumerator(int a);
void setDenominator(int a);
int getNumerator();
int getDenominator();
void printFraction();
};
Fraction Fraction :: add(Fraction b){
Fraction temp;
temp.bottom = this->bottom*b.getDenominator();
temp.top = this->top*b.getDenominator() + b.getNumerator()*this->bottom;
int i = temp.bottom<temp.top? temp.bottom : temp.top;
while(i>1){
if(temp.bottom%i == 0 && temp.top%i == 0){
temp.bottom /= i;
temp.top /= i;
break;
}
i--;
}
return temp;
}
Fraction Fraction :: subtract(Fraction b){
Fraction temp;
temp.bottom = this->bottom*b.getDenominator();
temp.top = this->top*b.getDenominator() - b.getNumerator()*this->bottom;
int i = temp.bottom<temp.top? temp.bottom : temp.top;
while(i>1){
if(temp.bottom%i == 0 && temp.top%i == 0){
temp.bottom /= i;
temp.top /= i;
break;
}
i--;
}
return temp;
}
Fraction Fraction :: multiple(Fraction b){
Fraction temp;
temp.bottom = this->bottom*b.getDenominator();
temp.top = this->top*b.getNumerator();
int i = temp.bottom<temp.top? temp.bottom : temp.top;
while(i>1){
if(temp.bottom%i == 0 && temp.top%i == 0){
temp.bottom /= i;
temp.top /= i;
break;
}
i--;
}
return temp;
}
Fraction Fraction :: divide(Fraction b){
Fraction temp;
temp.bottom = this->bottom*b.getNumerator();
temp.top = this->top*b.getDenominator();
int i = temp.bottom<temp.top? temp.bottom : temp.top;
while(i>1){
if(temp.bottom%i == 0 && temp.top%i == 0){
temp.bottom /= i;
temp.top /= i;
break;
}
i--;
}
return temp;
}
int Fraction :: compare(Fraction b){
if(this->top*b.getDenominator() < b.getNumerator()*this->bottom){
return -1;
}else if(this->top*b.getDenominator() > b.getNumerator()*this->bottom){
return 1;
}else{
return 0;
}
}
void Fraction :: setFraction(int a,int b){
this->top = a;
this->bottom = b;
}
void Fraction :: setNumerator(int a){
this->top = a;
}
void Fraction :: setDenominator(int a){
this->bottom = a;
}
int Fraction :: getNumerator(){
return this->top;
}
int Fraction :: getDenominator(){
return this->bottom;
}
void Fraction :: printFraction(){
cout << "Numerator: " << this->top << " Denominator: " << this->bottom << endl;
}
In above code Design and implementation of Fraction class is given. For design it is to be taken care that top and bottom are to be declared as private variables i.e. they can't be accessed directly outside the class so functions have to be made to set them or to get them. Also basic mathematical operations like add, subtract, multiply, divide, compare are to be implemented to make fractions behave as numbers. Compare functions retruns 1 if it is greater, -1 if it is smaller, 0 if it is equal. Also an optional function to print a fraction object is desined. Also the constructor is overloaded so that it can 0,1,2 no. of arguments.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.