#include<iostream>
using namespace std;
//长方体类
class Cuboid
{
public:
void setABC(int a, int b,int c)
{
m_a = a;
m_b = b;
m_c = c;
}
int getArea()
{
return (m_a*m_b+m_a*m_c+m_b*m_c)*2;
}
int getVolume()
{
return (m_a*m_b*m_c);
}
int getA()
{
return m_a;
}
int getB()
{
return m_b;
}
int getC()
{
return m_c;
}
bool judgeCuboid(Cuboid &c2)//使用引用,没有值拷贝动作
{
if (m_a == c2.m_a&&//可以改写为c2.m_a,因为同类之间没有私处
m_b == c2.getB()&&
m_c == c2.getC())
return true;
else
return false;
}
private:
int m_a;
int m_b;
int m_c;
};
//全局函数
bool judgeCuboid(Cuboid &c1,Cuboid &c2)//使用引用,没有值拷贝动作
{
if (c1.getA() == c2.getA()&&
c1.getB() == c2.getB()&&
c1.getC() == c2.getC())
return true;
else
return false;
}
int main()
{
Cuboid c1;
c1.setABC(10,20,30);
Cuboid c2;
c2.setABC(10,20,30);
cout << "c1的体积是" << c1.getVolume() << endl;
cout << "c1的体积是" << c1.getArea() << endl;
//全局函数的方法
if (judgeCuboid(c1,c2))
cout << "c1与c2是两个相等的长方体" << endl;
else
cout << "c1与c2是两个不相等的长方体" << endl;
cout << "------------------------------" << endl;
//面向对象的方法
if (c1.judgeCuboid(c2))
cout << "c1与c2是两个相等的长方体" << endl;
else
cout << "c1与c2是两个不相等的长方体" << endl;
return 0;
}
2.10 求两个长方体是否相等
最新推荐文章于 2024-05-15 16:59:37 发布