欢迎来到天天文库
浏览记录
ID:38120954
大小:166.92 KB
页数:18页
时间:2019-06-06
《C++面向对象程序设计实验报告》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、——面向对象程序设计学院:计算机与信息工程学院班级:电子信息工程1401班实验一一.实验题目定义盒子Box类,要求具有以下成员变量:长、宽、高分别为length,with,high;具有一个静态成员变量:盒子和个数number;具有以下成员函数:1)构造函数和析构函数;2)计算盒子体积的函数;3)计算盒子的表面积的函数;4)显示长、宽、高、体积和表面积函数;5)显示盒子个数的静态成员函数。编写main函数进行测试。二.实验代码Box.h#includeusingnamespacestd;classBo
2、x{private:doublelength,width,high;staticintnumber;public:Box(doublea=0,doubleb=0,doublec=0){length=a;width=b;high=c;number++;}~Box(){number--;}doublevolunm();doublearea();voiddisplay();intgetnumber();};Box.cpp#include#include"Box.h"usingnamespacestd;dou
3、bleBox::volunm(){returnlength*width*high;}doubleBox::area(){return2*(length*width+length*high+width*high);}voidBox::display(){cout<<"长:"<4、rea()<<"平方米"<#include"Box.h"usingnamespacestd;intBox::number=0;voidmain(){Boxa(10,10,10);a.display();cout<<"个数:"<5、个私有成员,和一个number的静态成员变量。这道题的关健是不能直接访问静态成员变量,必须访问包含静态成员变量的函数才可以对其进行操作,且静态成员必须赋初值。实验二一.实验题目定义一个车基类Vehicle,含私有成员speed、weight。派生出自行车类Bicycle,增加high成员;派生出汽车类Car,增加seatnum(座位数)成员。每个类都有:各自的构造函数、设置(如setSpeed等)和返回每个成员变量(如getSpeed等)的函数、显示名称和参数的函数。编写main函数进行测试。二.实验代码Vehicle.6、h#ifndefVEHICLE_H#defineVEHICLE_H#includeusingnamespacestd;classVehicle{private:doublespeed,weight;public:Vehicle(doublea=0,doubleb=0){speed=a;weight=b;}voidsetspeed(doublea);voidsetweight(doubleb);doublegetspeed();doublegetweight();};#endifVehicle.cpp#7、include"Vehicle.h”usingnamespacestd;voidVehicle::setspeed(doublea){speed=a;}voidVehicle::setweight(doubleb){weight=b;}doubleVehicle::getspeed(){returnspeed;}doubleVehicle::getweight(){returnweight;}Bicycle.h#ifndefBICYCLE_H#defineBICYCLE_H#include"Base.h"classBic8、ycle:publicVehicle{private:doublehigh;public:Bicycle(doublea=0){high=a;}voidsethigh(doublea);doublegethigh();voidgetname();};#endifBicycle.cpp#include
4、rea()<<"平方米"<#include"Box.h"usingnamespacestd;intBox::number=0;voidmain(){Boxa(10,10,10);a.display();cout<<"个数:"<5、个私有成员,和一个number的静态成员变量。这道题的关健是不能直接访问静态成员变量,必须访问包含静态成员变量的函数才可以对其进行操作,且静态成员必须赋初值。实验二一.实验题目定义一个车基类Vehicle,含私有成员speed、weight。派生出自行车类Bicycle,增加high成员;派生出汽车类Car,增加seatnum(座位数)成员。每个类都有:各自的构造函数、设置(如setSpeed等)和返回每个成员变量(如getSpeed等)的函数、显示名称和参数的函数。编写main函数进行测试。二.实验代码Vehicle.6、h#ifndefVEHICLE_H#defineVEHICLE_H#includeusingnamespacestd;classVehicle{private:doublespeed,weight;public:Vehicle(doublea=0,doubleb=0){speed=a;weight=b;}voidsetspeed(doublea);voidsetweight(doubleb);doublegetspeed();doublegetweight();};#endifVehicle.cpp#7、include"Vehicle.h”usingnamespacestd;voidVehicle::setspeed(doublea){speed=a;}voidVehicle::setweight(doubleb){weight=b;}doubleVehicle::getspeed(){returnspeed;}doubleVehicle::getweight(){returnweight;}Bicycle.h#ifndefBICYCLE_H#defineBICYCLE_H#include"Base.h"classBic8、ycle:publicVehicle{private:doublehigh;public:Bicycle(doublea=0){high=a;}voidsethigh(doublea);doublegethigh();voidgetname();};#endifBicycle.cpp#include
5、个私有成员,和一个number的静态成员变量。这道题的关健是不能直接访问静态成员变量,必须访问包含静态成员变量的函数才可以对其进行操作,且静态成员必须赋初值。实验二一.实验题目定义一个车基类Vehicle,含私有成员speed、weight。派生出自行车类Bicycle,增加high成员;派生出汽车类Car,增加seatnum(座位数)成员。每个类都有:各自的构造函数、设置(如setSpeed等)和返回每个成员变量(如getSpeed等)的函数、显示名称和参数的函数。编写main函数进行测试。二.实验代码Vehicle.
6、h#ifndefVEHICLE_H#defineVEHICLE_H#includeusingnamespacestd;classVehicle{private:doublespeed,weight;public:Vehicle(doublea=0,doubleb=0){speed=a;weight=b;}voidsetspeed(doublea);voidsetweight(doubleb);doublegetspeed();doublegetweight();};#endifVehicle.cpp#
7、include"Vehicle.h”usingnamespacestd;voidVehicle::setspeed(doublea){speed=a;}voidVehicle::setweight(doubleb){weight=b;}doubleVehicle::getspeed(){returnspeed;}doubleVehicle::getweight(){returnweight;}Bicycle.h#ifndefBICYCLE_H#defineBICYCLE_H#include"Base.h"classBic
8、ycle:publicVehicle{private:doublehigh;public:Bicycle(doublea=0){high=a;}voidsethigh(doublea);doublegethigh();voidgetname();};#endifBicycle.cpp#include
此文档下载收益归作者所有