c++ - 从用作数据部分的字符串派生的类

标签 c++ inheritance

我有一个任务,我必须“创建一个名为 Number 的类,该类派生自字符串。您将使用它作为基类来派生 Integer 和 Double 类。”它接着说 Integer 和 Double 类的数据部分将“消失”。

“因为 Number 派生自字符串,而 Integer 和 Double 派生自 Number,它们都变成了字符串。这为您提供了一个内置数据部分。”我不明白的是我的数据部分现在在哪里。怎么调用和修改。

“此时您的 Number 类将只包含以下代码:

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

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

//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 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

我们的一个提示是关于 void quals(double 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 的成员函数。我只需要知道如何调用这个新数据以及如何更改它。我的 Number 类也不能再有函数了。

最佳答案

What I don't understand is where my data section is now. How do I call it and change it.

你不知道。数字是字符串,因为您是从字符串派生的。你想要什么额外的数据?您可以通过调用 std::string 的任何成员函数或将您的类的实例传递给任何采用 std::string 的函数(或对一个函数的引用)来调用它).

关于c++ - 从用作数据部分的字符串派生的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43951306/

相关文章:

java - 创建第二个实例时抛出异常

java - 何时使用组合,何时使用继承

c++ - 带有 std::ate 的 std::ofstream 未在末尾打开

c++ - is_error_code_enum<> 枚举必须仅在全局命名空间中定义?

java - 从 Java 程序分析 C++ 文件

c++ - 设置 C++ 中内置 exe 的 Windows 文件详细信息?

inheritance - 如何在 Flutter 中重用非 Widget 类?

c++ - 使用继承拆分模板代码是正确的方法吗?

oop - 为什么 OOP 中的类有一个选项可以标记为从不继承自祖先?

.net - 关于.NET 中接口(interface)继承的问题