使用cpp定义一个基类person类有姓名性别年龄在由基类派生出student类和teacher类,学生类增加学号,班级,专业和入学成绩,教师类增加工号,职称和工资
时间: 2023-04-11 08:01:50 浏览: 260
一个 Student类:包括姓名、学号、性别、专业、家庭地址。
可以使用以下代码定义一个基类person类和由基类派生出的student类和teacher类:
```cpp
#include <string>
class person {
public:
person(const std::string& name, const std::string& gender, int age)
: name_(name), gender_(gender), age_(age) {}
std::string name() const { return name_; }
std::string gender() const { return gender_; }
int age() const { return age_; }
private:
std::string name_;
std::string gender_;
int age_;
};
class student : public person {
public:
student(const std::string& name, const std::string& gender, int age,
const std::string& student_id, const std::string& class_name,
const std::string& major, double admission_score)
: person(name, gender, age),
student_id_(student_id),
class_name_(class_name),
major_(major),
admission_score_(admission_score) {}
std::string student_id() const { return student_id_; }
std::string class_name() const { return class_name_; }
std::string major() const { return major_; }
double admission_score() const { return admission_score_; }
private:
std::string student_id_;
std::string class_name_;
std::string major_;
double admission_score_;
};
class teacher : public person {
public:
teacher(const std::string& name, const std::string& gender, int age,
const std::string& teacher_id, const std::string& title, double salary)
: person(name, gender, age),
teacher_id_(teacher_id),
title_(title),
salary_(salary) {}
std::string teacher_id() const { return teacher_id_; }
std::string title() const { return title_; }
double salary() const { return salary_; }
private:
std::string teacher_id_;
std::string title_;
double salary_;
};
```
其中,person类有姓名、性别和年龄三个成员变量,student类在person类的基础上增加了学号、班级、专业和入学成绩四个成员变量,teacher类在person类的基础上增加了工号、职称和工资三个成员变量。
阅读全文