Question
a string overloaded constructor that takes a string c++ an overloaded constructer that takes an argument as a string. #ifndef FRACTION #define FRACTION class Fraction
a string overloaded constructor that takes a string c++
an overloaded constructer that takes an argument as a string.
#ifndef FRACTION #define FRACTION
class Fraction { private: int num; int den; public: void setFraction(int n, int d); Fraction add(const Fraction &f); Fraction sub(const Fraction &f); Fraction mul(const Fraction &f); Fraction div(const Fraction &f); void printFraction();
Fraction(); Fraction(int num, int den);
};
#endif
#include
using namespace std;
Fraction::Fraction() :num(1), den(1) { this->setFraction(1, 1); } Fraction::Fraction(int num, int den) { this->setFraction(num, den); } void Fraction::setFraction(int n, int d) { this->num = n; this->den = d; } Fraction Fraction::add(const Fraction &f) { Fraction tmp; tmp.num = (this->num * f.den) + (f.num * this->den); tmp.den = f.den * this->den; return tmp; } Fraction Fraction::sub(const Fraction &f) { Fraction tmp; tmp.num = (this->num * f.den) - (f.num * f.den); tmp.den = f.den * this->den; return tmp;
} Fraction Fraction::mul(const Fraction &f) { Fraction tmp; tmp.num = this->num * f.num; tmp.den = this->den * f.den; return tmp; } Fraction Fraction::div(const Fraction &f) { Fraction tmp; tmp.num = this->num * f.den; tmp.den = this->den * f.den; if (tmp.den < 0) { tmp.num *= -1; tmp.den *= -1; } return tmp;
}
void Fraction::printFraction() { cout << num << "/" << den << endl; }
#include
using namespace std;
int main() { Fraction f1(1,4), f2(1,2), f3; f3 = f1.add(f2); f3.printFraction();
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started