C++???????
???????????? ???????[ 2015/2/28 13:29:23 ] ????????C++ ??????? ???
????C++???????????????????????????????????????????????????
????????????????????????????????????У????????????????????
??????????£?
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
class Parent{
protected:
string pname;
public:
Parent(string name){
pname=name;
}
virtual void printName(){};
};
class Child: public Parent{
protected:
string cname;
public:
Child(string name):Parent(name){
cname=name;
}
virtual void printName(){
cout<<"This is child?? cname is "<<cname<<"?? pname is "<<pname<<endl;
}
};
class GrandChild: public Child{
private:
string gname;
public:
GrandChild(string name):Child(name){
gname=name;
}
virtual void printName(){
cout<<"This is grandchild?? gname is "<<gname<<"?? cname is "<<cname<<"?? pname is "<<pname<<endl;
}
};
int main(){
string name="C";
Child child(name);
name="GC";
GrandChild gchild(name);
vector<Parent*> mlist;
mlist.push_back(dynamic_cast<Parent*>(&child));
mlist.push_back(dynamic_cast<Parent*>(&gchild));
for(int i=0;i<mlist.size();++i){
mlist[i]->printName();
}
}
????????????????????printName???????virtual?????????????????????……
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11