继承的案例
#include <iostream>
using namespace std;
class Person{
private:
string name;
int id;
public:
Person(const string& name, int id);
const string& getName() const {
return this->name; }
int getId() const {
return this->id; }
void setName(const string& name) {
this->name = name; }
void setId(int id) {
this->id = id; }
virtual void display() const{
}
virtual void input() {
}
};
class Student : public Person{
private:
int score;
public:
Student(const string& name, int id, int score);
void display() const;
void input();
};
class Teacher : public Person{
private:
int teacherYear;
public:
Teacher(const string& name, int id, int teacherYear);
void display() const;
void input();
};
Person::Person(const string& name, int id)
:name(name),
id(id)
{
}
Student::Student(const string& name, int id, int score)
:Person(name, id),
score(score)
{
}
Teacher::Teacher(const string& name, int id, int teacherYear)
:Person(name, id),
teacherYear(teacherYear)
{
}
void Student::display() const
{
cout << "这是学生:";
cout << this->getId() << "\t" << this->getName()
<< "\t" << this->score << endl;
}
void Student::input()
{
string name;
int id = 0;
cin >> name >> id >>this->score;
this->setName(name);
this->setId(id);
}
void Teacher::display() const
{
cout << "这是老师:";
cout << this->getId() << "\t" << this->getName()
<< "\t" << this->teacherYear << endl;
}
void Teacher::input()
{
string name;
int id = 0;
cin >> name >> id >> teacherYear;
this->setName(name);
this->setId(id);
}
int main()
{
Student tom(&