#includeusing namespace std; class cu{ //创建立方体类 public: //行为 //设置获取长宽高 void setm_L(int L) {//设置长 m_L = L; } int getm_L() {//获取长 return m_L; } void setm_W(int W) {//设置宽 m_W = W; } int getm_W() {//获取宽 return m_W; } void setm_H(int H) {//设置高 m_H = H; } int getm_H() {//获取高 return m_H; } int SS() {//获取立方体面积 return 2 * m_H * m_L + 2 * m_H * m_W + 2 * m_W * m_L; } int VV() {//获取立方体面积 return m_L*m_H*m_W; } //获取立方体体积 //利用成员函数判断立方体是否相等 bool issamebyclass(cu &c) { if (m_L == c.getm_L() && m_H == c.getm_H() && m_W == c.getm_W()) { return true; } else { return false; } } private: //属性 int m_H; int m_L; int m_W; }; //利用全局函数判断两个立方体是否相等 bool issame(cu &c1,cu &c2) { if (c1.getm_L() == c2.getm_L() && c1.getm_H() == c2.getm_H() && c1.getm_W() == c2.getm_W()) { return true; } else { return false; } } int main() { //创建立方体对象 cu c1; c1.setm_H(10); c1.setm_W(10); c1.setm_L(10); cout << "c1的面积为" << c1.SS() << endl; cout << "c1的体积为" << c1.VV() << endl; //创建第二个立方体 cu c2; c2.setm_H(10); c2.setm_W(11); c2.setm_L(10); bool ret = issame(c1, c2);//利用全局函数判断 if (ret) { cout << "相等" << endl; } else { cout << "不相等" << endl; } ret = c1.issamebyclass(c2);//利用成员函数判断 if (ret) { cout << "成员函数判断结果相等" << endl; } else { cout << "成员函数判断结果不相等" << endl; } system("pause"); }



