设计长方体类
1 //设计立方体类(Cube),求出立方体的面积(2 * a*b + 2 * a*c + 2 * b*c)和体积(a * b * c), 2 //分别用全局函数和成员函数判断两个立方体是否相等。 3 #include <iostream> 4 using namespace std; 5 class Cub 6 { 7 public: 8 void setL(int l) { mL = l; } 9 void setW(int w) { mW = w; } 10 void setH(int h) { mH = h; } 11 int getL() { return mL; } 12 int getW() { return mW; } 13 int getH() { return mH; } 14 //立方体的面积 15 int caculateS() 16 { 17 return (mL*mW + mL * mH + mH * mW) * 2; 18 } 19 //立方体体积 20 int caculateV() 21 { 22 return mW * mL*mH; 23 } 24 //成员方法 25 bool CubCompare(Cub &c) 26 { 27 if (getL()==c.getL()&&getW()==c.getW()&&c.getH()==getH()) 28 { 29 return true; 30 } 31 else 32 { 33 return false; 34 } 35 } 36 private: 37 int mL; 38 int mW; 39 int mH; 40 }; 41 //比较两个立方体是否相等 42 bool CubCompare(Cub &c1, Cub &c2) 43 { 44 if (c2.getL() == c1.getL() && c2.getW() == c1.getW() && c1.getH() == c2.getH()) 45 { 46 return true; 47 } 48 else 49 { 50 return false; 51 } 52 } 53 void test0() 54 { 55 Cub c1, c2; 56 c1.setH(10); 57 c1.setL(20); 58 c1.setW(30); 59 60 c2.setH(10); 61 c2.setL(10); 62 c2.setW(30); 63 cout << "c1的面积:" << c1.caculateS() << "体积:" << c1.caculateV() << endl; 64 cout << "c2的面积:" << c2.caculateS() << "体积:" << c2.caculateV() << endl; 65 66 //比较两个立方体是否相等 67 if (CubCompare(c1,c2)) 68 { 69 cout<<"c1和c2相等"<<endl; 70 } 71 else 72 { 73 cout<<"c1和c2不相等"<<endl; 74 } 75 if (c1.CubCompare(c2)) 76 { 77 cout << "c1和c2相等" << endl; 78 } 79 else 80 { 81 cout << "c1和c2不相等" << endl; 82 } 83 } 84 int main() 85 { 86 test0(); 87 return 0; 88 }
道阻且长,行则将至