C++类实验报告.doc
文本预览下载声明
集美大学实验报告用纸
课程 面向对象程序设计 日期 2017 年 4月 21 日
班级 信计1512 学号 201521143040 姓名 王玉 成绩
实验三 类与对象编程
一、实验目的
掌握类与对象的定义与使用方法
掌握类的不同属性成员的使用方法。
掌握构造函数与析构函数的概念及使用方法
二、实验内容
定义一个Box类,包括数据成员:length、width和height,分别表示盒子的长、宽和高;成员函数:构造函数设置长、宽和高的初始值;函数volume计算盒子的体积。在主函数中,要求创建Box对象,并输出长、宽和高分别为20,18和16的盒子的体积。
设计一个表示二维圆形的Circle类,用于计算空心圆环的体积和重量。设圆环的厚度为0.5m,圆环密度为3600kg/m3,内环半径为15m,外环半径为20m。
三、实验结果
#includeiostream.h
#define pi 3.1415926
class Box
{
public:
Box(int a,int b,int c)
{
x=a;
y=b;
h=c;
}
void function()
{
int v;
v=x*y*h;
coutv=vendl;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
int GetH()
{
return h;
}
private:
int x, y,h;
};
void main()
{
int x,y,h;
cinxyh;
Box A1(x,y,h);
A1.function();
}
#includeiostream.h
#define pi 3.1415926
class Circle
{
public:
Circle(double a,double b,double c,double d)
{
R=a;
r=b;
h=c;
p=d;
}
void function()
{
double v,m;
v=pi*(R*R-r*r)*h;
m=p*v;
coutv=vendl;
coutm=mendl;
}
double GetR()
{
return R;
}
double Getr()
{
return r;
}
double Geth()
{
return h;
}
double Getp()
{
return p;
}
private:
double R, r,h,p;
};
void main()
{
double R,r,h,p;
cinRrhp;
Circle A1(R,r,h,p);
A1.function();
}
四.实验结果
20 18 16
v=5760
Press any key to continue
20 15 0.5 3600
v=274.889
m=989602
Press any key to continue
显示全部