Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从用作数据节的字符串派生的类_C++_Inheritance - Fatal编程技术网

C++ 从用作数据节的字符串派生的类

C++ 从用作数据节的字符串派生的类,c++,inheritance,C++,Inheritance,我有一个任务,我必须“创建一个名为Number的类,它是从string派生的。您将使用它作为基类来派生整数和双精度类。”它接着说,整数和双精度类的数据部分将“消失” “因为Number是从string派生的,Integer和Double是从Number派生的,所以它们都变成了字符串。这给了您一个内置的数据部分。”我不明白的是,我的数据部分现在在哪里。我该如何称呼它和改变它 “此时,数字类将只包含以下代码: -将数据节设置为“0”的无参数构造函数 -重载构造函数,它接受字符串并将数据段设置为传递给

我有一个任务,我必须“创建一个名为Number的类,它是从string派生的。您将使用它作为基类来派生整数和双精度类。”它接着说,整数和双精度类的数据部分将“消失”

“因为Number是从string派生的,Integer和Double是从Number派生的,所以它们都变成了字符串。这给了您一个内置的数据部分。”我不明白的是,我的数据部分现在在哪里。我该如何称呼它和改变它

“此时,数字类将只包含以下代码:

-将数据节设置为“0”的无参数构造函数

-重载构造函数,它接受字符串并将数据段设置为传递给它的值。”

//带有两个构造函数的Number类
#ifndef数
#定数
#包括
#包括
使用名称空间std;
类号:公共字符串
{
公众:
Number():字符串(“0”){}
数字(字符串s):字符串{}
};
#恩迪夫
双层

//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
//双类
#ifndef双
#定义双精度
#包括
#包括“Integer.h”
#包括“Number.h”
使用std::string;
第二类:公众号
{
私人:
//我在这里打电话吗?像号码数据,还是双重数据;?
//还是数据不在这里?
void isNan(字符串s);
布尔南=假;
void recursiveNaN(字符串::迭代器p,字符串::迭代器n);
公众:
//建设者
双();
双(双d);
双倍(const-Double&d);
双精度(常数整数&i);
//功能性
void等于(双d);
Double&equals(常数Double&d);
双加(const-Double&d);
双接头(常数双d);
双mul(常数双d);
双分区(连续双分区和双分区);
双toDouble()常数;
//原语
双加(双d);
双接头(双d);
双mul(双d);
双div(双d);
//操作员过载
双运算符+(常数双和d);
双运算符-(const Double&d);
双运算符*(常数双和d);
双运算符/(常数双&d);
双精度和运算符=(常数双精度和d);
布尔运算符==(常数双d);
布尔运算符==(双d);
布尔运算符!=(常数双d);
布尔运算符!=(双d);
//弦乐
双(字符串s);
布尔伊斯南();
void等于(字符串s);
双精度&运算符=(字符串s);
字符串toString();
};
#endif/!双重的
我们的一个提示是关于void-quals(双d)函数。提示是将d转换为字符串,然后调用带有重载字符串的equals参数来分配此->

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");
}
void Double::equals(双d)
{
细流ss;
ss等于(ss);
}
void Double::equals(字符串s)
{
此->isNan(s);
如果(!this->isNan())
此->分配;
其他的
此->分配(“0.0”);
}
在我看来,这就是他的意思,但这给了我一个错误,没有成员函数调用equals。我只需要知道如何调用这个新数据以及如何更改它。我的数字类中也不能有更多的函数

我不明白的是我的数据部分现在在哪里。我该如何称呼它和改变它


你没有。数字是字符串,因为您是从字符串派生的。您还需要哪些其他数据?调用C++的任何成员函数:代码> STD::String < /C>或将类的实例传递给任何函数,使用一个<代码> STD::String (或引用一个)。例如,使用
std::string
时,没有任何
虚拟
函数可以
重写
,因此继承有些毫无意义。使用内部
std::string
成员变量可能更好。谁会从string类派生数字类呢??!是的,我不知道从字符串派生有多实际,但它是赋值所必需的。所有的帮助都很感激“这给了我一个错误,没有调用等于的成员函数”。你为什么不告诉我们你得到的实际错误?这可能是许多事情中的任何一个。
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");
}