创建立方体类,使用全局函数判断立方体是否相等
#include<iostream>
using namespace std;
class cube
{
public:
//设置长宽高
void setmL(int length)
{
mL = length;
}
void setmW(int width)
{
mW = width;
}
void setmH(int height)
{
mH = height;
}
//读取长宽高
int getmL()
{
return mL;
}
int getmW()
{
return mW;
}
int getmH()
{
return mH;
}
void cubeinint(int length,int width,int height)
{
mL = length;
mW = width;
mH = height;
/*cout << "立方体的长:" << mL << endl;
cout << "立方体的宽:" << mW << endl;
cout << "立方体的高:" << mH << endl;*/
}
int cubevolume(void) //体积
{
return mL * mW * mH;
}
int cubearea(void)//面积
{
return 2*(mL * mW + mH*mL+mW*mH);
}
private:
int mL;
int mW;
int mH;
};
//全局函数
bool mycomparecube(cube C1, cube C2)
{
if (C1.getmL() == C2.getmL() && C1.getmW() == C2.getmW() && C1.getmH() == C2.getmH())
{
return true;
}
return false;
}
void test()
{
cube cube1;
cube1.cubeinint(10,5,3);
//输出立方体的面积和体积
//cout << "立方体体积:" << cube1.cubevolume() << endl;
//cout << "立方体面积:" << cube1.cubearea() << endl;
cube cube2;
cube2.cubeinint(10, 5, 4);
//全局函数调用判断实例
if (mycomparecube(cube1, cube2) == true)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
}
int main()
{
test();
return 0;
}
输出结果