#include<iostream> using namespace std; class A { public:int get(){return 1;} }; class B:public A { public:int get(){return 2;} }; int main() { A a; int one; one=a.get(); cout<<"a的值是:"<<one<<endl; B b; one=b.get(); cout<<"b的值是:"<<one<<endl; return 0; }
#include<iostream> using namespace std; class A { public:int get() {return 1;} }; class B: public A { public:int get() {return 2;} }; int main() { while (true) { cout << "(1)父类(2)子类(3)退出:"; int choice; cin >> choice; A *p; bool quit = false; switch (choice) { case 1: p = new A; break; case 2: p=new B; break; case 3: quit=true; break; default: cout<<"请输入1 、2或者3."; break; } if(quit==true){break;} cout<<p->get()<<endl; } return 0; }