1程序设计基础实验报告姓名:班级:计算机科学与技术学号:合肥工业大学计算机与信息学院2实验七类与对象1.实验目的要求(1)掌握类的定义和实现。(2)掌握对象创建及使用的基本方法。2.实验设备硬件环境:微型计算机软件环境:操作系统:Windows语言环境:VisualC++3.预习要求学习教材有关类的定义与实现、对象创建与应用等有关内容,对实验基本要求应在上机实验前仔细阅读,程序应事先编制完成,上机时录入调试,同时还应设计相应的测试用例集,检查程序的正确性、可靠性、完备性和容错能力。4.实验内容(1)下面程序定义了一个以hours,minutes和seconds作为数据成员的Time类。设计了成员函数将两个Time对象相加(即时间相加),并进行相应的检查,查看增加的分钟数及秒数是否大于59。如果秒数大于59,则分钟数向前递增1。类似地,如果分钟数大于59,则小时数向前增1。#includeiostreamusingnamespacestd;classTime{private:inthours,minutes,seconds;public:voidget_time(){3cinhoursminutesseconds;}voiddisplay_time(){couthours':'minutes':'secondsendl;}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=60){minutes-=60;hours++;}}};voidmain(){Timeone,two,three;cout\nEnterthefirsttime(hoursminutesseconds):;one.get_time();cout\nEnterthesecondtime(hoursminutesseconds):;two.get_time();three.add_time(one,two);couttheresultis:endl;three.display_time();}[基本要求]上机录入、调试上面程序。运行程序,输入下面两组数据:①2344514756②267100156200分析运行结果是否正确。4[分析与思考]定义构造函数对Time类的对象进行初始化(即不用成员函数get_time)。该程序要求用户输入的分钟数和秒数必须小于60,如何修改程序使得用户在输入分钟数和秒数大于等于60时,也能得到正确的结果。①②结果不正确,不是预想的5,1,8说明程序不能输入大于60的份和秒。更改后的程序为#includeiostreamusingnamespacestd;classTime{private:inthours,minutes,seconds;public:Time(inth=0,intm=0,ints=0):hours(h),minutes(m),seconds(s){}voiddisplay_time(){couthours':'minutes':'secondsendl;}voidadd_time(Time&t1,Time&t2){5hours=t1.hours+t2.hours;minutes=t1.minutes+t2.minutes;seconds=t1.seconds+t2.seconds;while(seconds=60){seconds-=60;minutes++;}while(minutes=60){minutes-=60;hours++;}}};voidmain(){Timeone(2,34,45),two(1,47,56),three;three.add_time(one,two);three.display_time();}(2)阅读下面的一段程序代码,代码可能有错误,请仔细分析并体会。classDate{public:voidDate(){};intDate(intyear,intmonth,intday);void~Date(){};int&GetYear(){returnyear;}int&GetMonth(){returnmonth;}int&GetDay(){returnday;}private:intyear=2000;intmonth=12;intday=31;staticboolIsLeapyear;//是否闰年};boolDate::IsLeapyear=true;intDate::Date(intyear,intmonth,intday)6{(*this).year=year;(*this).month=month;(*this).day=day;}voidmain(){intyear,month,day;cinyearmonthday;Datemydate(year,month,day);int&myyear=mydate.GetYear();int&mymonth=mydate.GetMonth();int&myday=mydate.GetDay();coutmyyearendlmymonthendlmydayendl;myyear=8888;coutmydate.GetYear();}[基本要求]·仔细阅读上面程序,如果有错误,请更正。·上机录入、调试上面程序。[分析和思考]main函数中int&myyear=mydate.GetYear();、int&mymonth=mydate.GetMonth();和int&myday=mydate.GetDay();语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?如果“不好”应该怎样修改?修改后的程序:#includeiostreamusingnamespacestd;classDate{public:Date(intyear=2000,intmonth=12,intday=31);~Date(){};int&GetYear(){returnyear;}int&GetMonth(){returnmonth;}int&GetDay(){returnday;}private:intyear;intmonth;intday;staticboolIsLeapyear;};7boolDate::IsLeapyear=true;Date::Date(intyear,intmonth,intday){(*this).year=year;(*this).month=month;(*this).day=day;}voidmain(){intyear,month,day;cinyearmonthday;Datemydate(year,month,day);int&myyear=mydate.GetYear();int&mymonth=mydate.GetMonth();int&myday=mydate.GetDay();coutmyyearendlmymonthendlmydayendl;myyear=8888;coutmydate.GetYear();}输入19900607得到分析:语句的意义是为了让本不可以调用的私有成员,可以利用公有成员函数在主函数中得到一个引用的变量,也就是可以用引用的变量来修改类中私有成员。但是这种做法并不可取,因为类的封装性,这样就破坏的类的封装性,没有保护好私有成员。删掉即可。8实验八继承与派生类1.实验目的要求(1)掌握单继承程序设计的基本方法。(2)掌握多继承程序设计的基本方法。2.实验设备硬件环境:微型计算机软件环境:操作系统:Windows语言环境:VisualC++3.预习要求学习教材有关继承与派生类的内容,对单继承语义、继承控制和访问控制,多继承的多义性及其解决方法有充分的理解和把握。对实验基本要求应在上机实验前仔细阅读,程序应事先编制完成,上机时录入调试,同时还应设计相应的测试用例集,检查程序的正确性、可靠性、完备性和容错能力。4.实验内容(1)下面程序定义一个vehicle类,并派生出car和truck两个派生类。#includeiostream.hclassvehicle{protected:intwheels;doubleweight;public:voidinitialize(intwhls,doublewght);intget_wheels(){returnwheels;}doubleget_weight(){returnweight;}doublewheel_loading(){returnweight/wheels;}};classcar:publicvehicle{private:9intpassenger_load;public:voidinitialize(intwhls,doublewght,intpeople=4);intpassengers(){returnpassenger_load;}};classtruck:publicvehicle{private:intpassenger_load;doublepayload;public:voidinit_truck(intnumber=2,doublemax_load=24000.0);doubleefficiency();intpassengers(){returnpassenger_load;}};voidvehicle::initialize(intwhls,doublewght){wheels=whls;weight=wght;}voidcar::initialize(intwhls,doublewght,intpeople){wheels=whls;weight=wght;passenger_load=people;}voidtruck::init_truck(intnumber,doublemax_load){passenger_load=number;payload=max_load;}doubletruck::efficiency(){returnpayload/(payload+weight);}voidmain(){vehiclebicycle;10bicycle.initialize(2,25);coutthebicyclehasbicycle.get_wheels()wheels.\n;coutthebicycleweighsbicycle.get_weight()pounds.\n;coutthebicycle'swheelloadingisbicycle.wheel_loading()poundspertire.\n\n;caraudi;audi.initialize(4,3500.0,5);couttheaudihasaudi.get_wheels()wheels.\n;couttheaudiweighsaudi.get_weight()pounds.\n;couttheaudi'swheelloadingisaudi.wheel_loading()poundspertire.\n\n;truckjief;jief.initialize(18,12500.0);jief.init_truck(2,33675.0);coutthejiefhasjief.get_wheels()wheels.\n;coutthejiefweighsjief.g