我正在尝试创建一个模仿Number
数据类型的arbitrary precision
类。
我希望能够执行以下操作:
Number a, b;
cin >> a >> b;
cout << "The sum of " << a << " and " << b << " is "
<< a+b << endl;
目前我有:
class Number {
public:
Number & operator = (const Number & N);
bool operator == (const Number & N) const;
bool operator != (const Number & N) const;
bool operator < (const Number & N) const;
bool operator > (const Number & N) const;
bool operator <= (const Number & N) const;
bool operator >= (const Number & N) const;
Number operator += (const Number & N);
Number operator + (const Number & N) const;
Number operator *= (const Number & N);
Number operator * (const Number & N) const;
friend ostream & operator << (ostream & output, const Number & N);
friend istream & operator >> (istream & input, Number & N);
};
我如何将Number Class设置为特定值?
Number foo = 5;
答案 0 :(得分:2)
对于那一行,你需要一个构造函数。这是一个例子:
class Number {
public:
Number(int I = 0);
...
};
例如,如果您的号码存储为名为std::vector<int>
的{{1}}中的数字序列,则构造函数的定义可能如下所示:
m_digits