内蒙古科技大学面向对象的程序设计实验4 类和对象一.docx
- 文档编号:18557579
- 上传时间:2023-08-19
- 格式:DOCX
- 页数:14
- 大小:277.47KB
内蒙古科技大学面向对象的程序设计实验4 类和对象一.docx
《内蒙古科技大学面向对象的程序设计实验4 类和对象一.docx》由会员分享,可在线阅读,更多相关《内蒙古科技大学面向对象的程序设计实验4 类和对象一.docx(14页珍藏版)》请在冰点文库上搜索。
内蒙古科技大学面向对象的程序设计实验4类和对象一
内蒙古科技大学
面向对象的程序设计
实验报告
课程名称:
面向对象的程序设计
姓名:
陈伟光
实验名称:
类和对象
(一)
学号:
1567159118
任课教师:
刘月峰
专业:
软件工程
指导教师:
刘月峰
班级:
15-软件一班
一、实验目的
(1) 掌握声明类的方法,类和类的成员的概念以及定义对象的方法。
(2) 初步掌握用类和对象编制基于对象的程序。
(3) 学习检查和调试基于对象的程序。
二、实验环境
编译器:
VisualC++6.0.
操作系统:
Windows7 旗舰版
三、实验内容
2.1练习
(一):
1. 新建一个项目。
2. 输入以下程序:
#include
usingnamespacestd;
classTime
{
public:
inthour;intminute;intsec;
};
intmain()
{
Timet1;
cin>>t1.hour;
cin>>t1.minute;
cin>>t1.sec;
cout< ”< ”< return0; } 3. 按要求编写程序,生成后执行,并分析输出的信息。 按要求改写程序: 1. 将数据成员改为私有的; 2. 将输入输出的功能改为由成员函数实现; 3. 在类体内定义成员函数。 4. 编译和运行程序。 请分析什么成员应指定为公用的? 什么成员应指定为私有的? 什么函数最好放在类中定义? 什么函数最好放在类外定义? 2.2练习 (二): 1. 新建一个项目。 2. 在项目中,添加头文件student.h,并输入以下代码。 classStudent { public: voiddisplay(); private: intnum;charname[20]; charsex; }; 3. 在项目中,添加源文件student.cpp,包含成员函数定义。 #include #include“student.h” voidStudent: : display() { cout<<“num: ”< cout<<“name: ”< cout<<“sex: ”< } 4. 在包含主函数的源文件main.cpp,添加如下代码。 #include #include“student.h” intmain() { Studentstud; stud.display(); return0; } 5. 请完善该程序,在类中添加一个对数据成员赋初值的成员函数set_value。 6. 按要求编写程序,生成后执行,并分析输出的信息。 2.3练习(三): 1. 新建一个项目。 2. 需要求3个长方柱的体积,请编写一个基于对象的程序。 数据成员包括length(长)、width(宽)、height(高)。 要求用成员函数实现以下功能: 1) 由键盘分别输入3个长方柱的长、宽、高; 2) 计算长方柱的体积; 3) 输出3个长方柱的体积。 3. 按要求编写程序,生成后执行,并分析输出的信息。 四、实验过程 练习1 实验代码如下: 运行结果如下: 运行结果无误,已将源代码中的函数功能包含至成员函数 练习2 工作区如下 文件分别如下 main.cpp student.cpp student.h 运行结果如下 练习3 实验代码如下: 运行结果如下 五、实验总结 实验源代码: 练习(4.1) #include usingnamespacestd; classTime { private: inthour;intminute;intsec; public: Time(inth=0,intm=0,ints=0) { hour=h; minute=m; sec=s; cout<<"构造函数已调用;"< } ~Time() { cout<<"析构函数已调用;"< } voiddisplay() { cout< "< "< } voidSet_value() { cout<<"输入创建对象的数据信息: 依次输入hour,minute,sec"< cin>>hour>>minute>>sec; } }; intmain() { Timet1; t1.Set_value(); t1.display(); return0; } 练习(4.2) Student.h classStudent { public: Student(intn=0,char*na="noname",chars=’M’) { cout<<"调用构造函数"< num=n; strcpy(name,na); sex=s; } voidset_value(); voiddisplay(); private: intnum; charname[20]; charsex; }; Student.cpp #include #include #include"student.h" voidStudent: : display() { cout<<"调用成员函数display"< cout<<"num: "< cout<<"name: "< cout<<"sex: "<<((sex==’M’||sex==’m’)? "Man": "Woman")< } voidStudent: : set_value() { cout<<"调用成员函数set_value"< cout<<"请依次输入num,name,sex: "< cin>>num>>name>>sex; } Main.h #include #include #include"student.h" intmain() { Studentstud; stud.set_value(); stud.display(); return0; } 练习(4.3) #include #include classCuboid { intlength; intwidth; intheight; public: Cuboid(intl=0,intw=0,inth=0) { length=l; width=w; height=h; } voidSetData(); voidvolume(); }; voidCuboid: : SetData() { cout<<"请输入长方体的长,宽,高: "< cin>>length>>width>>height; } voidCuboid: : volume() { intvolume; volume=length*width*height; cout<<"体积为"< } voidmain() { CuboidC1,C2,C3; C1.SetData(); C1.volume(); C2.SetData(); C2.volume(); C3.SetData(); C3.volume(); } 六、参考文献 参考的资料: C++面向对象程序设计
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 内蒙古科技大学面向对象的程序设计实验4 类和对象一 内蒙古 科技大学 面向 对象 程序设计 实验