41 lines
755 B
C++
41 lines
755 B
C++
// 创建一个基类 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;
|
||
}
|