|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include "iostream"
- using std::endl;
- using std::cout;
- using std::cin;
- class Parent
- {
- private:
- int a;
- public:
- Parent(int a = 0)
- {
- }
- public:
- virtual void print()
- {
- cout << "我是父亲\n";
- }
- };
- class Child :public Parent
- {
- private:
- //int b;
- public:
- Child(int b = 0) :Parent(11)
- {
- }
- public:
- virtual void print()
- {
- cout << "我是儿子\n";
- }
- };
- void main()
- {
- Child cArr[] = { Child(11),Child(22),Child(33) };//这cArr是三个匿名对象,cArr[0] cArr[1] cArr[2] 依次等于他们
- Parent* pP = NULL;
- Child* pC = NULL;
- pP = cArr;
- pC = cArr;
- pP->print();//pP = cArr+0; pP++ = cArr+1;pP++ = cArr+2 那pP->print() 是什么意思呢?父类指针pP指向子类对象 然后打印子类成员函数?
- pC->print();
- pP++;
- pC++;
- pP->print();
- pC->print();
- pP++;
- pC++;
- pP->print();
- pC->print();
- system("pause");
- }
复制代码 |
|