1 #include<iostream> 2 using namespace std; 3 4 class Cube 5 { 6 public: 7 8 int func_v() 9 { 10 return m_H * m_W * m_L; 11 } 12 void func_g(int h, int w, int l) 13 { 14 m_H = h; 15 m_W = w; 16 m_L = l; 17 } 18 bool define(Cube &c) 19 { 20 if (c.func_v() == m_H * m_W * m_L) 21 { 22 return true; 23 } 24 else 25 { 26 return false; 27 } 28 } 29 private: 30 int m_H; 31 int m_W; 32 int m_L; 33 34 }; 35 36 int main() 37 { 38 Cube c1; 39 Cube c2; 40 c1.func_g(2, 3, 4); 41 c2.func_g(2, 3, 4); 42 43 if (c1.define(c2)) 44 { 45 cout << "same" << endl; 46 } 47 else 48 { 49 cout << "differ" << endl; 50 } 51 system("pause"); 52 return 0; 53 }
注意func_g和func_v的定义顺序谁先谁后都行。
函数的形参尽量用引用,节省空间