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

第三周C++ 任务4.

2012-03-07 17:25 197 查看
【任务4】(改自教材P262第6题)仿照你阅读过的程序,编写基于对象的程序,求3个长方柱的体积。
数据成员包括长(length)、宽(width)、高(heigth)、体积,要求用成员函数实现下面的功能:
(1)由键盘输入3个长方柱的长、宽、高;(2)计算长方柱的体积(volume)和表面积(areas);(3)输出这3个长方柱的体积和表面积;
 
 
马琳。
#include <iostream>
using namespace std;
class fang
{
public:
void set_data(int, int, int);
void set_volume();
void set_areas();
void display();
private:
int length;
int width;
int heigth;
int volume;
int areas;
};

void fang::set_data(int n1,int n2,int n3)
{
length=n1;
width=n2;
heigth=n3;
}

void fang::set_volume()
{
volume=length*width*heigth;
}

void fang::set_areas()
{
areas=(length*width+length*heigth+width*heigth)*2;
}

void fang::display()
{
cout<<volume<<endl;
cout<<areas<<endl;
}

int main()
{
int length,heigth,width;
cin>>length>>heigth>>width;
fang x;
fang x2;
fang x3;
x.set_data(length,heigth,width);
x.set_volume();
x.set_areas();
x.display();
x2.set_data(length,heigth,width);
x2.set_volume();
x2.set_areas();
x2.display();
x3.set_data(length,heigth,width);
x3.set_volume();
x3.set_areas();
x3.display();
return 0;
}




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