Question
Assignment 2 Fractional Number Test In this assignment, you will practice two main Object Oriented C++ programming techniques: operator overloading and template class. Our focus
Assignment 2 Fractional Number Test
In this assignment, you will practice two main Object Oriented C++ programming techniques: operator overloading and template class. Our focus in this course is the algorithmic concept and solution. Although the class programming skills are equally important, however with limited time available for one semester, we will rely on the powerful container classes provided by the C++ Standard Template Library and open source Boost Library.
Even the above library container classes are available to us, we still need to know how to use it and make modification to those ready made containers. The C++ Object Oriented programming techniques are still required, however we will leave out most of the advanced C++ OO programming from this course. You are encouraged to take COMSC-200 Object Oriented C++ Programming in parallel or following this course.
Each exercise allows you to write a programming solution "from scratch", without a lot of guidance. All that's required is that you apply the programming tools and conventions presented so far in this course.
As you complete this assignment, post the required file(s) to the D2L. For assignments that involve more than one file, you may submit them in one .zip ZIP file. This assignment is worth 25-points, to be awarded after all assignments are successfully completed.
AS2: Fraction Number Test
The AS2 requires the techinque to create linked list. Since part of the topics are covered in module 3, hence the AS2 due date would be the same as AS3.
In this assignment, we are going to create a Fraction Number (Frac.h) class that supports all essential methods required by the STL. We are going to test the features using the STL list container.
Two starter files are included:
a sample floating point linked list application (testList.cpp (Links to an external site.)Links to an external site.) https://pastebin.com/980Ed78E
a fractional number class declaration (interface) (Frac_declaration.h (Links to an external site.)Links to an external site..) https://pastebin.com/XHMzDhcB
The following steps shall be followed to complete this assignment
Complete all method definitions for the Frac class (Frac.h).
Change the (STL) list container type in the testList.cpp from the double type to the Frac type.
Recreate the same test logic applied on the double list for this Frac list.
To verify your work against the sample test below.
Hint:
1.operator double(), type casting
For item 2 above, the type conversion from Frac to double is covered on Ch14.6 Object Conversion (type casting) p.858 of Gaddis textbook (8th ed.) . This is achived by overloading a data type constructor to take in another type ad an argument. For example, to explicit convert Frac to a double type is achieved by overloading the double constructor to take a Frac instance as an argument. Here is how this type conversion method declaration:
Frac::operator double();
2. operator+, arithmetic operator
We have demonstrated a common implementation of the operator+ of the Frac type math operator:
Frac operator + (const Frac &right) { Frac temp; temp.num = num*(right.den) + den*(right.num); temp.den = den*(right.den); return temp; }
Also, one would intuitively solve the relational operator the same way. However, programing always have alternative solutions. For example, we can define the operator> (greater than) as
bool operator<(Frac& rhs){return (get_value() < rhs.get_value()) ? true : false;} // and double get_value() {return double(num)/den;}
There are alway alternative solutions!
3. operator> relationship operator
To use the technique illustrated above, simplifies the expression.
4. string constructor
For an application program, one of the most useful constructor for a user define data type is the string constructor.
The text representation of a Fractional Number of of 3/4 in C++ is "3/4". The desirable constructor for a Frac instance would be:
Frac xyz("3/4");
5. operator++ and operator--
See the detail in Chapter 14.5 Operator Overloading, page 843.
Both the increment and decrement operator has pre and post operations.
For example, the operator++:
The pre operation, ++var, works the same as regular operator+, add one unit and return the result.
The post operation, var++, is to perform the operation after the value is returned. This is tricky in a single thread function. The way to achieve this effect is
to create a copy of the original value,
to perform the increment
return the copy (original value)
Hence the definitions for pre and post decrements are:
Frac operator-- () { --num; return lowterms(*this); } // pre Frac operator-- (int) { Frac temp(*this); num--; lowterm(*this); return temp; } // post
Again, the usage of the member method declaration format for pre and post operations must be:
frac operator-- (); // pre frac operator-- (int); // post
Sample Test Driver
Here is the a sample of additional test driver (Links to an external site.)Links to an external site. to test the above 5 Frac class methods.
Sample Test Run
The following test output is tested using the above test driver and your completed Frac class definition.
Running /home/ubuntu/workspace/comsc210/Week02/testFrac3.cpp Create x: 3/4 Create y: 2/3 Create z from x: 3/4 Assign z as y: 2/3 x+y: 3/4 + 2/3 is 17/12 x/y: 3/4 / 2/3 is 9/8 3/4 is greater than 2/3 2/3 is not greater than 3/4 x: 3/4 y: 2/3 a: 1/2 b: 2/4 1/2 is equal to 2/4 Step by Stp inc/dereament a: 1/2, 1/2, 3/2, 5/2, 3/2, 1/2 Continuous inc/decrement a: 1/2, -1/2, -3/2, -1/2, 1/2, 1/2
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