求教,谁能给解释下
#include <iostream>using namespace std;
class A
{
public:
virtual int get(){cout<<"a"<<endl;return 2;}
};
class B :public A
{
public:
int get(){cout<<"b"<<endl;return 3;}
};
void main()
{
A *a;
int i;
bool quit=false;
while (1)
{
cout<<"1.父类,2子类,3退出 ";
cin>>i;
switch(i)
{
case 1:a=new A;
break;
case 2:a=new B;
break;
case 3:quit=true;
break;
default:cout<<"请正确输入选项!!";
break;
}
if(quit==true)
{
break;
}
cout<<a->get()<<endl;
delete a;
}
}
输入1、2、3以外的数字是报错!!