======================Person.h=======================
#pragma once
#define NAMELEN 20 //定义姓名的字节长度
#define SEXLEN 10 //定义性别的字节长度
#include <string>
#include <iostream>
using namespace std;
class Person{
private:
int age;
char name[NAMELEN],sex[SEXLEN];
public:
Person();
Person(char*,int,char*);
void inputName(char *);
void inputAge(int);
void inputSex(char *);
void display();
};
Person::Person(){
strcpy(name,"NULL");
age=0;
strcpy(sex,"NULL");
}
Person::Person(char *name,int age,char *sex){
strcpy(this->name,name);
this->age=age;
strcpy(this->sex,sex);
}
void Person::inputName(char *name){
strcpy(this->name,name);
}
void Person::inputAge(int age){
this->age=age;
}
void Person::inputSex(char *sex){
strcpy(this->sex,sex);
}
void Person::display(){
cout<<"Name:"<<name<<endl<<"Age:"<<age<<endl<<"Sex:"<<sex<<endl;
}
======================Teacher.h=======================
#include "Person.h"
#define TITLELEN 20 //定义职称字节长度
#define DEPTLEN 20 //定义系别字节长度
class Teacher:public Person{
private:
long number;
char title[TITLELEN],DEPT[DEPTLEN];
public:
Teacher();
Teacher(char *,int,char *,long,char *,char *);
void inputNumber(long);
void inputTitle(char *);
void inputDEPT(char *);
void display();
};
Teacher::Teacher(){
number=0;
strcpy(title,"NULL");
strcpy(DEPT,"NULL");
}
Teacher::Teacher(char *name,int age,char *sex,long number,char *title,char *DEPT):Person(name,age,sex){
this->number=number;
strcpy(this->title,title);
strcpy(this->DEPT,DEPT);
}
void Teacher::inputNumber(long number){
this->number=number;
}
void Teacher::inputTitle(char *title){
strcpy(this->title,title);
}
void Teacher::inputDEPT(char *DEPT){
strcpy(this->DEPT,DEPT);
}
void Teacher::display(){
Person::display();
cout<<"TeacherNumber:"<<number<<endl<<"Title:"<<title<<endl<<"Department:"<<DEPT<<endl;
}
======================Student.h=======================
#include "Person.h"
#define CLASSLEN 10 //定义班别字节长度
class Student:public Person{
private:
long number;
char Class[CLASSLEN];
float ChnSco,MathSco,EngSco;
public:
Student();
Student(char *,int,char*,long,char *,float,float,float);
void inputNumber(long);
void inputClass(char *);
void inputChnSco(float);
void inputMathSco(float);
void inputEngSco(float);
void display();
};
Student::Student(){
number=0;
strcpy(Class,"NULL");
ChnSco=MathSco=EngSco=0;
}
Student::Student(char *name,int age,char *sex,long number,char *Class,float ChnSco,float MathSco,float EngSco):Person(name,age,sex){
this->number=number;
strcpy(this->Class,Class);
this->ChnSco=ChnSco;
this->MathSco=MathSco;
this->EngSco=EngSco;
}
void Student::inputNumber(long number){
this->number=number;
}
void Student::inputClass(char *Class){
strcpy(this->Class,Class);
}
void Student::inputChnSco(float ChnSco){
this->ChnSco=ChnSco;
}
void Student::inputMathSco(float MathSco){
this->MathSco=MathSco;
}
void Student::inputEngSco(float EngSco){
this->EngSco=EngSco;
}
void Student::display(){
Person::display();
cout<<"StudentNumber:"<<number<<endl<<"Class:"<<Class<<endl<<"ChineseScore:"<<ChnSco<<endl<<"MathScore:"<<MathSco<<endl<<"EnglishScore:"<<EngSco<<endl;
}
======================main.cpp=======================
#include "Student.h"
#include "Teacher.h"
#define TEMPLEN 20 //定义临时string长度
void main(){
char tempchar[TEMPLEN]; //临时string
int tempint; //临时int
long templong; //临时long int
float tempfloat; //临时float
Person P1;
cout<<"==================For Person1====================="<<endl;
P1.display();
cout<<"Update the name:";
cin>>tempchar;
P1.inputName(tempchar);
cout<<"Update the age:";
cin>>tempint;
P1.inputAge(tempint);
cout<<"Update the sex:";
cin>>tempchar;
P1.inputSex(tempchar);
P1.display();
Person P2("张三",20,"男");
cout<<"==================For Person2====================="<<endl;
P2.display();
Teacher T1;
cout<<"=================For Teacher1====================="<<endl;
T1.display();
cout<<"Update the name:";
cin>>tempchar;
T1.inputName(tempchar);
cout<<"Update the age:";
cin>>tempint;
T1.inputAge(tempint);
cout<<"Update the sex:";
cin>>tempchar;
T1.inputSex(tempchar);
cout<<"Update the TeacherNumber:";
cin>>templong;
T1.inputNumber(templong);
cout<<"Update the title:";
cin>>tempchar;
T1.inputTitle(tempchar);
cout<<"Update the Department:";
cin>>tempchar;
T1.inputDEPT(tempchar);
T1.display();
Teacher T2("李四",30,"女",2013090101,"副教授","计算机系");
cout<<"=================For Teacher2====================="<<endl;
T2.display();
Student S1;
cout<<"=================For Student1====================="<<endl;
S1.display();
cout<<"Update the name:";
cin>>tempchar;
S1.inputName(tempchar);
cout<<"Update the age:";
cin>>tempint;
S1.inputAge(tempint);
cout<<"Update the sex:";
cin>>tempchar;
S1.inputSex(tempchar);
cout<<"Update the StudentNumber:";
cin>>templong;
S1.inputNumber(templong);
cout<<"Update the Class:";
cin>>tempchar;
S1.inputClass(tempchar);
cout<<"Update the ChineseScore:";
cin>>tempfloat;
S1.inputChnSco(tempfloat);
cout<<"Update the MathScore:";
cin>>tempfloat;
S1.inputMathSco(tempfloat);
cout<<"Update the EnglishScore:";
cin>>tempfloat;
S1.inputEngSco(tempfloat);
S1.display();
Student S2("王五",18,"男",2111111111,"计算机1班",90,80,70);
cout<<"=================For Student2====================="<<endl;
S2.display();
}