#include<iostream>
using namespace std;
class Fraction
{
public:
Fraction(int top, int bottom) :_top(top), _bottom(bottom)
{
_value = static_cast<double>(_top) / _bottom;//显式将int转换为double类型
}
int getTop() { return _top; }
int getBottom() { return _bottom; }
double getValue() { return _value; }
private:
int _top;
int _bottom;
double _value;//保存分数值,避免每次使用时重新计算,以空间换时间
};
int main()
{
Fraction f(7, 4);
cout << f.getTop() << " " << f.getBottom() << " " << f.getValue() << endl;
system("pause");
return 0;
}
构建C++ 简单的分数类
猜你喜欢
转载自blog.csdn.net/weixin_42333471/article/details/88084802
今日推荐
周排行