除了“Car”之外,还有一种方法可以根据类的不同设置每个对象的名称变量,例如“Turbo 01”或“Tank 02”或“Buggy 03”,其中id包含创建的车辆数量。不同小类的构造函数中的个性化名称
#include <iostream>
#include <string>
#include <sstream>
static int id = 0; //Total Number of cars right now
class Car
{
private:
std::string name;
Car()
{
std::ostringstream tmp;
std::string temp;
tmp << "Car" << ++id;
temp = tmp.str();
}
Car(std::string name){this->name=name; id++;}
};
class Turbo : public Car()
{
Turbo():Car()
{
}
Turbo(std::string name):Car(name);
{
}
};
在C++中构造函数是特殊的成员函数,没有名称,并且不能被显式调用。 – 2014-11-08 17:15:06
'std :: array type;'这是否实际编译? – 2014-11-08 17:21:44
当您调用基础构造函数时,请将其添加为:例如Turbo(std :: string name):Car(名字,“Turbo”){..}但是std :: array不正确,数组是模板,需要实例化 – 2014-11-08 17:37:16