tuz*_*zer 7 c++ inheritance class
例如,在main函数中,我想获取用户的输入.根据输入,我将创建a Rectangle或a Circle,它们是子类Object.如果没有输入(或未知),那么我将只创建一个通用对象.
class Object
{
public:
Object();
void Draw();
private:
....
};
class Rectangle:public Object
{
public:
Rectangle();
.... //it might have some additional functions
private:
....
};
class Circle:public Object
{
public:
Circle();
.... //it might have some additional functions
private:
....
};
Run Code Online (Sandbox Code Playgroud)
主功能:
string objType;
getline(cin, objType);
if (!objType.compare("Rectangle"))
Rectangle obj;
else if (!objType.compare("Circle"))
Circle obj;
else
Object obj;
obj.Draw();
Run Code Online (Sandbox Code Playgroud)
当然,上面的代码不起作用,因为我不能在If语句中实例化一个对象.所以我试过这样的事情.
Object obj;
if (!objType.compare("Rectangle"))
obj = Rectangle();
else if (!objType.compare("Circle"))
obj = Circle();
obj.Draw();
Run Code Online (Sandbox Code Playgroud)
这段代码会编译,但它不会做我想要的.由于某种原因,对象不是以子类的方式启动的(例如,我在子类中设置了一些Object的成员变量,特别是向量,不同).但是,当我在Child类构造函数中放置一个断点时,它确实在那里运行.
那么我应该如何在一些if语句中将实例化对象作为其子类?
Set*_*gie 10
您可以在if语句中创建自动对象,但它们将在创建它们的范围的末尾销毁,因此它们不适用于此问题.
你无法做到这obj = Rectangle()一点的原因是因为切片.
你必须有一个指针Object.指向基础对象的指针也可以指向子对象的实例.然后你可以在ifwith中动态创建对象new(使用new忽略范围创建的对象,只有在调用delete指向它们的指针时才会销毁它们),然后delete在你完成时创建它:
Object* obj = NULL; // obj doesn't point to anything yet
string objType;
getline(cin, objType);
if (objType == "Rectangle")
obj = new Rectangle; // make obj point to a dynamic Rectangle
else if (objType == "Circle")
obj = new Circle; // make obj point to a dynamic Circle
else
obj = new Object; // make obj point to a dynamic Object
obj->Draw(); // draw whatever shape obj really points to
delete obj; // deallocate dynamic object
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用智能指针,然后您不必担心手动释放对象:
std::unique_ptr<Object> obj(NULL); // obj doesn't point to anything yet
string objType;
getline(cin, objType);
if (objType == "Rectangle")
obj.reset(new Rectangle); // make obj point to a dynamic Rectangle
else if (objType == "Circle")
obj.reset(new Circle); // make obj point to a dynamic Circle
else
obj.reset(new Object); // make obj point to a dynamic Object
obj->Draw(); // draw whatever shape obj really points to
// the unique_ptr takes care of delete'ing the object for us
// when it goes out of scope
Run Code Online (Sandbox Code Playgroud)