c++程序设计实验报告模板Word文件下载.docx
- 文档编号:13852337
- 上传时间:2022-10-14
- 格式:DOCX
- 页数:52
- 大小:172.81KB
c++程序设计实验报告模板Word文件下载.docx
《c++程序设计实验报告模板Word文件下载.docx》由会员分享,可在线阅读,更多相关《c++程序设计实验报告模板Word文件下载.docx(52页珍藏版)》请在冰豆网上搜索。
#include<
iostream.h>
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时,也能得到正确的结果。
(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;
cin>
year>
month>
day;
Datemydate(year,month,day);
myyear=mydate.GetYear();
int&
mymonth=mydate.GetMonth();
myday=mydate.GetDay();
cout<
myyear<
endl<
mymonth<
myday<
myyear=8888;
cout<
mydate.GetYear();
·
仔细阅读上面程序,如果有错误,请更正。
上机录入、调试上面程序。
[分析和思考]
main函数中int&
、int&
和int&
语句表达的是什么思想?
这样做的目的是什么?
这种方法是否“好”呢?
为什么?
如果“不好”应该怎样修改?
4.源代码
1.#include<
inthours,minutes,seconds;
Time(){}
Time(intx,inty,intz){hours=x;
minutes=y;
seconds=z;
/*voidget_time()
}*/
voiddisplay_time()
voidadd_time(Time&
hours=t1.hours+t2.hours;
minutes=t1.minutes+t2.minutes;
seconds=t1.seconds+t2.seconds;
while(seconds>
while(minutes>
Timeone(2,67,100),two(1,56,200),three;
three.add_time(one,two);
three.display_time();
2.
#include<
Date(){};
Date(intyear,intmonth,intday);
~Date(){};
intyear;
intmonth;
intday;
Date:
5.代码测试
1.
6.测试过程和运行结果分析
1、if(seconds>
用if时当seconds和minutes>
=60时,程序只减一次60,如果seconds和minutes是60的两倍或以上的话,明显减的不够。
所以改用while的话就可以很好的解决这个问题了。
2、int&
是对mydate.GetDay()的引用,相当于给它起了个别名叫做myday,所以当myyear=8888;
时,cout<
mydate.GetYear();
输出的也是8888.
7.思考题解答
答:
是引用,相当于给右边的变量起了个别名。
这样做,“myyear=8888;
”输出的就是8888了。
这样不好,破坏了类的封装性,导致类的私有成员数据在类外可以被随意修改。
实验报告八继承与派生类
(1)掌握单继承程序设计的基本方法。
(2)掌握多继承程序设计的基本方法。
VisualC++
(1)下面程序定义一个vehicle类,并派生出car和truck两个派生类。
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(){returnpassen
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 程序设计 实验 报告 模板