实验内容
编写一个学生和教师数据输入和显示程序。
学生数据有编号、姓名、班级和成绩。
教师数据有编号、姓名、职称和部门。
要求将编号、姓名设计成一个类person,并作为学生数据操作类student和教师类数据操作类teacher的基类。
main.cpp
#include <iostream>
#include <string>
#include "Student.h"
#include "Teacher.h"
using namespace std;
int main()
{
// 定义一个学生对象
Student student(1090280, "张三", 678.0, "数字媒体技术");
// 定义一个教师对象
Teacher teacher(199009, "李四", "教授", "数字媒体技术教研室");
// 显示学生对象的信息
student.display();
// 显示教师对象的信息
teacher.display();
// 修改学生对象的信息
student.setScore(701);
// 显示学生对象的信息
student.display();
// 修改教师对象的信息
teacher.setDepartment("计算机科学技术");
// 显示教师对象的信息
teacher.display();
return 0;
}
Person.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(int id,const char* name);
char* getName();
void setName(const char* name);
void display();
int getId();
void setID(int id);
protected:
int id;
char name[20];
};
Teacher.h
#pragma once
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
class Teacher :public Person
{
public:
Teacher(int id, const char* name, const char* title, const char* dp);
char* getTitle();
void setTitle(const char* title);
char* getDepartment();
void setDepartment(const char* dp);
void display();
private:
char title[20];
char department[30];
};
Student.h
#pragma once
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
class Student :public Person
{
public:
Student(int id, const char* name, double score, const char* GC);
char* getGradeClass();
void setGradeClass(const char* GC);
double getScore();
void setScore(double score);
void display();
private:
char gradeClass[20];
double score;
};
参考答案
Person.cpp
#include "Person.h"
// 注意类字符数组成员的初始化
Person::Person(int id, const char* name) :id(id), name("\0")
{
this->setName(name);
}
char* Person::getName()
{
return this->name;
}
void Person::setName(const char* name)
{
strcpy_s(this->name,name);
}
void Person::display()
{
cout << this->id << " " << this->name << endl;
}
int Person::getId()
{
return this->id;
}
void Person::setID(int id)
{
this->id = id;
}
Teacher.cpp
#include "Teacher.h"
Teacher::Teacher(int id, const char* name, const char* title, const char* dp)
:Person(id, name), title("\0"), department("\0")
{
this->setTitle(title);
this->setDepartment(dp);
}
char* Teacher::getTitle()
{
return this->title;
}
void Teacher::setTitle(const char* title)
{
strcpy_s(this->title, title);
}
char* Teacher::getDepartment()
{
return this->department;
}
void Teacher::setDepartment(const char* dp)
{
strcpy_s(this->department, dp);
}
void Teacher::display()
{
Person::display();
cout << "title: " << this->title << endl;
cout << "department: " << this->department << endl << endl;
}
Student.cpp
#include "Student.h"
Student::Student(int id, const char* name, double score, const char* GC)
:Person(id, name), score(score), gradeClass("\0")
{
this->setGradeClass(GC);
}
char* Student::getGradeClass()
{
return this->gradeClass;
}
void Student::setGradeClass(const char* GC)
{
strcpy_s(this->gradeClass, GC);
}
double Student::getScore()
{
return this->score;
}
void Student::setScore(double score)
{
this->score = score;
}
void Student::display()
{
Person::display();
cout << "gradeClass: " << this->gradeClass << endl;
cout << "score: " << this->score << endl << endl;
}