C++虚函数、继承等综合使用
工程结构
四个类分别为:Person、Student、HaveClass、Time。
其中:
Person表示一个人的抽象;
Student类是一个学生类,具有自己独有的特征和行为;
HaveClass类是与学生上课有关的,这里实现了一个简单的课程表;
Time类处理时间的,这里只是设置了星期、小时、分钟。
Person是Student的基类。而在Student类中引用了HaveClass类,HaveClass类中引用了Time类。
工程如图所示:
类的设计
1、Person类
头文件person.h如下:
#ifndef PERSON_H_
#define PERSON_H_
#include <iostream>
using namespace std;
class Person{
long id;
string name;
int age;
char gender;
string addr;
bool married;
public:
Person();
Person(long id,string name);
Person(long id,string name,int age,char gender,string addr,bool married);
virtual void Show(); // Show声明为虚函数,表示后面的派生类会使用相同的函数名重写该函数
void GetGeneralTimeTable();
virtual ~Person();
};
#endif /*PERSON_H_*/
Person.cpp如下所示:
#include "person.h"
Person::Person(){
}
Person::Person(long id,string name){
this->id = id;
this->name = name;
}
Person::Person(long id,string name,int age,char gender,string addr,bool married){
this->id = id;
this->name = name;
this->age = age;
this->gender = gender;
this->addr = addr;
this->married = married;
}
void Person::Show(){
cout << "身份:" << id << endl;
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << gender << endl;
cout << "住址:" << addr << endl;
if(married){
cout << "婚否:" << "已婚" << endl;
}
else{
cout << "婚否:" << "未婚" << endl;
}
}
void Person::GetGerneralTimeTable(){
cout << "早上,起床之后,洗漱,吃早餐。" << endl;
cout << "中午,吃午餐。" << endl;
cout << "下午,吃晚餐。" << endl;
cout << "晚上,睡觉。" << endl;
}
Person::~Person(){
cout << "执行Person的析构函数~Person()。" << endl;
}
2、Student类
头文件student.h如下:
#ifndef STUDENT_H_
#define STUDENT_H_
#include "person.h"
#include "haveclass.h"
class Student : public Person {
long sno;
string grade;
string sclass;
string myClass;
string school;
HaveClass* sClass;
public:
Student(long id,string name,long sno) : Person(id,name){
this->sno = sno;
}
Student(long sno,string grade,string myClass,string school,HaveClass sClass[5]);
Student(long id,string name,int age,char gender,string addr,bool married,long sno,string grade,string myClass,string school,HaveClass* sClass) : Person(id,name,age,gender,addr,married){
this->sno = sno;
this->grade = grade;
this->myClass = myClass;
this->school = school;
this->sClass = sClass;
}
void SetHaveClass(HaveClass* sClass);
HaveClass* GetHaveClass();
void Show();
void DispalyCourseTimeTable();
virtual ~Student();
};
#endif /*STUDENT_H_*/
Student.cpp如下所示:
#include "student.h"
Student::Student(long sno,string grade,string myClass,string school,HaveClass* sClass){
this->sno = sno;
this->grade = grade;
this->myClass = myClass;
this->school = school;
this->sClass = sClass;
}
void Student::SetHaveClass(HaveClass* sClass){
this->sClass = sClass;
}
HaveClass* Student::GetHaveClass(){
return this->sClass;
}
void Student::Show(){
cout << "学号:" << sno << endl;
cout << "年级:" << grade << endl;
cout << "班级:" << myClass << endl;
cout << "学校:" << school << endl;
}
void Student::DispalyCourseTimeTable(){
for(int i=0;i<5;i++){
HaveClass* hc = &sClass[i];
string* course = hc->GetCourse();
string weekday = hc->GetFromTime()->GetWeekday();
Time* fromTime = hc->GetFromTime();
Time* toTime = hc->GetToTime();
cout << weekday << " " << fromTime->GetHour() << ":" << fromTime->GetMinute();
cout << " ~ " << toTime->GetHour() << ":" << toTime->GetMinute() << " " << *course << endl;
}
}
Student::~Student(){
cout << "执行Student的析构函数~Student()。" << endl;
}
3、HaveClass类
头文件haveclass.h如下:
#ifndef HAVECLASS_H_
#define HAVECLASS_H_
#include <iostream>
#include "time.h"
using namespace std;
class HaveClass {
Time fromTime;
Time toTime;
string course;
public:
HaveClass();
HaveClass(Time fromTime,Time toTime,string course);
void SetFromTime(Time fromTime);
Time* GetFromTime();
void SetToTime(Time toTime);
Time* GetToTime();
void SetCourse(string course);
string* GetCourse();
};
#endif /*HAVECLASS_H_*/
HaveClass.cpp如下所示:
#include "haveclass.h"
HaveClass::HaveClass(){
}
HaveClass::HaveClass(Time fromTime,Time toTime,string course){
this->fromTime = fromTime;
this->toTime = toTime;
this->course = course;
}
void HaveClass::SetFromTime(Time fromTime){
this->fromTime = fromTime;
}
Time* HaveClass::GetFromTime(){
return &(this->fromTime);
}
void HaveClass::SetToTime(Time toTime){
this->toTime = toTime;
}
Time* HaveClass::GetToTime(){
return &(this->toTime);
}
void HaveClass::SetCourse(string course){
this->course = course;
}
string* HaveClass::GetCourse(){
return &(this->course);
}
4、Time类
头文件time.h如下:
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
using namespace std;
class Time {
string weekday;
int hour;
int minute;
public:
Time();
Time(string weekday,int hour,int minute);
void SetWeekday(string weekday);
string GetWeekday();
void SetHour(int hour);
int GetHour();
void SetMinute(int minute);
int GetMinute();
string GetFormatTime();
};
#endif /*TIME_H_*/
Time.cpp如下所示:
#include "mytime.h"
Time::Time(){
}
Time::Time(string weekday,int hour,int minute){
this->weekday = weekday;
this->hour = hour;
this->minute = minute;
}
void Time::SetWeekday(string weekday){
this->weekday = weekday;
}
string Time::GetWeekday(){
return this->weekday;
}
void Time::SetHour(int hour){
this->hour = hour;
}
int Time::GetHour(){
return this->hour;
}
void Time::SetMinute(int minute){
this->minute = minute;
}
int Time::GetMinute(){
return this->minute;
}
5、测试主函数Main.cpp
#include "person.h"
#include "student.h"
#include "haveclass.h"
#include "time.h"
int main(){
Time t11("星期一",8,30);
Time t12("星期二",8,30);
Time t13("星期三",8,30);
Time t14("星期四",8,30);
Time t15("星期五",8,30);
Time t21("星期一",11,30);
Time t22("星期二",11,30);
Time t23("星期三",11,30);
Time t24("星期四",11,30);
Time t25("星期五",11,30);
HaveClass hc[5];
HaveClass hc0(t11,t21,"形式语言与自动机理论");
HaveClass hc1(t12,t22,"算法分析");
HaveClass hc2(t13,t23,"人工智能");
HaveClass hc3(t14,t24,"数据挖掘");
HaveClass hc4(t15,t25,"搜索引擎");
hc[0] = hc0;
hc[1] = hc1;
hc[2] = hc2;
hc[3] = hc3;
hc[4] = hc4;
Student stu(1000000,"Shirdrn",26,'M',"Changchun",false,2007001,"研一","八班","CUST",hc);
Student* pStu = &stu;
pStu->Person::Show();
cout << endl;
pStu->Show();
cout << endl;
pStu->SetHaveClass(hc);
pStu->DispalyCourseTimeTable();
cout << endl;
pStu->Person::GetGeneralTimeTable();
cout << endl;
return 0;
}
输出如下所示:
身份:1000000
姓名:Shirdrn
年龄:26
性别:M
住址:Changchun
婚否:未婚
学号:2007001
年级:研一
班级:八班
学校:CUST
星期一 8:30 ~ 11:30 形式语言与自动机理论
星期二 8:30 ~ 11:30 算法分析
星期三 8:30 ~ 11:30 人工智能
星期四 8:30 ~ 11:30 数据挖掘
星期五 8:30 ~ 11:30 搜索引擎
早上,起床之后,洗漱,吃早餐。
中午,吃午餐。
下午,吃晚餐。
晚上,睡觉。
执行Student的析构函数~Student()。
执行Person的析构函数~Person()。
后记
通过今天这么一折腾,把C++中函数的传址都学习了一下,感觉指针还是很有趣的,不过有些用起来感觉不是很顺手,不像Java中那么傻瓜式的,只要一点就可以解决问题。
再一个,今天在Linux下,我的Eclipse总是出现这样那样的问题,感觉这个版本不是很稳定啊,有时想用一下IDE自带的提示功能,都需要等待很久的,感觉她的查找速度怎么那么地迟钝呢...这样的代价还不如我就直接飞速地用手敲了