c++关于类和对象创建的问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 18:34:35
求助
想编一个程序
建立2个类 tri rect
希望在main函数中 用switch
case 1时建立一个名字叫 tri1的对象
case 2时建立一个名字叫 rect1的对象
case 3时退出
如果不3就继续建立
第2个 tri类的对象叫tri2 第3个叫 tri3
.....
请问怎么编这个程序
我做了一个晚上
没做出来
新手
希望大家帮助
谢谢

#include <stdio.h>

//基类:图形类
class Shape
{
public:
const char* GetName() const
{
return m_Name;
}
protected:
char m_Name[16];
};

//三角形类,从图形类派生
class tri : public Shape
{
public:
tri()
{
sprintf(m_Name, "tri%d", m_sIndex++);
}
private:
static int m_sIndex;
};

int tri::m_sIndex = 1;

//矩形类,从图形类派生
class rect : public Shape
{
public:
rect()
{
sprintf(m_Name, "rect%d", m_sIndex++);
}
private:
static int m_sIndex;
};

int rect::m_sIndex = 1;

int main(int argc, char* argv[])
{
Shape *pShape = NULL;
bool bContinue = true;
while (bContinue)
{
int nSelect = 0;
printf("Please select:");
scanf("%d", &nSelect);
switch(nSelect)
{
case 1:
pShape =