C++类的应用举例(构建一个长方体类)

1. 编程实现一个长方体类 cuboid

 成员变量包括:

 length(长)

 width(宽)

 height(高)

 要求成员函数实现以下功能

 输入长方体的长、宽、高 void set(float length, float width, float height);

 计算长方体的总棱长 float edge();

 计算长方体的表面积 float surface();

 计算长方体的体积 float volume();

 判断长方体是否为正方体 bool iscube();

 获取长方体的长、宽、高 float getlength(); float getwidth(); float getheight();

#include<iostream>
#include<cmath> 
#include<stdlib.h>
#include<iomanip>
#include<cstdio>
#include<cstring>
using namespace std;
class cftl
{
	public:
		void set(float a, float b, float c);
		float edge();
		float surface();
		float volume();
		bool iscube();
		float getlength();
		float getwidth();
		float getheight();
	private:
		float length,width,height;	
};
void cftl::set(float a, float b, float c)
{
	length=a;width=b;height=c;
}
float cftl::edge()
{
	return (4*(length+width+height));
}
float cftl::surface()
{
	return (2*(length*width+length*height+width*height));
}
float cftl::volume()
{
	return (length*width*height);
}
bool cftl::iscube()
{
	if(length==width&&height==width)
	{
		cout<<"是正方体";
	} 
	else 
	cout<<"长方体";
}
float cftl::getlength()
{
	return (length);
}
float cftl::getwidth()
{
	return (width);
}
float cftl::getheight()
{
	return (height);
}
int main()
{
	cftl q;
	int a,b,c;
	cout<<"请输入长方体的长宽高:";
	cin>>a>>b>>c;
	q.set(a, b, c);
	cout<<"周长"<<q.edge()<<endl;
	cout<<"表面积"<<q.surface()<<endl;
	cout<<"体积"<<q.volume()<<endl;
	q.iscube(); 
	cout<<endl;
	cout<<"长"<<q.getlength()<<endl;
	cout<<"宽"<<q.getwidth()<<endl;
	cout<<"高"<<q.getheight()<<endl;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值