六百里加急:为什么在基类的get()函数前加上virual后,继承以后,在主函数中输出不是动态联编,输出的都是1呢还是,求教!谢谢啊
#include<iostream>using namespace std;
class A
{
public:
virtual int get(){return 1;} //难道不对吗这里?
};
class B:public A
{
public:
int get(){return 2;}
};
int main()
{
while(1)
{
cout<<"1:父类2:子类3:退出:"<<endl;
int choice;
cin>>choice;
A*p;
bool quit =false;
switch(1)
{
case 1:p=new A;
break;
case 2:p=new B;
break;
case 3:quit=true;
break;
default:
cout<<"请输入1,2,3"<<endl;
break;
}
if(quit==true)
{
break;
}
cout<<p->get()<<endl;
}
system("pause");
return 0;
}