#include <iostream>
using namespace std;
//抽象类
class Animal
{
private:
char sex;
public:
virtual void speak() = 0; //纯虚函数
virtual void eat() = 0;
};
class Person:public Animal
{
public:
virtual void speak()
{
cout << "I am Person!" << endl;
}
virtual void eat()
{
cout << "I can eat by chopsticks!" <<endl;
}
};
class Dog:public Animal
{
public:
virtual void speak();
virtual void eat();
};
void Dog::speak()
{
cout << "I am Dog!" << endl;
}
void Dog::eat()
{
cout << "I can eat by hand!" << endl;
}
int main()
{
Person p1;
p1.speak();
p1.eat();
Dog d1;
d1.speak();
d1.eat();
Animal* a = NULL;
a = &p1;
a->speak();
return 0;
}
纯虚函数
最新推荐文章于 2023-09-07 16:34:49 发布