正方体的体积和面积以及判断两个正方体是否相同。
#include#include using namespace std; class Cube { public: void setL(int l) { C_L = l; } int getL() { return C_L; } void setW(int w) { C_W = w; } int getW() { return C_W; } void setH(int h) { C_H = h; } int getH() { return C_H; } int calculatrS() { return 2 * C_L * C_W + 2 * C_L * C_H + 2 * C_W * C_H; } int calculatrV() { return C_L * C_W * C_H; } bool issameByClass(Cube &c) { if (C_H== c.getH() && C_W == c.getW() && C_L == c.getL()) return true; else return false; } private: int C_L; int C_W; int C_H; }; bool issame(Cube& c1, Cube& c2) { if (c1.getH() == c2.getH() && c1.getW() == c2.getW() && c1.getL() == c2.getL()) return true; else return false; } int main() { Cube c1; c1.setL(10); c1.setW(10); c1.setH(10); cout <<"c1的表面积为:" << c1.calculatrS() << endl; cout << "c1的体积为:" << c1.calculatrV() << endl; Cube c2; c2.setL(10); c2.setW(10); c2.setH(10); bool ret1 = issame(c1, c2); if (ret1) cout << "c1和c2相等" << endl; else cout << "c1和c2不相等" << endl; bool ret2 = c1. issameByClass(c2); if (ret2) cout << "利用成员函数,c1和c2相等" << endl; else cout << "利用成员函数,c1和c2不相等" << endl; system("pause"); return 0; }



