In this assignment, the class declaration should come first, followed by the definitions of the class member functions, followed by the client program. #include using namespace std; class Fraction { private: int numerator; int denominator; public: void set(int n,int d){ numerator=n; denominator=d; } int getNumerator(){ return numerator; } int getDenominator(){ return denominator; } Fraction addedTo(Fraction& r){ int a=this->numerator; int b=this->denominator; int c=r.numerator; int d=r.denominator; int e,f; e=(a*d+b*c); f=b*d; Fraction f1; f1.set(e,f); return f1; } Fraction subtract(Fraction& r){ int a=this->numerator; int b=this->denominator; int c=r.numerator; int d=r.denominator; int e,f; e=(a*d-b*c); f=b*d; Fraction f1; f1.set(e,f); return f1; } Fraction multipliedBy(Fraction& r){ int a=this->numerator; int b=this->denominator; int c=r.numerator; int d=r.denominator; int e,f; e=a*c; f=b*d; Fraction f1; f1.set(e,f); return f1; } Fraction dividedBy( Fraction& r){ int a=this->numerator; int b=this->denominator; int c=r.numerator; int d=r.denominator; int e,f; if(this->denominator!=0){ e=a*d; f=b*c; Fraction f1; f1.set(e,f); return f1; } Fraction f1; return f1;} bool isEqualTo(Fraction& r){ int a=this->numerator; int b=this->denominator; int c=r.numerator; int d=r.denominator; int e,f; e=a*d; f=c*d; return (e==f); } void print(){ cout<numerator<<"/"<denominator; } }; int main() { Fraction f1; Fraction f2; Fraction result; f1.set(9, 8); f2.set(2, 3); cout << "The product of "; f1.print(); cout << " and "; f2.print(); cout << " is "; result = f1.multipliedBy(f2); result.print(); cout << endl; cout << "The quotient of "; f1.print(); cout << " and "; f2.print(); cout << " is "; result = f1.dividedBy(f2); result.print(); cout << endl; cout << "The sum of "; f1.print(); cout << " and "; f2.print(); cout << " is "; result = f1.addedTo(f2); result.print(); cout << endl; cout << "The difference of "; f1.print(); cout << " and "; f2.print(); cout << " is "; result = f1.subtract(f2); result.print(); cout << endl; if (f1.isEqualTo(f2)){ cout << "The two Fractions are equal." << endl; } else { cout << "The two Fractions are not equal." << endl; } }
In this assignment, the class declaration should come first, followed by the definitions of the class member functions, followed by the client program.
#include <iostream>
using namespace std;
class Fraction {
private:
int numerator;
int denominator;
public:
void set(int n,int d){
numerator=n;
denominator=d;
}
int getNumerator(){
return numerator;
}
int getDenominator(){
return denominator;
}
Fraction addedTo(Fraction& r){
int a=this->numerator;
int b=this->denominator;
int c=r.numerator;
int d=r.denominator;
int e,f;
e=(a*d+b*c);
f=b*d;
Fraction f1;
f1.set(e,f);
return f1;
}
Fraction subtract(Fraction& r){
int a=this->numerator;
int b=this->denominator;
int c=r.numerator;
int d=r.denominator;
int e,f;
e=(a*d-b*c);
f=b*d;
Fraction f1;
f1.set(e,f);
return f1;
}
Fraction multipliedBy(Fraction& r){
int a=this->numerator;
int b=this->denominator;
int c=r.numerator;
int d=r.denominator;
int e,f;
e=a*c;
f=b*d;
Fraction f1;
f1.set(e,f);
return f1;
}
Fraction dividedBy( Fraction& r){
int a=this->numerator;
int b=this->denominator;
int c=r.numerator;
int d=r.denominator;
int e,f;
if(this->denominator!=0){
e=a*d;
f=b*c;
Fraction f1;
f1.set(e,f);
return f1;
}
Fraction f1;
return f1;}
bool isEqualTo(Fraction& r){
int a=this->numerator;
int b=this->denominator;
int c=r.numerator;
int d=r.denominator;
int e,f;
e=a*d;
f=c*d;
return (e==f);
}
void print(){
cout<<this->numerator<<"/"<<this->denominator;
}
};
int main() {
Fraction f1;
Fraction f2;
Fraction result;
f1.set(9, 8);
f2.set(2, 3);
cout << "The product of ";
f1.print(); cout << " and ";
f2.print(); cout << " is ";
result = f1.multipliedBy(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print(); cout << " and ";
f2.print(); cout << " is ";
result = f1.dividedBy(f2);
result.print(); cout << endl;
cout << "The sum of ";
f1.print(); cout << " and ";
f2.print(); cout << " is ";
result = f1.addedTo(f2);
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.subtract(f2);
result.print();
cout << endl;
if (f1.isEqualTo(f2)){
cout << "The two Fractions are equal." << endl;
}
else {
cout << "The two Fractions are not equal." << endl;
}
}
Step by step
Solved in 3 steps with 6 images