Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to design a class called Rational with public member functions read, write, add, subtract, multiply, divide. The prototype for the read and write

You are to design a class called Rational with public member functions read, write, add, subtract, multiply, divide. The prototype for the read and write methods should be:

1) You are to design a class called Rational with public member functions read, write, add, subtract, multiply, divide. The prototype for the read and write methods should be: bool read(); void write() const; The second const means that the method write can't modify the class data members. The method read returns true if the read operation was successful. It returns false if end of file was encountered before the read was completed. 2) The prototype for all arithmetic functions should be of the form Rational add(const Rational & rhs) const The const in the parameter list means the function add cant modify rhs object and the second const means that method add can't modify the class data members. For example: Rational R1, R2; If (R1.read() && R2.read()){ Rational R3 = R1.add(R2); R3.write(); } 3) Create a header file called rational.h which contains the class definitions. Include a definition for a default constructor, a copy constructor, and a constructor that takes two integers to be used as numerator and denominator. 4) Create the file rational.cpp which uses #include to include rational.h and which contains the implementations of the constructors and the other methods. 5) The default constructor should create the Rational 0/1. The constructor that takes two explicit integers should check for the denominator being zero. If it is, your implementation should ignore both parameters and create the Rational 0/1 instead. Do not output error messages from constructors or any other Rational methods. 6) Create another C++ source file called main.cpp, which uses #include to include rational.h and which reads directive of the form: Rational operator Rraction operator is one of the characters +, -, *, /. Your main code should invoke the Rational read method when reading the two Rationales, and it should read the operator character in the standard way. Based on the operator, you are to invoke the appropriate method and produce an output line on the screen of the form: Rational operator Rraction = Rational_in_lowest_terms For example when the user enter the following: 1/3 + 1/6 The output should be: 1/3 + 1/6 =1/2 Not the output shouldnt be 1/3 + 1/6 =2/6 (wrong) So you will need to declare and implement a method simplify that converts a rational to its simplest form. 7) When generating the output, be sure to use the Rational write routine for all three Rationales. Your program is to continue read directives until end of file (EOF) is sensed while trying to read one of the inputs. Write another main function, main_file.cpp that reads the rational numbers and the operations from a text file instead of the screen. 9) What to turn in: a. The source files rational.cpp and main.cpp b. The header file rational.h c. A test file contains some Rational numbers. d. The executable file. 10) You are to do the Assignment by yourself. Any copy will be considered as cheating. 11) Grading: The program will be graded as follow: 10% documentation 50% correctness 20% bugs 20% robustness

C+

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

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

Students also viewed these Databases questions

Question

friendliness and sincerity;

Answered: 1 week ago