c++程序设计实验报告 下载本文

{ passenger_load=number; payload=max_load; }

double truck::efficiency() { return payload/(payload+weight); }

void main() { vehicle bicycle; bicycle.initialize(2,25); cout<<\ cout<<\ cout<<\ car audi; audi.initialize(4,3500.0,5); cout<<\ cout<<\ cout<<\ truck jief; jief.initialize(18,12500.0); jief.init_truck(2,33675.0);

cout<<\ cout<<\ cout<<\}

[基本要求]

? 上机录入、调试上面程序。

? 运行程序,观察运行结果是否正确且满足题意要求。

? 将class car: public vehicle和class truck: public vehicle分别改为: class car: private vehicle和class truck: private vehicle 程序运行结果有无变化,为什么? [分析与思考]

? 定义并实现vehicle类、car类和truck类的构造函数,完成vehicle类、car类和truck类的数据成员初始化工作。

? 将vehicle中数据成员wheels和weight改为private性质,如何修改程序以达到相同的输出结果。

(2)下面程序对应图1所示的类层次继承结构: #include person person #include #include

class person {

protected:

char name[20];

in-service_graduate int birth_year;

public:

person(char *na, int year) { strcpy(name,na); birth_year=year; }

int cal_age(int this_year) {

return this_year-birth_year; } };

class graduate :public person {

protected: int grade;

char specialty[20]; public:

graduate(char *na, int y, int g, char *spec):person(na,y) { grade=g; strcpy(specialty,spec); }

void display(int this_year) { cout<<\ graduate age grade specialty\\n\ cout<

class teacher :public person {

protected:

char title[15]; char specialty[20]; public:

teacher(char *na, int y, char *ti, char *spec):person(na,y) { strcpy(title,ti);

teacher graduate strcpy(specialty,spec); }

void display(int this_year) { cout<<\ teacher age title specialty\\n\ cout<

};

class in_service_graduate:public teacher, public graduate {

public:

in_service_graduate(char *na, int y, char *ti, char *spec1, int g, char *spec2): teacher(na, y, ti, spec1), graduate(na, y, g, spec2) { }

void display(int this_year) {

cout<<\ age title work_specialty grade study_specialty\\n\cout<

} };

void main() {

graduate gr(\ teacher te(\

in_service_graduate sg(\ gr.display(2002); cout<

sg.display(2002); }

[基本要求]

? 阅读程序,完成in_service_graduate类中的display()函数的程序。

? 上机录入、调试上面程序。

? 运行程序,观察运行结果是否正确且满足题意要求。 [分析与思考]

? 在上面程序中类person中的数据成员name和birth_year在in_service_graduate类中有两个副本,请使用虚基类使它们在in_service_graduate类中只有一个副本。注意同时修改程序的其他相关部分。

4.源代码

1.

#include class vehicle {

protected: int wheels; double weight;

public: vehicle(int x,double y){wheels=x;weight=y;} int get_wheels() { return wheels; } double get_weight() { return weight; } double wheel_loading() { return weight/wheels; } };

class car: public vehicle {

private: int passenger_load;

public:car(int x,double y,int z=4):vehicle(x,y) {passenger_load=z;} // void initialize(int whls, double wght, int people=4 ); int passengers() { return passenger_load; } };

class truck: public vehicle {

private: int passenger_load; double payload;

public: truck(int x,double y,int z=2,double w=24000.0):vehicle(x,y) {passenger_load=z;payload=w;} // void init_truck(int number =2, double max_load =24000.0); double efficiency(); int passengers() { return passenger_load; } };

double truck::efficiency() { return payload/(payload+weight); }