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

时间:2016-12-10 03:46:30

标签: c++ class arbitrary-precision

我正在尝试创建一个模仿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;

1 个答案:

答案 0 :(得分:2)

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

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

例如,如果您的号码存储为名为std::vector<int>的{​​{1}}中的数字序列,则构造函数的定义可能如下所示:

m_digits