82 lines
1.3 KiB
C++
82 lines
1.3 KiB
C++
// 立方体类与应用
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
class Cube
|
|
{
|
|
public:
|
|
void init(int l, int w, int h)
|
|
{
|
|
this->a = l;
|
|
this->b = w;
|
|
this->c = h;
|
|
}
|
|
int S(); // 面积
|
|
int V(); // 体积
|
|
void show();
|
|
bool isEqual(Cube &tmp);
|
|
|
|
private:
|
|
int a; // 长
|
|
int b; // 宽
|
|
int c; // 高
|
|
};
|
|
|
|
int Cube::S()
|
|
{
|
|
return 2 * (a * b + a * c + b * c);
|
|
}
|
|
|
|
int Cube::V()
|
|
{
|
|
return a * b * c;
|
|
}
|
|
|
|
void Cube::show()
|
|
{
|
|
cout << "S = " << S() << ", V = " << V() << endl;
|
|
}
|
|
|
|
bool Cube::isEqual(Cube &tmp)
|
|
{
|
|
// if (this->a == tmp.a && this->b == tmp.b && this->c == tmp.c)
|
|
if (this->S() == tmp.S() && this->V() == tmp.V())
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
bool equalCube(Cube &c1, Cube &c2)
|
|
{
|
|
if (c1.S() == c2.S() && c1.V() == c2.V())
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
const char *resBool(bool b) // bool 数字值转换人类可读
|
|
{
|
|
if (b != 0)
|
|
return "true";
|
|
return "false";
|
|
}
|
|
|
|
int main()
|
|
{
|
|
Cube c1, c2, c3;
|
|
c1.init(2, 3, 4);
|
|
cout << "c1: " << endl;
|
|
c1.show();
|
|
|
|
c2.init(3, 4, 5);
|
|
cout << "c2: " << endl;
|
|
c2.show();
|
|
|
|
c3.init(4, 2, 3);
|
|
|
|
cout << "Cube::isEqual(c2) = " << resBool(c1.isEqual(c2)) << endl;
|
|
cout << "equalCube(c1, c2) = " << resBool(equalCube(c1, c3)) << endl;
|
|
|
|
return 0;
|
|
}
|