"文字列から派生したNumberというクラスを作成する必要があります。これを基本クラスとして使用してIntegerクラスとDoubleクラスを派生させます。 IntegerクラスとDoubleクラスのデータセクションは「なくなる」と言います。データセクションとして使用される文字列から派生したクラス
"Numberは文字列とIntegerから派生し、DoubleはNumberから派生しているため、すべてが文字列になります。これにより、組み込みのデータセクションが得られます。私が理解できないことは、私のデータセクションが今どこにあるのかです。どのように私はそれを呼び出して変更するのですか?
「この時点で、あなたのNumberクラスは、次のコードが含まれます。
を-Aにデータセクションを設定し、引数なしのコンストラクタません 『の文字列とセットになります0』
-Anオーバーロードされたコンストラクタデータセクションは値に渡されます。私たちのヒントの
//Number class with the two constructors
#ifndef NUMBER
#define NUMBER
#include <iostream>
#include <string>
using namespace std;
class Number: public string
{
public:
Number() : string("0") { }
Number(string s) : string(s) { }
};
#endif
Doubleクラス
//Double class
#ifndef DOUBLE
#define DOUBLE
#include <string>
#include "Integer.h"
#include "Number.h"
using std::string;
class Double: public Number
{
private:
// do i call Number here? like Number data; or double data;?
// or is the data not here?
void isNan(string s);
bool nan = false;
void recursiveNaN(string::iterator p, string::iterator n);
public:
// Constructors
Double();
Double(double d);
Double(const Double &d);
Double(const Integer &i);
// Functionality
void equals(double d);
Double &equals(const Double &d);
Double add(const Double &d);
Double sub(const Double &d);
Double mul(const Double &d);
Double div(const Double &d);
double toDouble() const;
//Primitives
Double add(double d);
Double sub(double d);
Double mul(double d);
Double div(double d);
// Operator Overload
Double operator + (const Double &d);
Double operator - (const Double &d);
Double operator * (const Double &d);
Double operator/(const Double &d);
Double &operator = (const Double &d);
bool operator == (const Double &d);
bool operator == (double d);
bool operator != (const Double &d);
bool operator != (double d);
//String stuff
Double(string s);
bool isNan();
void equals(string s);
Double &operator = (string s);
string toString();
};
#endif // !DOUBLE
一つボイドQUALS(ダブルD)機能にありました。ヒントはdを文字列に変換し、オーバーロードされた文字列引数でequalsを呼び出してthis->を代入することでした。私の心の中で
void Double::equals(double d)
{
stringstream ss;
ss << d;
this->equals(ss);
}
void Double::equals(string s)
{
this->isNan(s);
if (!this->isNan())
this->assign(s);
else
this->assign("0.0");
}
これは彼が何を意味だろうが、これはちょうどこの新しいデータを呼び出す方法と、それを変更する方法を知っておく必要がありequals.Iに呼び出して何のメンバ関数がないことを私とエラーになります。私のNumberクラスはそれの中でもはや機能を持つことができません。
一般的に、C++の標準ライブラリクラスは、継承にはあまり適していないことに注意してください。 'std :: string'では' override'できる 'virtual'関数がないので、継承はやや意味がありません。おそらく内部の 'std :: string'メンバ変数を使う方が良いでしょう。 –
誰が文字列クラスから数値クラスを派生させ、??で始めることさえあろうか? – zett42
さて、私は文字列からどのように派生しているか分かりませんが、それは割り当てに必要です。 – dnun99