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

41 lines
755 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 创建一个基类 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;
}