练习案例:设计立方体类
1.设计立方体类(cube)
2.求出立方体的面积和体积
3.分别用全局函数和成员函数判断两个立方体是否相等
#include <iostream>
using namespace std;
class Cube
{
public:
//设置长,宽,高
Cube(int m_L, int m_W, int m_H)
{
this->m_H = m_H;
this->m_L = m_L;
this->m_W = m_W;
}
//获取立方体面积
int calculateS()
{
return 2 * m_H * m_L + 2 * m_H * m_W + 2 * m_L * m_W;
}
//获取立方体体积
int calculateV()
{
return m_H * m_L * m_W;
}
bool isSamme_V(Cube& cu)
{
if (cu.calculateV() == calculateV())
{
return true;
}
return false;
}
private: //属性用私有
int m_L; //长
int m_W; //宽
int m_H; //高
};
bool isSamme_S(Cube &cu1,Cube &cu2)
{
if (cu1.calculateS() == cu2.calculateS())
{
return true;
}
return false;
}
int main()
{
Cube cu(2,3,5);
cout << "S: " << cu.calculateS() << endl;
cout << "V: " << cu.calculateV() << endl;
Cube cu1(4, 5, 6);
cout << "S: " << cu1.calculateS() << endl;
cout << "V: " << cu1.calculateV() << endl;
cout << "cu S == cu1 S :" << isSamme_S(cu,cu1) << endl;
cout << "cu v == cu1 v :" << cu.isSamme_V(cu1) << endl;
return 0;
}