c++程序设计实验报告 .doc
《c++程序设计实验报告 .doc》由会员分享,可在线阅读,更多相关《c++程序设计实验报告 .doc(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, minutes
2、, seconds;public: void get_time() cinhoursminutesseconds; void display_time() couthours:minutes:seconds=60) seconds-=60; minutes+; if(minutes=60) minutes-=60; hours+; ;void main() Time one, two, three; coutnEnter the first time(hours minutes seconds):; one.get_time(); coutnEnter the second time(hour
3、s minutes seconds):; two.get_time(); three.add_time(one,two); coutthe 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;
5、/* void get_time() cinhoursminutesseconds; */ void display_time() couthours:minutes:seconds=60) seconds-=60; minutes+; while(minutes=60) minutes-=60; hours+; ;void main() Time one( 2 , 67 , 100), two( 1 , 56 , 200), three; three.add_time(one,two); coutthe result is:endl; three.display_time();2.#incl
6、udeclass Date public: Date(); Date(int year,int month,int day); Date(); int &GetYear()return year; int &GetMonth()return month; int &GetDay()return 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=
7、year; (*this).month=month; (*this).day=day;void main() int year,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=6
8、0) minutes-=60; hours+; 用if时当seconds和minutes=60时,程序只减一次60,如果seconds和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
9、.GetMonth(); 和int &myday=mydate.GetDay();语句表达的是什么思想?这样做的目的是什么?这种方法是否“好”呢?为什么?如果“不好”应该怎样修改?答:int &myyear=mydate.GetYear(); 、int &mymonth=mydate.GetMonth(); 和int &myday=mydate.GetDay();是引用,相当于给右边的变量起了个别名。这样做,“myyear=8888;cout mydate.GetYear();”输出的就是8888了。这样不好,破坏了类的封装性,导致类的私有成员数据在类外可以被随意修改。 实验报告八 继承与派生
10、类1. 实验目的(1) 掌握单继承程序设计的基本方法。(2) 掌握多继承程序设计的基本方法。2. 实验设备 硬件环境:微型计算机软件环境: 操作系统: 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_weig
11、ht() return weight; double wheel_loading() return weight/wheels; ;class car: public 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
12、: void init_truck(int number =2, double max_load =24000.0); double efficiency(); int passengers() 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;voi
13、d truck:init_truck(int number, double max_load) passenger_load=number; payload=max_load;double truck:efficiency() return payload/(payload+weight);void main() vehicle bicycle; bicycle.initialize(2,25); coutthe bicycle has bicycle.get_wheels() wheels.n; coutthe bicycle weighs bicycle.get_weight() poun
14、ds.n; coutthe bicycles wheel loading is bicycle.wheel_loading() pounds per tire.nn; car audi; audi.initialize(4,3500.0,5); coutthe audi has audi.get_wheels() wheels.n; coutthe audi weighs audi.get_weight() pounds.n; coutthe audis wheel loading is audi.wheel_loading() pounds per tire.nn; truck jief;
15、jief.initialize(18,12500.0); jief.init_truck(2,33675.0); coutthe jief has jief.get_wheels() wheels.n; coutthe jief weighs jief.get_weight() pounds.n; coutthe jiefs efficiency is 100.0*jief.efficiency() percent.n;基本要求l 上机录入、调试上面程序。l 运行程序,观察运行结果是否正确且满足题意要求。l 将class car: public vehicle和class truck: pub
16、lic vehicle分别改为:class car: private vehicle和class truck: private vehicle程序运行结果有无变化,为什么?分析与思考l 定义并实现vehicle类、car类和truck类的构造函数,完成vehicle类、car类和truck类的数据成员初始化工作。l 将vehicle中数据成员wheels和weight改为private性质,如何修改程序以达到相同的输出结果。person(2)下面程序对应图1所示的类层次继承结构:person#include #include graduateteacher#include class pers
17、on protected:char name20;in-service_graduateint birth_year;public: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 person protected:int grade;char specialty20;public:graduate(char *na, int y, int g, char *spec):p
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+程序设计实验报告 c+ 程序设计 实验 报告
链接地址:https://www.31ppt.com/p-2384409.html