|
本帖最后由 1603611958 于 2021-12-2 15:34 编辑
#include<iostream>
using namespace std;
class Box
{
public:
Box() {};
//有参构造函数
Box(double leng,double brea,double heig) {
this->length = leng;
this->breadth = brea;
this->height = heig;
}
//获取体积
double getVolume(void)
{
return length * breadth * height;
}
// 重载 + 运算符,用于把两个 Box 对象相加
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
int main() {
Box Box1(6.0, 7.0, 5.0); // 声明 Box1,类型为 Box
Box Box2(12.0,13.0,10.0); // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
cout << "Volume of Box1 : " << Box1.getVolume() << endl;
cout << "Volume of Box2 : " << Box2.getVolume() << endl;
// 把两个对象相加,得到 Box3
Box3 = Box1 + Box2;
cout << "Volume of Box3 : " << Box3.getVolume() << endl;
return 0;
}
把两个对象相加,怎么只传了一个对象进去?为什么不是传两个对象?
我想再增加一个对象相加,把改成3个对象相加系统为什么还不允许?
哪位大神有空和我说说这个怎么理解;
成员版本所需的参数少一个,因为其中一个操作数是被隐式地传递的调用对象。this对象。
|
上一篇: 再次请教老师?下一篇: 求助 C语言问题。 在C语言 123.456×3≠370.368
|