c++程序设计实验报告.docx
《c++程序设计实验报告.docx》由会员分享,可在线阅读,更多相关《c++程序设计实验报告.docx(41页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、试验报告七 类与对象1. 试验目的(1) 把握类的定义和实现。(2) 把握对象创立及使用的根本方法。2. 试验设备硬件环境:微型计算机软件环境:操作系统: Windows语言环境: Visual C+3. 试验内容(1) 下面程序定义了一个以hours, minutes 和 seconds 作为数据成员的Time 类。设计了成员函数将两个Time 对象相加即时间相加,并进展相应的检查,查看增加的分钟数及秒数是否大于59。假设秒数大于 59,则分钟数向前递增 1。类似地,假设分钟数大于 59,则小时数向前增 1。#include class Timeprivate:int hours, minu
2、tes, seconds; public:void get_timecinhoursminutesseconds;void display_timecouthours”:”minutes”:”seconds=60)seconds-=60; minutes+;if(minutes=60)minutes-=60; hours+;void mainTime one, two, three;cout“nEnter the first time(hours minutes seconds):“; one.get_time;cout“nEnter the second time(hours minutes
3、 seconds):“; two.get_time;three.add_time(one,two); cout“the result is:“yearmonthday; Date mydate(year,month,day);int&myyear=mydate.GetYear; int&mymonth=mydate.GetMonth; int&myday=mydate.GetDay;coutmyyearendlmymonthendlmydayendl; myyear=8888;cout mydate.GetYear;根本要求认真阅读上面程序,假设有错误,请更正。上机录入、调试上面程序。分析和思
4、考main 函 数 中 int &myyear=mydate.GetYear; 、 int &mymonth=mydate.GetMonth;和 int &myday=mydate.GetDay;语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?假设“不 好”应当怎样修改?4. 源代码1.#include class Timeprivate:int hours, minutes, seconds;public:Time Time (int x,int y,int z)hours=x;minutes=y;seconds=z;/* void get_timecinhoursm
5、inutesseconds;*/void display_timecouthours”:”minutes”:”seconds=60)seconds-=60; minutes+;while(minutes=60)minutes-=60; hours+;void mainTime one(2 , 67 , 100), two( 1 , 56 , 200), three; three.add_time(one,two);cout“the result is:“endl; three.display_time;2.#includeclass Date public:Date;Date(int year
6、,int month,int day);Date;int &GetYearreturn year;int &GetMonthreturn month; int &GetDayreturn day;private:int year; int month; int day;static bool IsLeapyear;/是否闰年;bool Date:IsLeapyear=true; Date:Date(int year,int month,int day)(*this).year=year; (*this).month=month; (*this).day=day;void mainint yea
7、r,month,day; cinyearmonthday; Date mydate(year,month,day);int&myyear=mydate.GetYear; int&mymonth=mydate.GetMonth; int&myday=mydate.GetDay;coutmyyearendlmymonthendlmydayendl; myyear=8888;cout=60)seconds-=60; minutes+;if(minutes=60)minutes-=60; hours+;用 if 时当 seconds 和 minutes=60 时,程序只减一次 60,假设 second
8、s 和 minutes 是 60 的两倍或以上的话,明显减的不够。所以改用while 的话就可以很好的解决这个问题了。2、int&myday=mydate.GetDay;是对 mydate.GetDay的引用,相当于给它起了个别名叫做myday,所以当myyear=8888;时,cout mydate.GetYear;输出的也是 8888.7. 思考题解答main 函 数 中 int &myyear=mydate.GetYear; 、 int &mymonth=mydate.GetMonth;和 int &myday=mydate.GetDay;语句表达的是什么思想?这样做的目的是什么?这种方
9、法是否“好”呢?为什么?假设“不 好”应当怎样修改?答:int &myyear=mydate.GetYear; 、int &mymonth=mydate.GetMonth; 和 int &myday=mydate.GetDay;是引用,相当于给右边的变量起了个别名。这样做,“myyear=8888;cout mydate.GetYear;”输出的就是 8888 了。这样不好,破坏了类的封装性,导致类的私有成员数据在类外可以被任凭修改。试验报告八 继承与派生类1. 试验目的(1) 把握单继承程序设计的根本方法。(2) 把握多继承程序设计的根本方法。2. 试验设备硬件环境:微型计算机软件环境:操作
10、系统: Windows语言环境: Visual C+3. 试验内容(1) 下面程序定义一个vehicle 类,并派生出car 和 truck 两个派生类。#includeclass vehicleprotected:int wheels; double weight;public:;void initialize(int whls, double wght); int get_wheels return wheels; double get_weight return weight; double wheel_loading return weight/wheels; class car: p
11、ublic vehicleprivate:int passenger_load;public:;void initialize(int whls, double wght, int people =4); int passengers return passenger_load; class truck: public vehicleprivate:int passenger_load; double payload;public:;void init_truck(int number =2, double max_load =24000.0); double efficiency;int p
12、assengers return passenger_load; void vehicle:initialize(int whls, double wght)wheels=whls; weight=wght;void car:initialize(int whls, double wght, int people)wheels=whls;weight=wght; passenger_load=people;void truck:init_truck(int number, double max_load)passenger_load=number; payload=max_load;doubl
13、e truck:efficiencyreturn payload/(payload+weight);void mainvehicle bicycle; bicycle.initialize(2,25);cout“the bicycle has “bicycle.get_wheels“ wheels.n“; cout“the bicycle weighs “bicycle.get_weight“ pounds.n“;cout“the bicycle”s wheel loading is “bicycle.wheel_loading“ pounds per tire.nn“;car audi; a
14、udi.initialize(4,3500.0,5);cout“the audi has “audi.get_wheels“ wheels.n“; cout“the audi weighs “audi.get_weight“ pounds.n“;cout“the audi”s wheel loading is “audi.wheel_loading“ pounds per tire.nn“;truck jief; jief.initialize(18,12500.0); jief.init_truck(2,33675.0);cout“the jief has “jief.get_wheels“
15、 wheels.n“; cout“the jief weighs “jief.get_weight“ pounds.n“;cout“the jief”s efficiency is “100.0*jief.efficiency“ percent.n“;根本要求l 上机录入、调试上面程序。l 运行程序,观看运行结果是否正确且满足题意要求。l 将 class car: public vehicle 和 class truck: public vehicle 分别改为: class car: private vehicle 和 class truck: private vehicle程序运行结果有无
16、变化,为什么?分析与思考l 定义并实现 vehicle 类、car 类和 truck 类的构造函数,完成vehicle 类、car 类和 truck 类的数据成员初始化工作。l 将 vehicle 中数据成员wheels 和 weight 改为private 性质,如何修改程序以到达一样的输出结果。personperson2下面程序对应图 1 所示的类层次继承构造: #include teachergraduate#include #include class personin-service_graduateprotected:char name20;int birth_year; publ
17、ic:person(char *na, int year) strcpy(name,na); birth_year=year;int cal_age(int this_year) return this_year-birth_year;class graduate :public personprotected: int grade;char specialty20; public:graduate(char *na, int y, int g, char *spec):person(na,y) grade=g;strcpy(specialty,spec);void display(int t
18、his_year) cout“graduateagegradespecialtyn“; coutsetw(20)namesetw(5)cal_age(this_year); coutsetw(7)gradesetw(17)specialtyendl;class teacher :public personprotected:char title15;char specialty20; public:teacher(char *na, int y, char *ti, char *spec):person(na,y) strcpy(title,ti);strcpy(specialty,spec)
19、;void display(int this_year) cout“teacheragetitlespecialtyn“; coutsetw(20)namesetw(5)cal_age(this_year); coutsetw(14)titlesetw(17)specialtyendl;class in_service_graduate:public teacher, public graduatepublic:in_service_graduate(char *na, int y, char *ti, char *spec1, int g, char *spec2): teacher(na,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 程序设计 实验 报告
限制150内