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自帶的提示功能,都需要等待很久的,感覺她的查找速度怎麼那麼地遲鈍呢...這樣的代價還不如我就直接飛速地用手敲了