Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include "Fraction.h"

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 #include "Fraction.h"

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

=+c. What reforms have been proposed to fix what's wrong?

Answered: 1 week ago