1 //设计立方体类,求出立方体的面积和体积,分别用全局函数和成员函数判断两个立方体是否相等 2 #include <iostream> 3 using namespace std; 4 //立方体类设计 5 //1、创建立方体类 6 //2、设计属性 7 //3、设计行为 获取立方体面积和体积 8 //4、分别利用全局函数和成员函数,判断两个立方体是否相等 9 //bool函数,返回值为true和false,利用bool ret =来接收 10 class Cube 11 { 12 public: 13 void SetL(int l) 14 { 15 m_L = l; 16 } 17 int GetL() 18 { 19 return m_L; 20 } 21 void SetW(int w) 22 { 23 m_W = w; 24 } 25 int GetW() 26 { 27 return m_W; 28 } 29 void SetH(int h) 30 { 31 m_H = h; 32 } 33 int GetH() 34 { 35 return m_H; 36 } 37 //成员函数,判断是否相等,利用引用传递 38 bool isSameByClass(Cube &c) 39 { 40 if(m_L == c.GetL()&&m_W == c.GetW()&&m_H == c.GetH()) 41 { 42 return true; 43 } 44 return false; 45 } 46 int calculateS() 47 { 48 return 2*m_L*m_W+2*m_L*m_H+2*m_W*m_H; 49 } 50 int calculateV() 51 { 52 return m_L*m_W*m_H; 53 } 54 private: 55 int m_L; 56 int m_W; 57 int m_H; 58 }; 59 //全局函数判断是否相等,利用引用传递 60 bool isSame(Cube &c1,Cube &c2) 61 { 62 if(c1.GetL() == c2.GetL()&&c1.GetW() == c2.GetW()&&c1.GetH() == c2.GetH()) 63 { 64 return true; 65 } 66 return false; 67 } 68 int main() 69 { 70 Cube c1; 71 c1.SetL(10); 72 c1.SetW(10); 73 c1.SetH(10); 74 Cube c2; 75 c2.SetL(10); 76 c2.SetW(10); 77 c2.SetH(10); 78 cout<<"c1的面积为:"<<c1.calculateS()<<endl; 79 cout<<"c1的体积为:"<<c1.calculateV()<<endl; 80 bool ret = isSame(c1,c2); 81 if(ret) 82 { 83 cout<<"c1和c2相等"<<endl; 84 } 85 else 86 { 87 cout<<"c1和c2不相等"<<endl; 88 } 89 ret = c1.isSameByClass(c2); 90 if(ret) 91 { 92 cout<<"成员函数判断:c1和c2相等"<<endl; 93 } 94 else 95 { 96 cout<<"成员函数判断:c1和c2不相等"<<endl; 97 } 98 }
标签:10,return,int,2023.4,c2,立方体,15,c1 From: https://www.cnblogs.com/muzhaodi/p/17322198.html