qfedu-cpp-level/day6/homework/h3.cpp

41 lines
755 B
C++
Raw Normal View History

2023-08-01 09:07:49 +08:00
// 创建一个基类 Person其中包含一个纯虚函数 display() ,用于显示人的信息。从 Person 派生出 Student 类和 Teacher 类,分别实现不同类型人的信息显示。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Person
{
public:
virtual void display() = 0;
};
class Student : public Person
{
public:
// 子类的 virtual 函数可以不写 virtual 关键字
virtual void display()
{
cout << "Student" << endl;
}
};
class Teacher : public Person
{
public:
virtual void display()
{
cout << "Teacher" << endl;
}
};
int main()
{
Person *p = new Student();
p->display();
p = new Teacher();
p->display();
return 0;
}