文档详情

实验二类和对象_参考答案分析.doc

发布:2017-01-07约6.1千字共10页下载文档
文本预览下载声明
实验二 类和对象(参考答案) 班级: 学号: 姓名: 成绩: 实验目的 1.理解面向对象程序设计的基本思想; 2.掌握类和对象的概念、定义和使用方法。 3.掌握不同特性对象成员的访问方法。 使用的设备和仪器 计算机+Windows XP +Visual C++6.0 实验内容及要求 定义一个表示长方体类Cuboid,数据成员包括length(长)、width(宽)、height(高),成员函数包括长方体的输入、输出、计算体积和表面积等。在主函数中,定义3个长方体的对象,并调用成员函数完成其功能。 定义一个学生类Student,数据成员包括学号、姓名、数学成绩、英语成绩和C++成绩,成员函数包括:输入学生的信息函数;输出学生的信息函数;设置学生的信息函数;计算学生的平均成绩的函数。在main函数中调用以上函数实现相应功能。 定义一个图书类Book,在该类中包括以下数据成员和成员函数: 数据成员:id(书号)、bookname(书名)、price(价格)、total(总存书数量)、number(当前剩余图书数量) 成员函数: Input()——图书信息输入; Output()——图书信息输出; Borrow()——借阅图书,并显示当前剩余图书数量; Restore()——归还图书,并显示当前剩余图书数量。 在主函数中,要求创建某种图书对象,并对该图书进行简单的输入、输出、借阅和归还管理。 选择题: 根据以下要求类的编写。 1)定义一个日期类Date,数据成员包括年、月、日,成员函数包括: Input()——日期信息输入; Output()——日期信息输出; Set()——设置日期信息 2)在第2题Student类中增加一个出生日期成员,使用Date类来定义。然后修改相应的成员函数,并增加一个成员函数GetAge,用来计算并返回学生的年龄。 在主函数中定义对象,测试以上功能。 实验步骤 程序代码: #include iostream using namespace std; class Cuboid { public: void Input(); void Show(); float Volume(); float Area(); private: float length; float width; float height; }; void Cuboid::Input() { coutplease input length,width,height:; cinlengthwidthheight; } 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; Cuboid1.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; } 运行结果: 程序代码: //student.h 学生信息的头文件 #include iostream #includestring using namespace std; class Student { public: 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
显示全部
相似文档