实验二类和对象_参考答案

上传人:今*** 文档编号:105754322 上传时间:2019-10-13 格式:DOC 页数:10 大小:190KB
返回 下载 相关 举报
实验二类和对象_参考答案_第1页
第1页 / 共10页
实验二类和对象_参考答案_第2页
第2页 / 共10页
实验二类和对象_参考答案_第3页
第3页 / 共10页
实验二类和对象_参考答案_第4页
第4页 / 共10页
实验二类和对象_参考答案_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《实验二类和对象_参考答案》由会员分享,可在线阅读,更多相关《实验二类和对象_参考答案(10页珍藏版)》请在金锄头文库上搜索。

1、实验二 类和对象(参考答案)班级: 学号: 姓名: 成绩: 一 实验目的1理解面向对象程序设计的基本思想;2掌握类和对象的概念、定义和使用方法。3掌握不同特性对象成员的访问方法。二 使用的设备和仪器计算机+Windows XP +Visual C+6.0三 实验内容及要求1、 定义一个表示长方体类Cuboid,数据成员包括length(长)、width(宽)、height(高),成员函数包括长方体的输入、输出、计算体积和表面积等。在主函数中,定义3个长方体的对象,并调用成员函数完成其功能。2、 定义一个学生类Student,数据成员包括学号、姓名、数学成绩、英语成绩和C+成绩,成员函数包括:输

2、入学生的信息函数;输出学生的信息函数;设置学生的信息函数;计算学生的平均成绩的函数。在main函数中调用以上函数实现相应功能。3、 定义一个图书类Book,在该类中包括以下数据成员和成员函数:数据成员:id(书号)、bookname(书名)、price(价格)、total(总存书数量)、number(当前剩余图书数量)成员函数:Input()图书信息输入;Output()图书信息输出;Borrow()借阅图书,并显示当前剩余图书数量;Restore()归还图书,并显示当前剩余图书数量。在主函数中,要求创建某种图书对象,并对该图书进行简单的输入、输出、借阅和归还管理。选择题:4、 根据以下要求类

3、的编写。1)定义一个日期类Date,数据成员包括年、月、日,成员函数包括:Input()日期信息输入;Output()日期信息输出;Set()设置日期信息2)在第2题Student类中增加一个出生日期成员,使用Date类来定义。然后修改相应的成员函数,并增加一个成员函数GetAge,用来计算并返回学生的年龄。在主函数中定义对象,测试以上功能。四 实验步骤1、 程序代码:#include using namespace std;class Cuboidpublic:void Input();void Show();float Volume();float Area();private:float

4、 length;float width;float height;void Cuboid:Input() coutlengthwidthheight;void Cuboid:Show() coutlength=length width=width height=heightendl;float Cuboid:Volume() return(length*width*height);float Cuboid:Area()return (length*width+length*height+height*width)*2;int main()Cuboid Cuboid1,Cuboid2;Cuboi

5、d1.Input();coutCuboid1 Information:endl;Cuboid1.Show();coutVolmue=Cuboid1.Volume()endl;coutArea=Cuboid1.Area()endl;coutendl;Cuboid2.Input();coutCuboid2 Information:endl;Cuboid2.Show();coutVolmue=Cuboid2.Volume()endl;coutArea=Cuboid2.Area()endl;coutendl;return 0;运行结果:2、 程序代码:/student.h 学生信息的头文件#inclu

6、de #includeusing namespace std;class Studentpublic:void Input_Stu(); /输入学生信息函数void Show_Stu(); /输出学生信息函数void Set(int n,string nm,double m,double e,double c); /设置学生信息函数double Ave_Stu(); /计算并返回学生平均成绩函数private:int num;string name;double math,english,cprogram;/student.cpp 学生信息的源文件#includestudent.hvoid S

7、tudent:Input_Stu()cout请输入学生的学号、姓名、数学、英语、C+的成绩:numnamemathenglishcprogram;void Student:Show_Stu()cout*Student Info*endl;coutnum=numendl;coutname=nameendl;coutmath=mathendl;coutenglish=englishendl;coutcprogram=cprogramendl;void Student:Set(int n,string nm,double m,double e,double c)num=n;name=nm; math

8、=m;english=e;cprogram=c;double Student:Ave_Stu()return (math+english+cprogram)/3;/main.cpp 主函数所对应的源文件#includestudent.hint main()Student s1;s1.Input_Stu ();s1.Show_Stu ();coutAverage Score=s1.Ave_Stu ()endl;coutendl;s1.Set(2001,Tom,70,80,90);s1.Show_Stu ();coutendlAverage Score=s1.Ave_Stu ()endl;cout

9、endl;return 0;运行结果:3、 程序代码:#include#includeusing namespace std;class Bookpublic:void Input(); /图书信息输入;void Output(); /图书信息输出;void Borrow(); /借阅图书,并显示当前剩余图书数量;void Restore(); /归还图书,并显示当前剩余图书数量。void ShowNumber(); /显示剩余图书数量private:int id;string bookname;double price;int total;int number;void Book:Input

10、 ()while(1)cout请输入图书编号、名称、价格、总数量:idbooknamepricetotal;if(price0 | total 0)cout价格或总数量不合法,请重新输入图书信息!endl;elsenumber=total;break;void Book:Output ()cout编号:idendl;cout名称:booknameendl;cout价格:priceendl;cout总数量:totalendl;cout剩余数量:numberendl;void Book:ShowNumber ()cout剩余数量:numberendl;void Book:Borrow ()if(n

11、umber=0)cout图书剩余数量为0!请下次再来借阅。endl;elsenumber-;cout成功借阅出一本图书!endl;ShowNumber();void Book:Restore ()number+;cout成功归还回一本图书!endl;ShowNumber();int main()Book bk1;bk1.Input ();bk1.Output ();coutendl;bk1.Borrow ();bk1.Borrow ();coutendl;bk1.Restore ();coutendl;bk1.Output ();coutendl;return 0;运行结果:4、 程序代码:#

12、include #include #include using namespace std;const int NOWYEAR=2015; /当前年份/*Date类*class Dateprivate:int year,month,day;public:void Input();void Set(int,int,int); void Show();int GetYear();void Date:Input()cinyearmonthday;void Date:Set(int y,int m,int d)year=y; month=m; day=d;void Date:Show()coutyear年month月day日endl;int D

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号