main.h
#include<string>
#include<iostream>
using namespace std;
#include "Person.h"
#include "Student.h"
#include "Teacher.h"
int main(){
//Person类
Person p1=Person("包子",21);
p1.print();
//学生类
Student s1=Student("包子",21,"计算机",60);
s1.print();
//教师类
Teacher t1=Teacher("包子",21,"计算机",6000);
t1.print();
return 0;
}
Person.h
#ifndef PERSON_H
#define PERSON_H 0
#include<string>
#include<iostream>
using namespace std;
//基类 Person
class Person{
protected:
string name;
int num;
public:
Person(string name,int num);
string getName();
int getNum();
void print();
~Person(){};
};
#endif
Student.h
#ifndef STUDENT_H
#define STUDENT_H 0
#include "Person.h"
//学生类 Student
class Student:private Person{
protected:
string zhuanye;
int ave;
public:
Student(string name,int num,string zhuanye,int ave):Person(name,num){
this->zhuanye=zhuanye;
this->ave=ave;
}
void print();
~Student(){};
};
#endif
Teacher.h
#ifndef TEACHER_H
#define TEACHER_H
#include "Person.h"
//教师类 Teacher
class Teacher:private Person{
protected:
string zhicheng;
int salary;
public:
Teacher(string name,int num,string zhicheng,int salary):Person(name,num){
this->zhicheng=zhicheng;
this->salary=salary;
}
void print();
~Teacher(){};
};
#endif
P_Function.cpp
#include "Person.h"
#include "main.h"
//构造函数
Person::Person(string name,int num){
this->name=name;
this->num=num;
};
//返回name
string Person::getName(){
return this->name;
}
//返回num
int Person::getNum(){
return this->num;
}
//输出
void Person::print(){
cout<<"姓名为:"<<name<<" "<<"编号为:"<<num<<endl;
}
S_Function.cpp
#include "Student.h"
#include "Person.h"
#include <iostream>
using namespace std;
void Student::print(){
cout<<"姓名为:"<<this->getName()<<" "<<"编号为:"<<this->getNum()<<" "<<"专业为:"<<zhuanye<<" "<<"平均成绩为:"<<ave<<endl;
}
T_Function.cpp
#include "Person.h"
#include "Teacher.h"
void Teacher::print(){
cout<<"姓名为:"<<this->getName()<<" "<<"编号为:"<<this->getNum()<<" "<<"职称为:"<<zhicheng<<" "<<"工资为:"<<salary<<endl;
};