C++分数类设计题
Define and implement (that is, create Fraction.h and Fraction.cpp files) for a class called Fraction. The class represents a fraction in math (i.e., an integer over an integer) that has the following features and operations:
The class's attributes should consist of two int values: numerator and denominator.
There should be the following constructors:A parameterless constructor that initializes the fraction to 0. That is, 0/1.
A constructor that takes a single int value, x. The fraction should be initialized to be a fraction equivalent of the int value. That is, x/1.
A constructor that takes two int values, x and y. The fraction should be initialized to be a fraction equivalent of the int value. That is, x/y.
A copy constructor.
The following operations should be defined:toDouble() : returns a double representation of the fraction.
toString() : returns a string representation of the fraction.
getNumerator() : returns the numerator
getDenominator() : returns the denominator
The following operators should be overridden:
+ : adds two fractions in the typical way
- : subtracts fractions in the typical way
* : multiplies fractions in the typical way
/ : divides fractions in the typical wayThe user should assume the responsibility of not dividing by zero. We'll learn how to deal with this later.
The stream operators << and >> should be implemented (as friend functions).
You must also provide a test program (main.cpp) that exercises the features of the class and ensures they work correctly.
源码传送门
传送门:https://pan.baidu.com/s/1JJs9vbZahUCB6cQvXLgAVg?pwd=1111
标签:分数,fractions,int,C++,should,returns,fraction,constructor,设计 From: https://www.cnblogs.com/codewriter/p/16710115.html