第一范文网 - 专业文章范例文档资料分享平台

面向对象程序设计c++试题(含答案)

来源:用户分享 时间:2025/5/25 18:43:38 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

3、设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。在主程序中定义一个car类对象,对其车轮个数、车重、载人数进行设置并显示。(10分) 1、

#include #include class rectangle //(2分) {

private:

int x1,y1,x2,y2; // (2分) public:

rectangle(int xx1,int yy1,int xx2,int yy2) //(1分) {

x1=xx1;y1=yy1;x2=xx2;y2=yy2; }

int getarea() //(2分) {

return abs((x2-x1)*(y1-y2)); } };

void main() {

rectangle rect1(3,7,8,5); (2分)

cout<

2、(10分)

#include class car;(1分) class boat{ private:

int weight; //(1分) public:

boat(int w):weight(w){} //(1分)

friend int totalweight(boat b1,car c1); //(2分) };

class car{ /(1分) private:

int weight; (1分) public:

car(int w):weight(w){};

friend int totalweight(boat b1,car c1); (1分) };

int totalweight(boat b1,car c1) //(1分) {

return b1.weight+c1.weight; }

void main() {

car c1(1000); boat b1(2000);

cout<

3、(10分)

#include

class vehicle // 定义汽车类 (3分) {

protected:

int wheels; // 车轮数 float weight; // 重量 public:

vehicle(int wheels,float weight); int get_wheels(); float get_weight(); float wheel_load(); void show(); };

class car:public vehicle // 定义小车类 (3分) {

int passenger_load; // 载人数

public:

car(int wheels,float weight,int passengers=4); int get_passengers(); void show(); };

vehicle::vehicle(int wheels1,float weight1) //(1分) {

wheels=wheels1; weight=weight1; }

int vehicle::get_wheels() {

return wheels; }

float vehicle::get_weight() {

return weight; }

void vehicle::show() (1分) {

cout << \车轮:\个\cout << \重量:\公斤\}

car::car(int wheels, float weight,

int passengers) :vehicle(wheels, weight) {

passenger_load=passengers; }

int car::get_passengers () {

return passenger_load; }

void car::show() {

cout <<\车型:小车\vehicle::show();

cout << \载人:\人\cout << endl;

}

void main () {

car car1(4,2000,5); (1分) cout << \输出结果\car1. show (); (1分) }

搜索更多关于: 面向对象程序设计c++试题(含答案) 的文档
面向对象程序设计c++试题(含答案).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1bzth53mj91lh1d7s6zm_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top