c++实验报告 类与对象
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
昆明理工大学信息工程与自动化学院学生实验报告
(201 —201 学年第二学期)
课程名称:C++程序设计开课实验室:年月日
题目1:
设计一个立方体类Box,它能计算并输出立方体的体积和表面积。
实验提示:Box类包含三个私有数据成员a(立方体边长)、volume(体积)和area(表面积),另有两个构造函数以及seta()(设置立方体边长)、getvolume()(计算体积)、getarea()(计算表面积)和disp()(输出结果)。
源程序1
/* box.h */
#ifndef BOX_H
#define BOX_H
class box
{
float len,wid,high;
public:
box(float l=0,float v=0,float h=0):len(l),wid(v),high(h){}
void a();//立方体边长
void seta(float l,float v,float h);//设置立方体边长void getvolume();//计算体积
void getarea();//计算表面积
void disp();//输出边长
};
#endif
/* box.cpp LLZ 2012.4.9 */
#include "box.h"
#include
#include
using namespace std;
void box::a()//立方体边长
{
cout<<"立方体边长"<<4*(len+wid+high)< } void box::seta(float l,float v,float h)//设置立方体边长{ len=l,wid=v,high=h; } void box::getvolume()//计算体积 { cout<<"立方体体积"< } void box::getarea()//计算表面积 { cout<<"立方体表面积"<<2*(len*wid+wid*high+len*high)< } void box::disp()//输出结果 { cout<<"立方体长宽高"< /* main.cpp */ #include "box.h" #include using namespace std; void main() { box a; a.seta(5,10,12); a.disp(); a.a(); a.getarea(); a.getvolume(); } 运行结果截图 题目2: 下面是一个类的测试程序,设计出能使用如下测试程序的类。 int main( ) { Test a; a.init(10,20); a.print(); return 0; } 源程序2 /* test.h */ #ifndef TEST_H #define TEST_H class test { int a,b; public: void init(int,int); void print(); }; #endif /* test.cpp */ #include "test.h" #include using namespace std; void test::init(int c,int d) { a=c; b=d; } void test::print() {