创建自定义数据类型 C++

Creating custom data type c++

我正在尝试创建一个模仿 arbitrary precision 数据类型的 Number class。

我希望能够执行以下操作:

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);
};

我如何才能将数字 Class 设置为特定值?

Number foo = 5;

对于该行,您需要一个构造函数。这是一个例子:

class Number {
public:
    Number(int I = 0);
...
};

例如,如果您的数字作为数字序列存储在名为 m_digitsstd::vector<int> 中,那么构造函数的定义可能如下所示:

Number::Number(int I) : m_digits(1, I)
{
}