在 C++ 中创建和实现 类
Creating and Implementing Classes in C++
在我的计算机科学 class 中,我们正在学习如何使用 classes 来创建虚拟电子元件...我们需要创建五个东西; main() 以及 ElectricalComponent.h/.cpp、Resistor.h/.cpp、Capacitor.h/.cpp 和 Battery.h/.cpp。
我已经创建了 Electrical 组件 Class,并且我知道如何创建和修复 main 函数中的任何错误,但是我在构建 Resistor Class.
#include <iostream>
#include "ElectronicComponent.h"
class Resistor :
public ElectronicComponent
{
public:
Resistor(double);
virtual ~Resistor();
virtual double getValue() const = 0;
virtual std::string getUnits() const = 0;
virtual std::string to_string() const = 0;
};
Resistor::Resistor(double v)
{
value = v;
}
std::string Resistor::to_string() const
{
return "Resistor value (" + std::to_string(value) + " " + units + ")";
}
我收到命名空间 std 没有成员 to_string 的错误,并且我的变量值和单位未声明。我理解未声明变量的含义,但不理解 to_string 错误。
这是我得到的有关此问题的所有信息 Class:
The Resistor class will be implemented with files Resistor.h and
Resistor.cpp. The Resistor class will inherit, publicly, from
ElectronicComponent. You need to implement a destructor and the three
virtual functions getValue, getUnits and to_string. You will also need
a constructor that takes one input parameter, the value of the
resistor. The value is of type double. The getValue function needs to
return the value as a double. The getUnits function needs to return
“Ohm(s)” as a std::string.
classes 的其余部分应该以相同的方式构建,因此了解它们的工作原理应该对我有很大帮助。谢谢
在您的页眉中使用:
#include<string>
派生的 Resistor class 中的函数不应指定为纯虚函数(由“=0”表示)。纯虚函数是基础 class 中没有基础 class 实现的函数。此外,大概您的 getValue 和 getUnits 函数在您的 ElectronicComponent 基础 class 中不是纯虚拟的,但实际上在那里实现为:
double ElectronicComponent::getValue() const {
return value;
}
std::string ElectronicComponent::getUnits() const {
return units;
}
在这种情况下,您在派生的 classes 中不需要它们。因此,您派生的电阻器 class 应该类似于:
#include <string>
#include "ElectronicComponent.h"
class Resistor : public ElectronicComponent
{
public:
Resistor(double);
virtual ~Resistor();
virtual std::string to_string() const;
};
Resistor::Resistor(double v) : units("Ohm(s)")
{
value = v;
}
std::string Resistor::to_string() const
{
return "Resistor value (" + std::to_string(value) + " " + units + ")";
}
在我的计算机科学 class 中,我们正在学习如何使用 classes 来创建虚拟电子元件...我们需要创建五个东西; main() 以及 ElectricalComponent.h/.cpp、Resistor.h/.cpp、Capacitor.h/.cpp 和 Battery.h/.cpp。
我已经创建了 Electrical 组件 Class,并且我知道如何创建和修复 main 函数中的任何错误,但是我在构建 Resistor Class.
#include <iostream>
#include "ElectronicComponent.h"
class Resistor :
public ElectronicComponent
{
public:
Resistor(double);
virtual ~Resistor();
virtual double getValue() const = 0;
virtual std::string getUnits() const = 0;
virtual std::string to_string() const = 0;
};
Resistor::Resistor(double v)
{
value = v;
}
std::string Resistor::to_string() const
{
return "Resistor value (" + std::to_string(value) + " " + units + ")";
}
我收到命名空间 std 没有成员 to_string 的错误,并且我的变量值和单位未声明。我理解未声明变量的含义,但不理解 to_string 错误。
这是我得到的有关此问题的所有信息 Class:
The Resistor class will be implemented with files Resistor.h and Resistor.cpp. The Resistor class will inherit, publicly, from ElectronicComponent. You need to implement a destructor and the three virtual functions getValue, getUnits and to_string. You will also need a constructor that takes one input parameter, the value of the resistor. The value is of type double. The getValue function needs to return the value as a double. The getUnits function needs to return “Ohm(s)” as a std::string.
classes 的其余部分应该以相同的方式构建,因此了解它们的工作原理应该对我有很大帮助。谢谢
在您的页眉中使用:
#include<string>
派生的 Resistor class 中的函数不应指定为纯虚函数(由“=0”表示)。纯虚函数是基础 class 中没有基础 class 实现的函数。此外,大概您的 getValue 和 getUnits 函数在您的 ElectronicComponent 基础 class 中不是纯虚拟的,但实际上在那里实现为:
double ElectronicComponent::getValue() const {
return value;
}
std::string ElectronicComponent::getUnits() const {
return units;
}
在这种情况下,您在派生的 classes 中不需要它们。因此,您派生的电阻器 class 应该类似于:
#include <string>
#include "ElectronicComponent.h"
class Resistor : public ElectronicComponent
{
public:
Resistor(double);
virtual ~Resistor();
virtual std::string to_string() const;
};
Resistor::Resistor(double v) : units("Ohm(s)")
{
value = v;
}
std::string Resistor::to_string() const
{
return "Resistor value (" + std::to_string(value) + " " + units + ")";
}