您的位置:首页 > 编程语言 > C语言/C++

C++程序设计实验报告(四十一)---第五周任务三

2012-03-17 19:54 330 查看
 /* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2012, 烟台大学计算机学院学生 

* All rights reserved.

* 文件名称:             构造函数(长方柱)                 

* 作    者:                              刘镇

* 完成日期:     2012    年    3   月    17    日

* 版 本 号:         1.038

* 对任务及求解方法的描述部分

* 输入描述: .。。。。

* 问题描述: 尝试不同构造函数初始化。

* 程序输出: 长方柱的表面积和体积。

* 程序头部的注释结束

*/

#include<iostream>

using namespace std;

class Cuboid
{
public:
Cuboid();
Cuboid(double a, double b, double c):length(a),width(b),heigth(c) {}
void set_cuboid();
double volume();
double areas();
void display();

private:
double length;
double width;
double heigth;
bool is_cuboid(double, double, double);
};

Cuboid::Cuboid()
{
length = 4;

width = 5;

heigth = 6;
}

int main()
{
Cuboid c[5] =
{
Cuboid(10, 12, 20),

Cuboid(20, 25, 30),

Cuboid(30, 14, 28),

Cuboid()
};

for(int i = 0; i < 4; ++i)
{
c[i].volume();

c[i].areas();

c[i].display();
}

c[4].set_cuboid();

c[4].volume();

c[4].areas();

c[4].display();

return 0;
}

void Cuboid::set_cuboid()
{
cout << "请输入长方体的长、宽、高:" << endl;

while(1)
{
cin >> length >> width >> heigth;

if (! is_cuboid(length, width, heigth))
{
cout << "数据非法,请重新输入:" << endl;
}
else
{
break;
}
}
}

bool Cuboid::is_cuboid(double l, double w, double h)
{
if(l <= 0 || w <= 0 || h <= 0)
{
return false;
}
else
{
return true;
}
}

double Cuboid::volume()
{

return (length * width * heigth);
}

double Cuboid::areas()
{
return (2 * (length * width + length * heigth  + width * heigth));
}

void Cuboid::display()
{
cout << "长是" << length << ", 宽是" << width << ", 高是" << heigth << " 的长方体体积是" << volume() << ",面积是" << areas() <<endl;
}


 

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 c++ c class