0%

设计立方体类

设计一个立方体类(cube)

求出立方体的面积和体积

分别用全局函数和成员函数判断两个立方体是否相等

image-20220806224913962


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!--more-->

#include <iostream>
using namespace std;


class cube
{
public:
void setl(int l)
{
m_l = l;
}

int getl()
{
return m_l;
}

void setw(int w)

{
m_w=w;
}

int getw()
{
return m_w;
}

void seth(int h)
{
m_h = h;
}

int geth()

{
return m_h;
}

int S()
{
return 2 * m_h * m_l + 2 * m_w * m_h + 2 * m_l * m_w;
}

int V()
{
return m_l * m_w * m_h;
}

//利用成员函数判断两个立方体是否相等
bool isSame1(cube& C2)
{
if (m_l == C2.getl() && m_w == C2.getw() && m_h == C2.geth())
{
return true;
}
return false;
}
//属性尽量设置为私有
private:
int m_l;
int m_w;
int m_h;

};

//利用全局函数判断两个立方体是否相等
bool isSame(cube& C1, cube& C2)
{
if (C1.getl() == C2.getl() && C1.getw() == C2.getw() && C1.geth() == C2.geth())
{
return true;
}
return false;
}
int main()
{
cube C1;
C1.setl(10);
C1.seth(10);
C1.setw(10);

cout << "体积" << C1.V() << endl;
cout << "面积" << C1.S() << endl;

cube C2;
C2.seth(10);
C2.setl(10);
C2.setw(10);

bool ret = isSame(C1, C2);
if (ret)
{
cout << "C1和C2是相等的" << endl;
}
else
{
cout << "C1和C2是不相等的" << endl;
}
isSame(C1, C2);

ret = C1.isSame1(C2);
if (ret)
{
cout << "C1和C2是相等的" << endl;
}
else
{
cout << "C1和C2是不相等的" << endl;
}
C1.isSame1(C2);
system("pause");
return 0;
}