面向过程编程向面向对象编程转换——比较两个长方体是否相等
1 #include <iostream> 2 using namespace std; 3 class Cube 4 { 5 public: 6 void setA(int a) 7 { 8 m_a = a; 9 } 10 void setB(int b) 11 { 12 m_b = b; 13 } 14 void setC(int c) 15 { 16 m_c = c; 17 } 18 void setABC(int a, int b, int c) 19 { 20 m_a = a; 21 m_b = b; 22 m_c = c; 23 } 24 int getV() 25 { 26 m_v = m_a*m_b*m_c; 27 return m_v; 28 } 29 int getA() 30 { 31 return m_a; 32 } 33 int getB() 34 { 35 return m_b; 36 } 37 int getC() 38 { 39 return m_c; 40 } 41 int judge(Cube &v1, Cube &v2) 42 { 43 44 if ((v1.getA() == v2.getA()) && (v1.getB() == v2.getB()) && (v1.getC() == v2.getC())) 45 { 46 return 1; 47 } 48 else 49 { 50 return 0; 51 } 52 } 53 int judge(Cube &v2)//成员函数 函数重载 54 { 55 //此时的这个场景下的m_a是属于v1还是v2?---属于v1,谁调用属于谁 56 if ((m_a==v2.getA())&&(m_b==v2.getB())&&m_c==v2.getC()) 57 { 58 return 1; 59 } 60 else 61 { 62 return 0; 63 } 64 } 65 66 private: 67 int m_a; 68 int m_b; 69 int m_c; 70 int m_s; 71 int m_v; 72 73 }; 74 75 //全局函数PK成员函数 76 77 //全局函数 78 int judge(Cube &v1,Cube &v2) 79 { 80 if ((v1.getA()==v2.getA())&&(v1.getB()==v2.getB())&&(v1.getC()==v2.getC())) 81 { 82 return 1; 83 } 84 else 85 { 86 return 0; 87 } 88 } 89 90 //面向对象 91 void main() 92 { 93 Cube V1, V2; 94 V1.setABC(1, 2, 3); 95 V2.setABC(1, 2, 4); 96 cout << V1.getV() << endl; 97 // 98 int tag = V1.judge(V2); 99 if (tag==1) 100 { 101 cout << "相等" << endl; 102 } 103 else 104 { 105 cout << "不等" << endl; 106 } 107 //V1.judge(V1);//判断v1立方体和v2立方体是否相等 通过类的成员函数 108 109 110 /* 111 V1.judge(V1, V2); 112 if (judge(V1, V2) == 1) 113 { 114 cout << "相等" << endl; 115 } 116 else 117 { 118 cout << "不等" << endl; 119 } 120 */ 121 system("pause"); 122 return; 123 } 124 125 void main_全局函数调用面向过程() 126 { 127 Cube V1,V2; 128 V1.setABC(1, 2, 3); 129 V2.setABC(1, 2, 4); 130 cout<<V1.getV()<<endl; 131 if (judge(V1,V2)==1) 132 { 133 cout << "相等" << endl; 134 } 135 else 136 { 137 cout << "不等" << endl; 138 } 139 system("pause"); 140 return; 141 }