- 1、本文档共7页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
c实验四实验报告
实验四实验报告
1.实验目的及要求
(1) 掌握单继承程序设计的基本方法。
(2) 掌握多继承程序设计的基本方法。
2.实验设备
计算机、Microsoft Visual C++
3.实验内容
(1) 下面程序定义一个vehicle 类,并派生出car 和truck 两个派生类。
#includeiostream.h
class vehicle
{
protected:
int wheels;
double weight;
public:
void initialize(int whls, double wght);
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:
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:
void init_truck(int number =2, double max_load =24000.0);
double efficiency();
int passengers() { return passenger_load; }
};
void vehicle::initialize(int whls, double wght)
{
wheels=whls;
weight=wght;
}
void car::initialize(int whls, double wght, int people)
{
wheels=whls;
weight=wght;
passenger_load=people;
}
void truck::init_truck(int number, double max_load)
{
passenger_load=number;
payload=max_load;
}
double truck::efficiency()
{
return payload/(payload+weight);
}
void main()
{
vehicle bicycle;
bicycle.initialize(2,25);
coutthe bicycle has bicycle.get_wheels() wheels.\n;
coutthe bicycle weighs bicycle.get_weight() pounds.\n;
coutthe bicycles wheel loading is bicycle.wheel_loading() pounds per
tire.\n\n;
car audi;
audi.initialize(4,3500.0,5);
coutthe audi has audi.get_wheels() wheels.\n;
coutthe audi weighs audi.get_weight() pounds.\n;
coutthe audis wheel loading is audi.wheel_loading() pounds per
tire.\n\n;
truck jief;
jief.initialize(18,12500.0);
person
person
teacher
graduate
in-service_graduate
jief.init_truck(2,33675.0);
coutthe jief has jief.get_wheels() wheels.\n;
coutthe jief weighs jief.get_weight() pounds.\n;
coutthe jiefs efficiency is 100.0*jief.efficiency() percent.\n;
}
[基本要求]
??上机录入、调试上面程序。
??运行程序,观察运行结果是否正确且满足题意要求。
??将class car: public vehicle 和class truck: public vehicle 分别改为:
class car: private vehicle 和class truck:
文档评论(0)