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

 /* (程序头部注释开始)
* 程序的版权和版本声明部分
* 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;  
}  





 


运行结果:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值