Description of this paper

Writing Overloading Operators LAB 2A, 2B and 2C




LAB 2a: Writing Overloading Operators [ Fraction.h, Fraction.cpp, and FractionDriver.cpp ];Purpose. The purpose of this lab is for you to how to program with overloaded operators.;Requirements. Write 3 files: Fraction.h, Fraction.cpp, and FractionDriver.cpp, to solve exercise #12 on page 47 of the Childs textbook. Note that Fraction is a struct and not a class, but the file structure is exactly the same as if it were a class. Put the struct specification in the H file, the implementation in Fraction.cpp, and int main() in FractionDriver.cpp.;Do NOT define any of the operators in the driver's CPP -- define them all in the Fraction's H and CPP. Do NOT forget to test ALL of the specified operators, even the one that does int times Fraction! Do NOT write any additional prototypes or functions in Fraction's specification file, because doing so affects the specified "public interface". So, no "constructors", no "setters", no nothing beyond exactly what's specified!;You may write the overloaded operator functions as member functions or stand-alone, as you wish. Put prototypes for stand-alone functions in Fraction.h, AFTER the closing curly brace and semicolon of the struct definition, and ABOVE the closing #endif. Put function definitions for stand-alones in Fraction.cpp WITHOUT Fraction:: scope resolution.;You may use ints, floats, or doubles for the numerator and denominator, as you wish, but I strongly recommend ints. Also, you do not have to reduce fractions. 1/4 plus 1/4 is 2/4 -- it does not have to be reduced to 1/2. 1/2 plus 1/4 is 6/8 -- it does not have to be reduced to 3/4. But if you do write a function to reduce your fractions, it must work correctly, and not be part of the public interface. That means its prototype should be after the #include for Fraction.h in Fraction.cpp, NOT in Fraction.h itself, in order to "hide" it from other CPPs that may include Fraction.h.;Besides the normal test driver requirements for ifndef and copies, include as much testing in the driver as you feel is necessary to assure that your struct works right. But at least you should test every operator to assure that each of them works exactly as expected. When testing object copies, since this is a struct, you have direct access to the data members. So instead of using getters to indirectly confirm that the copy was correctly made, just check the data member values.;Include value validation and assertions as appropriate. Submit the three files (2 CPPs and 1 H) to the class website for credit.;Program I/O. Input: All hard-coded in the driver CPP only. Output: From the driver CPP only, console (cout) output only.;LAB 2b: Writing Templated Classes [ Rectangle.h and RectangleDriver.cpp ];Purpose. The purpose of this lab is for you to learn how to create and apply a templated class. It also demonstrates the concept of "data abstraction", as the Rectangle class you write is to know or care nothing about the Fraction class!;Requirements. Write 2 files: Rectangle.h and RectangleDriver.cpp, to solve exercise #13 on page 47 of the Childs textbook. Put the templated class specification and its supporting function templates in the class' H file, and put int main() in RectangleDriver.cpp. Include as much testing in the driver as you feel is necessary to assure that your class works right, refering to lab 1 for object copy testing.;Use your Fraction struct files -- do NOT write new Fraction struct code. This means: DO include the Fraction's H and CPP in this project. But do NOT #include Fraction.cpp in anything -- if you have to do this in order to get this to compile, then your compile command is not correct. Do NOT write a Rectangle.cpp file -- what would have gone into a Rectangle.cpp file should be in Rectangle.h, instead. Do NOT test Fraction in Rectangle's driver -- it should have been tested already in lab 2a above. If Rectangle's H contains any reference to Fractions, you are not doing this right!;NOTE: For this and all future lab work in this course, it's NOT okay to write a separate templated class CPP file to contain the function templates, as modeled in our textbook. The textbook's way of writing templated classes is to use separate H and CPP files, but the way shown in lecture is to write only an H file. Both ways work and are equally valid in real-life practice, but this is in the specifications for COMSC 210. This applies to templated classes only -- do NOT put non-templated function definitions in H files.;Include value validation and assertions as appropriate. Submit the two files (1 CPP and 1 H) to the class website for credit.;Program I/O. Input: All hard-coded in the driver CPP only. Output: From the driver CPP only, console (cout) output only.;LAB 2c: Using Templated Classes [ Calculator2.cpp ];Purpose. The purpose of this lab is for you to learn how to apply the STL stack to a practical problem solution.;Requirements. Write 1 file: Calculator2.cpp, to solve exercise #14 on page 47-49 of the Childs textbook. But instead of using the Stack and Array classes referred to in the exercise, use an STL stack instead. You choose the data type for your stack -- there are more ways than one to do this right. Also, consider only the operations plus, minus, multiply, and divide, using floating point numbers.;Use the console for input, one token at a time, separated by pressing ENTER after each. Let an uppercase or lowercase Q terminate the program. Refer to the "how to's" at the bottom of the Lecture Topic 1 outline for dealing with strings. Note that 0 (zero) is a valid input -- if a user enters a 0, then a 0 should appear on the stack. In fact, if a user enters anything that resolves to zero, other than q or Q, consider it to be a zero.;DO NOT consider parentheses for forcing the order of operation. DO NOT worry about division by zero or any other numeric validation. DO avoid popping from the stack when the stack is empty -- for example, if the user enters a plus operator, and there are fewer than two values in the stack, ignore the user's request.;The solution will be discussed in class during lecture. Submit the CPP file to the class website for credit.;Program I/O. Input: From the console (cin), one entry per line. Remember to NOT read numerics directly from cin -- read them as strings and convert. Do NOT type multiple entries on the same line of input. Include the current stack contents in the input prompt. Output: To the console (cout). Formatting of output precision is not required or recommended, but if you choose to do so, remember to NOT use the manipulator fixed because it is not compatible with all compilers.;Example (computer prompts in bold). Your program should match these RESULTS, formatted as you choose.;Example: five plus six;Enter: 5 [ENTER];Enter: 5 6 [ENTER];Enter: 6 5 + [ENTER];Enter: 11.000000 Q [ENTER];Example: ten minus one;Enter: 10 [ENTER];Enter: 10 1 [ENTER];Enter: 1 10 - [ENTER];Enter: 9.000000 Q [ENTER];Example: one divided by two;Enter: 1 [ENTER];Enter: 1 2 [ENTER];Enter: 2 1 / [ENTER];Enter: 0.500000 Q [ENTER];Example: Pi times the radius squared;Enter: 3.14159 [ENTER];Enter: 3.14159 18 [ENTER];Enter: 18 3.14159 18 [ENTER];Enter: 18 18 3.14159 * [ENTER];Enter: 324.000000 3.14159 * [ENTER];Enter: 1017.875160 Q [ENTER];Example: (1+2)/(3+4);Enter: 1 [ENTER];Enter: 1 2 [ENTER];Enter: 2 1 + [ENTER];Enter: 3.000000 3 [ENTER];Enter: 3 3.000000 4 [ENTER];Enter: 4 3 3.000000 + [ENTER];Enter: 7 3.000000 / [ENTER];Enter: 0.428571 Q [ENTER];Lab 2A;12. Create a Fraction struct that has a numerator and a denominator. Make an overloadedoperator + for the struct that can add two Fractions and return a Fraction result. Makean overloaded * that can also multiply two Fractions together. Make an overloadedoperator > that can compare two Fractions and determine whether the first one isgreater than the second one. Make another overloaded operator * that has a Fraction asa first operand and an integer as a second operand, which together produce a Fraction result,and a third overloaded operator * which has an integer as a first operand and a Fractionas a second operand.;LAB 2B;13. Create a Rectangle class template that can store the lengths of the sides (in the private section)as any DataType. Make class functions for setting the width and length of the rectangle.Make a function that will return 'true' if the length is greater than the width and 'false'otherwise. Make two other functions that will return the perimeter and area of the Rectangle.Test the Rectangle class template by using a float for DataType and a Fraction forDataType (from Exercise 12).;Attachments;LAB_2A,_2B_and_2c.pdf (774.2 KB)


Paper#32169 | Written in 18-Jul-2015

Price : $42