合肥工业大学程序设计基础实验报告Word文档格式.docx
《合肥工业大学程序设计基础实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《合肥工业大学程序设计基础实验报告Word文档格式.docx(42页珍藏版)》请在冰豆网上搜索。
类似地,如果分钟数大于59,则小时数向前增1。
#include<
iostream>
usingnamespacestd;
classTime
{
private:
inthours,minutes,seconds;
public:
voidget_time()
{
cin>
>
hours>
minutes>
seconds;
}
voiddisplay_time()
cout<
<
hours<
'
:
minutes<
seconds<
endl;
voidadd_time(Time&
t1,Time&
t2)
hours=t1.hours+t2.hours;
minutes=t1.minutes+t2.minutes;
seconds=t1.seconds+t2.seconds;
if(seconds>
=60)
seconds-=60;
minutes++;
if(minutes>
minutes-=60;
hours++;
};
voidmain()
Timeone,two,three;
"
\nEnterthefirsttime(hoursminutesseconds):
;
one.get_time();
\nEnterthesecondtime(hoursminutesseconds):
two.get_time();
three.add_time(one,two);
theresultis:
three.display_time();
}
[基本要求]
●上机录入、调试上面程序。
●运行程序,输入下面两组数据:
123445
14756
②267100
156200
分析运行结果是否正确。
[分析与思考]
●定义构造函数对Time类的对象进行初始化(即不用成员函数get_time)。
●该程序要求用户输入的分钟数和秒数必须小于60,如何修改程序使得用户在输入分钟数和秒数大于等于60时,也能得到正确的结果。
1
2
结果不正确,不是预想的5,1,8说明程序不能输入大于60的份和秒。
更改后的程序为
Time(inth=0,intm=0,ints=0):
hours(h),minutes(m),seconds(s){}
while(seconds>
while(minutes>
Timeone(2,34,45),two(1,47,56),three;
(2)阅读下面的一段程序代码,代码可能有错误,请仔细分析并体会。
classDate{
public:
voidDate(){};
intDate(intyear,intmonth,intday);
void~Date(){};
int&
GetYear(){returnyear;
GetMonth(){returnmonth;
GetDay(){returnday;
private:
intyear=2000;
intmonth=12;
intday=31;
staticboolIsLeapyear;
//是否闰年
};
boolDate:
IsLeapyear=true;
intDate:
Date(intyear,intmonth,intday)
(*this).year=year;
(*this).month=month;
(*this).day=day;
intyear,month,day;
year>
month>
day;
Datemydate(year,month,day);
myyear=mydate.GetYear();
int&
mymonth=mydate.GetMonth();
myday=mydate.GetDay();
myyear<
endl<
mymonth<
myday<
myyear=8888;
cout<
mydate.GetYear();
·
仔细阅读上面程序,如果有错误,请更正。
上机录入、调试上面程序。
[分析和思考]
main函数中int&
、int&
和int&
语句表达的是什么思想?
这样做的目的是什么?
这种方法是否“好”呢?
为什么?
如果“不好”应该怎样修改?
修改后的程序:
Date(intyear=2000,intmonth=12,intday=31);
~Date(){};
intyear;
intmonth;
intday;
Date:
cin>
myyear=8888;
输入19900607
得到
分析:
语句的意义是为了让本不可以调用的私有成员,可以利用公有成员函数在主函数中得到一个引用的变量,也就是可以用引用的变量来修改类中私有成员。
但是这种做法并不可取,因为类的封装性,这样就破坏的类的封装性,没有保护好私有成员。
删掉即可。
实验八继承与派生类
(1)掌握单继承程序设计的基本方法。
(2)掌握多继承程序设计的基本方法。
学习教材有关继承与派生类的内容,对单继承语义、继承控制和访问控制,多继承的多义性及其解决方法有充分的理解和把握。
对实验基本要求应在上机实验前仔细阅读,程序应事先编制完成,上机时录入调试,同时还应设计相应的测试用例集,检查程序的正确性、可靠性、完备性和容错能力。
(1)下面程序定义一个vehicle类,并派生出car和truck两个派生类。
#include<
iostream.h>
classvehicle
protected:
intwheels;
doubleweight;
voidinitialize(intwhls,doublewght);
intget_wheels(){returnwheels;
doubleget_weight(){returnweight;
doublewheel_loading(){returnweight/wheels;
classcar:
publicvehicle
intpassenger_load;
voidinitialize(intwhls,doublewght,intpeople=4);
intpassengers(){returnpassenger_load;
classtruck:
doublepayload;
voidinit_truck(intnumber=2,doublemax_load=24000.0);
doubleefficiency();
voidvehicle:
initialize(intwhls,doublewght)
wheels=whls;
weight=wght;
voidcar:
initialize(intwhls,doublewght,intpeople)
passenger_load=people;
voidtruck:
init_truck(intnumber,doublemax_load)
passenger_load=number;
payload=max_load;
doubletruck:
efficiency()
returnpayload/(payload+weight);
vehiclebicycle;
bicycle.initialize(2,25);
thebicyclehas"
bicycle.get_wheels()<
wheels.\n"
thebicycleweighs"
bicycle.get_weight()<
pounds.\n"
thebicycle'
swheelloadingis"
bicycle.wheel_loading()<
poundspertire.\n\n"
caraudi;
audi.initialize(4,3500.0,5);
theaudihas"
audi.get_wheels()<
theaudiweighs"
audi.get_weight()<
theaudi'
audi.wheel_loading()<
truckjief;
jief.initialize(18,12500.0);
jief.init_truck(2,33675.0);
thejiefhas"
jief.get_wheels()<
thejiefweighs"
jief.get_weight()<
thejief'
sefficiencyis"
100.0*jief.efficiency()<
percent.\n"
●运行程序,观察运行结果是否正确且满足题意要求。
●将classcar:
publicvehicle和classtruck:
publicvehicle分别改为:
privatevehicle和classtruck:
privatevehicle
程序运行结果有无变化,为什么?
●定义并实现vehicle类、car类和truck类的构造函数,完成vehicle类、car类和truck类的数据成员初始化工作。
●将vehicle中数据成员wheels和weight改为private性质,如何修改程序以达到相同的输出结果。
答:
修改前和修改后的结果都是
如果将两个继承都改成私有继承,不仅使wheel和weight成员变量没办法继承,而且公有成员函数像get_wheel()都不能被类外调用,解决的办法有可以设一个在派生类和基类公有成员函数调用本类私有成员,类外调用公有成员函数即可。
思考题
vehicle(intwhls,doublewght)
{
wheels=whls;
}
intget_wheels(){returnwheels;
car(intwhls,doublewght,intpeople=4):
vehicle(whls,wght)
intpassengers(){returnpassenger_load;
truck(intwhls,doublewght,intnumber=2,doublemax_load=24000.0):
{
passenger_load=number;
doubleefficiency(){returnpayload/(payload+weight);
intpassengers(){returnpassenger_load;
vehiclebicycle(2,25);
caraudi(4,3500.0,5);
truckjief(18,12500.0,2,33675.0);
2.基类保护成员改为似有成员:
doubleweight;
weight=wght;
(2)下面程序对应图1所示的类层次继承结构:
iost