C++?е?Virtual Function (?麯??)
???????????? ???????[ 2014/3/21 9:48:10 ] ???????????? C++
????1.C++ Virtual ?÷?
??????????????????????????????????漰?????????????????????????
// VitualFunction.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
//base class
class Animal{
public:
virtual void eat(){
cout << "animal eat" << endl;
}
virtual void die(){
cout << "animal die" << endl;
}
};
class Dog : public Animal{
public:
void eat(){
cout << "dog eat" << endl;
Animal::die();//use base class's function
}
};
class Cat : public Animal{
public:
void eat(){
cout << "cat eat" << endl;
}
};
class Lion : public Animal{
};
int _tmain(int argc?? _TCHAR* argv[])
{
vector<Animal*> someAnimals;
someAnimals.push_back(new Animal());
someAnimals.push_back(new Dog());
someAnimals.push_back(new Cat());
someAnimals.push_back(new Lion());
for(int i = 0; i < someAnimals.size(); ++i){
someAnimals[i]->eat();
}
system("pause");
return 0;
}
??????
???·???
??????????????????
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