Question
Write a C++ to Define a class for fractions, name it Fraction. A fraction is composed of numerator and denominator. In addition to the member
Write a C++ to Define a class for fractions, name it Fraction. A fraction is composed of numerator and denominator. In addition to the member functions that sets and gets the member attributes (numerator and denominator), define following member functions: 1. Constructors a. A constructor uses default parameters.
b. Copy constructor 2. Mutators: setNumerator(), setDenominator()
3. Assessors: getNumerator(), getDenominator()
4. Overload output operators (<<) for Fraction class.
5. Overload input operator (>>) for Fraction class.
6. Overload assignment operator (=) for Fraction class Write a driver program using Fraction class. Every member functions and operators should be called or activated to verify that they are working properly. ** Use UML to design the class
The objective of this lab is to define a class with constructors and overload input/output operators.
- Define and implement Fraction class
- Define and implement constructors of Fraction class
- Overload operators: >>, <<, =
An Example of Program Output:
--------------------------------------------------------------------------------------
=== Test constructor with default arguments ===
f1 = ( 0/1 ) === Test constructor with arguments ===
f2 = ( 2/3 ) === Test copy constructor ===
f3 = ( 2/3 ) === Test >> operator ===
Enter a fraction:
numerator: -5
denominator: 3
f1 = ( -5/3 ) === Test assignment operator (=) === f4 = f1 = ( -5/3 ) Press any key to continue . . .
---------------------------------------------------------------------------------------
Driver program:
//
// lab_fraction.cpp
//
#include
#include
using namespace std;
#include "Fraction.h"
int main()
{
cout << "=== Test constructor with default arguments ===" << endl;
Fraction f1; // constructor with defualt arguments
cout << "f1 = " << f1 << endl; // overload output operator
cout << endl;
cout << "=== Test constructor with arguments ===" << endl;
Fraction f2(2, 3); // constructor
cout << "f2 = " << f2 << endl;
cout << endl;
cout << "=== Test copy constructor ===" << endl;
Fraction f3(f2); // copy constructor
cout << "f3 = " << f3 << endl;
cout << endl;
cout << "=== Test >> operator ===" << endl;
cout << "Enter a fraction: " << endl;
cin >> f1; // input operator
cout << "f1 = " << f1 << endl;
cout << endl;
cout << "=== Test assignment operator (=) ===" << endl;
Fraction f4;
f4 = f1;
cout << "f4 = f1 = " << f4 << endl;
//--------------------------
cout << endl;
system("pause");
return 0;
}
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