2015級C++第10、11周補充實踐專案 繼承和派生
【專案0 - 是春哥啊】參考解答
請在下面程式的橫線處填上適當內容,以使程式完整,並使程式的輸出為:
Name: 春哥
Grade: 19
#include <iostream>
#include <cstring>
using namespace std;
class Person{
public:
Person(char* s){
strcpy(name,s);
}
void display( ){
cout<<"Name: "<<name<<endl;
}
private :
char name [20];
};
class Student: ___________//(1)
{
public:
Student(char* s, int g):__________ // (2)
{grade=g;}
void display1( ) {
_________; // (3)
cout<<"Grade: "<<grade<<endl;
}
private:
int grade;
};
int main( )
{
Student s("春哥",19);
___________; // (4)
return 0;
}
【專案2 - 職員有薪水了】參考解答
(1)定義一個名為CPerson的類,有以下私有成員:姓名、身份證號、性別和年齡,成員函式:建構函式、解構函式、輸出資訊的函式。並在此基礎上派生出CEmployee類,派生類CEmployee增加了兩個新的資料成員,分別用於表示部門和薪水。要求派生類CEmployee的建構函式顯示呼叫基類CPerson的建構函式,併為派生類CEmployee定義解構函式,定義輸出資訊的函式。
class CPerson
{
protected:
string m_szName;
string m_szId;
int m_nSex;//0:women,1:man
int m_nAge;
public:
CPerson(string name,string id,int sex,int age);
void Show1();
~CPerson();
};
class CEmployee:public CPerson
{
private:
string m_szDepartment;
double m_Salary;
public:
CEmployee(string name,string id,int sex,int age,string department,double salary);
void Show2();
~CEmployee();
};
int main()
{
string name,id,department;
int sex,age;
double salary;
cout<<"input employee's name,id,sex(0:women,1:man),age,department,salary:\n";
cin>>name>>id>>sex>>age>>department>>salary;
CEmployee employee1(name,id,sex,age,department,salary);
employee1.Show2();
return 0;
}
下面的執行結果供參考:
(2)字串除了用C++擴充的string型別外,按C語言的傳統,還可以用char 表示。請將類宣告中的string全部改為char 後,重新寫一遍程式(此時的區別是,類中有指標成員,構造和解構函式需要考慮深複製的問題了。)
class CPerson
{
protected:
char *m_szName;
char *m_szId;
int m_nSex;//0:women,1:man
int m_nAge;
public:
CPerson(char *name,char *id,int sex,int age);
void Show1();
~CPerson();
};
class CEmployee:public CPerson
{
private:
char *m_szDepartment;
float m_Salary;
public:
CEmployee(char *name,char *id,int sex,int age,char *department,float salary);
void Show2();
~CEmployee();
};
int main()
{
char name[10],id[19],department[10];
int sex,age;
float salary;
cout<<"input employee's name,id,sex(0:women,1:man),age,department,salary:\n";
cin>>name>>id>>sex>>age>>department>>salary;
CEmployee employee1(name,id,sex,age,department,salary);
employee1.Show2();
return 0;
}
【專案3 - 點類派生直線類】參考解答
定義點類Point,並以點類為基類,派生出直線類Line,從基類中繼承的點的資訊表示直線的中點。請閱讀下面的程式碼,並將缺少的部分寫出來。
#include<iostream>
#include<Cmath>
using namespace std;
class Point //定義座標點類
{
public:
Point():x(0),y(0) {};
Point(double x0, double y0):x(x0), y(y0) {};
void PrintPoint(); //輸出點的資訊
protected:
double x,y; //點的橫座標和縱座標
};
void Point::PrintPoint()
{
cout<<"Point: ("<<x<<","<<y<<")"; //輸出點
}
class Line: public Point //利用座標點類定義直線類, 其基類的資料成員表示直線的中點
{
public:
Line(Point pts, Point pte); //建構函式,用初始化直線的兩個端點及由基類資料成員描述的中點
double Length(); //計算並返回直線的長度
void PrintLine(); //輸出直線的兩個端點和直線長度
private:
class Point pts,pte; //直線的兩個端點,從Point類繼承的資料成員表示直線的中點
};
int main()
{
Point ps(-2,5),pe(7,9);
Line l(ps,pe);
cout<<"About the Line: "<<endl;
l.PrintLine(); //輸出直線l的資訊:兩端點及長度
cout<<"The middle point of Line is: ";
l.PrintPoint(); //輸出直線l中點的資訊
return 0;
}
程式執行參考圖:
【專案4】日期時間類 參考解答
定義一個日期類Date,資料成員包括年、月、日,SetDate(int y,int m,int d)和PrintDate()函式分別用於設定日期和顯示日期;再定義一個時間類Time,資料成員包括時、分、秒,SetTime(int h,int m,int s)和PrintTime()函式分別用於設定時間和顯示時間,在此基礎上再定義一個日期時間類TimeDate,充分利用已有的兩個類中提供的方法,實現日期和時間的設定和顯示。
請實現類TimeDate,下面是用於測試的主函式及參考執行結果。
int main()
{
TimeDate dt_a,dt_b(2010,4,16,9,30,0);
cout<<"dt_a: ";
dt_a.PrintDate_Time();
cout<<endl;
cout<<"dt_b: ";
dt_b.PrintDate_Time();
dt_a.SetTime(20,00,00);
dt_a.SetDate(2008,8,7);
cout<<endl;
cout<<"dt_after uptate: ";
dt_a.PrintDate_Time();
return 0;
}