C++练习【笔记】

28 篇文章 0 订阅
4 篇文章 0 订阅

编程定义一个Box(盒子)类,在该类定义中包括数据成员:length(长)、width(宽) 和height(高)。成员函数:构造函数设置盒子长、宽和高三个数据;复制函数实现对象的复制;volume函数计算并输出盒子的体积。在main函数中,要求创建Box对象,实现对象的复制,并求盒子的体积。

#include<iostream>
using namespace std;
class BOX
{
private:
	double length, width, height;
public:
	BOX(double l, double w, double h)
	{
		length = l;
		width = w;
		height = h;
	}
	BOX(const BOX& t) {
		length = t.length;
		width = t.width;
		height = t.height;
	}
	double getlength() { return length; }
	double getwidth() { return width; }
	double getheight() { return height; }

};
double volume(BOX box)
{
	return box.getlength() * box.getwidth() * box.getheight();
}
int main()
{
	BOX box1(1, 3, 5);
	cout << "box1的体积:" << volume(box1) << endl;
	return 0;
}
#include<iostream>
using namespace std;
class Box {
private:
	int length, width, highth;
public:
	Box(int l,int w,int h):length(l),width(w),highth(h){}
	Box(const Box& B) {
		length = B.length;
		width = B.width;
		highth = B.highth;
	}
	int Volume() {
		return length * width * highth ;
	}
};
int main() {
	Box box1(10, 20, 30);
	Box box2(box1);
	cout << "box1 volume: " << box1.Volume() << endl;
	cout << "box2 volume: " << box2.Volume() << endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值