Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Please follow the instructions and solve it by C++. Thank you! Submit testFrac.cpp (a starter is provided below.) Frac.h or you may combine into

// Please follow the instructions and solve it by C++. Thank you!

Submit

  1. testFrac.cpp (a starter is provided below.)
  2. Frac.h or you may combine into one single testFrac.cpp
  3. Verification (All Frac class members test requirement 1-6 must be tested)
  4. Explanation of the different output pattern on running the special ++/-- test provided for this project.

Part-1 For Five groups of member functions:

  • Constructors (inc. string constructor)
  • Getter/setters
  • Math
  • Type cast
  • Friend << and >>

Part-2 For the explanation for the behavior observed by running the provided test pattern:

six (6) operations in one statement vs. six (6) operations in six (6) statements.

/////////////////////

Project Requirement

The form and format the Fractional number to be used

We are only going to

(1) use two numbers: numerator over denominator to represent a fraction, no mixed numerals.

(2) allow the usage of the proper and improper fraction:

  • Proper Fraction: the number is inferior to the denominator, for instance 3/4 ;
  • Improper fraction: the numerator is superior to the denominator, for instance 9/2 ;

We are not going to use Mixed Fraction and Equivalent Fractions:

  • Mixed Fraction or Mixed Numeral: it is composed of a whole part and a fractional one, for instance 2 1/3 ;
  • Equivalent Fractions: fractions that keep on the same proportion of another fraction, for instance: 5/2 =10/4; All Fraction shall be the minimum number representation.

TODO List

Part-1

To complete the definition of all Five (5) Required Frac class method groups:

1. Constructors: * you may combine the first 3 contructors below into one.

  • Default: numerator default as 0, denominator default as 1
  • One (1) argument Fill Constructor: (int numerator) numerator only, denominator default as 1
  • Two (2) arguments Fill Constructor: (int numerator, int denominator)
  • String Constructor: (string s); where s in the format of "3/4"

2. Necessary getters and setters

3. Support the following operators:

  • basic math: +, -, *. /
  • pre and postfix ++ and -- (4 different types)
  • comparators: <, >, <=, >=, !=, ==

4. Type conversion operators, to convert Frac numbers to integer or floating point numbers:

 cout << " First Frac f1: " << f1 << " Second Frac f2: " << f2 << " 1st fi as Integer: " << int(f1) << " 2nd fi as Double: " << double(f2);

5. Test overload friend iostream insertion <<, and extraction >> operator

You must create your own test driver for ALL above specified class operation methods code for verification purpose. There are some sample codes written in the github, however, if not in the github provided test driver, you must complete the missing ones.

//////////////////////

Part-2

Sequence of operations in 1 statements vs. 6 statements 5 pts

To include the Special ++/-- Test Pattern (inside the testFrac_starter.cpp) as part of your final test program.

 // line #74 onward cout << " Please observe the outputs of identical commands " << " executed in one statement v. separated statements. "; Frac f(5,6); cout << f << " " // start << --f << " " << f << " " << ++f << " " << --f << " " << ++f << endl; // end of one statement cout << f << " " ; cout << --f << " " ; cout << f << " " ; cout << ++f << " " ; cout << --f << " " ; cout << ++f; cout << " Why the above output values are not the same? ";

After successfully implemented and executed your program:

Final Check List

  • To complete all method's definition based on the following member method declaration of the Frac Class for Project 1. (the declaration only Frac.h in Github m03 folder)
  • Complete the testFrac.cpp program (testFrac_starter.cpp is provided in Github). Please expand the tester to make sure all the Frac member methods are verified with your own tester.
  • Make sure no error or any unverified member methods (point deduction)
  • Provide the reason/explanation on why the increment/decrement test sequence (one statement vs 6 statements) produce different outcome.

///////////////

(Frac.h)

// Function Prototypes for Overloaded Stream Operators

// Forward declaration needs to be filled

// if multifiles are used, make sure to place the inclusion guard.

#include

class Frac;

ostream &operator << (ostream &, const Frac &);

istream &operator >> (istream &, Frac &);

class Frac {

private:

int num, den;

long gcd(long a, long b)

Frac lowterms(Frac &f);

public:

Frac();

Frac(string s);

Frac(int num_, int den_);

Frac(const Frac& rhs);

Frac operator=(const Frac& rhs);

// math + - * must be minimum term, i.e. no 2/8

Frac operator + (Frac &rhs);

Frac operator - (Frac &rhs);

Frac operator * (Frac &rhs);

Frac operator / (Frac &rhs);

// increment ++ decrement --

Frac operator++();

Frac operator++(int);

Frac operator--();

Frac operator--(int);

// comparators

bool operator == (Frac &f2);

bool operator != (Frac &f2);

bool operator < (Frac &f2);

bool operator > (Frac &rhs);

bool operator <= (Frac &f2);

bool operator >= (Frac &f2);

// overloading >> << stream operators

friend istream& operator>>(istream& strm, Frac& f);

friend ostream& operator<<(ostream& strm,const Frac& f) {

};

//////////////////

(testFrac_starter.cpp)

// This starter is a one-file all inclusive test appliation.

// This starter combines with the necessary Frac definition.

// Do not re-include the Frac.h

// If you prefer a multiple file approach, separte them cleanly.

#include

#include

#include // stringstream

using namespace std;

class Frac;

ostream &operator<< (ostream &stm, Frac const &rhs);

class Frac {

long num;

long den;

public:

// Frac() { num=0; den=1; }

Frac() : num(0), den(1) {}

Frac(long n, long d) {num=n; den=d;}

Frac(const Frac &obj) {*this = obj;}

void operator=(const Frac &rhs) {

num = rhs.num; den = rhs.den;

} // Frac a = b;

// string constructor is challenging, worth 2 pts, try your best!

Frac(string s);

// math operators

Frac operator+(const Frac &rhs) {

Frac temp;

temp.num = num*rhs.den + rhs.num*den;

temp.den = den*rhs.den;

// need to apply lower term by / gcd here

return temp;

}

// postfix increment operator ++ --

Frac operator++() {

num += den;

// lowterms(*this);

return *this; }

Frac operator--() {

num -= den;

// lowterms(*this);

return *this; }

// overload ostream insertion operator<<

friend ostream &operator<< (ostream &stm, Frac const &rhs) {

stm << rhs.num << "/" << rhs.den; }

};

int main() {

Frac x(3,4);

Frac y(1,2);

cout << " Created Frac x(3,4) as " << x;

cout << " Created Frac y(1,2) as " << y;

// Turn on this one when you completed the definition of string constructor

// Frac s("6/7"); // passing a Frac number as a string.

// cout << " String constructed: s: " << s;

Frac z(x);

cout << " Copy constructed z as x: " << z;

Frac v = x + y;

cout << " x + y is: " << v;

cout << " Please observe the outputs of identical commands "

<< " executed in one statement v. separated statements. ";

Frac f(5,6);

cout << f << " " // start

<< --f << " "

<< f << " "

<< ++f << " "

<< --f << " "

<< ++f << endl; // end of one statement

cout << f << " " ;

cout << --f << " " ;

cout << f << " " ;

cout << ++f << " " ;

cout << --f << " " ;

cout << ++f;

cout << " Why the above output values are not the same? ";

}

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

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

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

Explain what is meant by the terms unitarism and pluralism.

Answered: 1 week ago