Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++: For an assignment, we are supposed to take the finished code and create an overloaded constructor that takes as an argument a string. The

C++:For an assignment, we are supposed to take the finished code and create an overloaded constructor that takes as an argument a string. The string will contain a fraction, which needs to be parsed. My professor said that the string can be as simple as "1 2", with the space between the two, with the numbers being the numerator and the denominator respectively. Here is the code it needs to be built upon:

fraction.h

#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 mult(const Fraction &f);

Fraction div(const Fraction &f);

void printFraction();

//Constructors

Fraction();

Fraction(int num, int den);

Fraction(int num, int den, string frac);

};

#endif

fraction.cpp

#include

#include "fraction.h"

using namespace std;

Fraction::Fraction()

{

this->setFraction(1, 1);

}

Fraction::Fraction(int num, int den)

{

this->setFraction(num, den);

}

Fraction::Fraction(int num, int den, string frac)

{

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 * den);

tmp.den = f.den * this->den;

return tmp;

}

Fraction Fraction::mult(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.num;

if (tmp.den < 0)

{

tmp.num *= -1;

tmp.den *= -1;

}

return tmp;

}

void Fraction::printFraction()

{

cout << this->num << "/" << this->den << endl;

}

main.cpp

#include

#include "fraction.h"

using namespace std;

int main()

{

Fraction f1(1, 4), f2(1, 2), f3, f4, f5, f6;

f3 = f1.add(f2);

f4 = f1.sub(f2);

f5 = f1.mult(f2);

f6 = f1.div(f2);

f3.printFraction();

f4.printFraction();

f5.printFraction();

f6.printFraction();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To complete your assignment by creating an overloaded constructor that takes a string representing a fraction we need to parse that string to extract ... 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

Step: 3

blur-text-image

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

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

3642271561, 978-3642271564

More Books

Students also viewed these Databases questions

Question

Define promotion.

Answered: 1 week ago

Question

Write a note on transfer policy.

Answered: 1 week ago

Question

Discuss about training and development in India?

Answered: 1 week ago

Question

Explain the various techniques of training and development.

Answered: 1 week ago

Question

Explain the various techniques of Management Development.

Answered: 1 week ago

Question

Additional Factors Affecting Group Communication?

Answered: 1 week ago