案例一
![image](https://img2020.cnblogs.com/blog/2634461/202111/2634461-20211124092418707-454607259.png)
# include<iostream>
using namespace std;
class Cube{
public:
void setL(int l){
m_L = l;
}
void setW(int w){
m_W = w;
}
void setH(int h){
m_H = h;
}
int getL(){
return m_L;
}
int getW(){
return m_W;
}
int getH(){
return m_H;
}
int calculateS(){
return 2*m_L*m_W + 2*m_W*m_H + 2*m_L*m_H;
}
int calculateV(){
return m_L*m_W*m_H;
}
// 利用成员函数判断两个立方体体积是否相等
// 注意:由于是成员函数,是由一个对象调用的,所以只要传入一个参数 -----------------
bool isVSameByClass(Cube &c){
if (calculateV() == c.calculateV()){
return true;
}
else{
return false;
}
}
private:
int m_L; // 长
int m_W; // 宽
int m_H; // 高
};
// 利用全局函数判断两个立方体体积是否相等
bool isVSame(Cube &c1, Cube &c2){
// 用的是引用 是地址传递
if (c1.calculateV() == c2.calculateV()){
return true;
}
else{
return false;
}
}
int main(){
// 创建立方体对象
Cube c1;
c1.setL(10);
c1.setW(10);
c1.setH(10);
cout << "c1的面积为:"<< c1.calculateS() << endl;
cout << "c1的体积为:"<< c1.calculateV() << endl;
Cube c2;
c2.setL(10);
c2.setW(10);
c2.setH(10);
cout << "c1的面积为:"<< c1.calculateS() << endl;
cout << "c1的体积为:"<< c1.calculateV() << endl;
// 利用全局函数判断两个立方体体积是否相等
bool result = isVSame(c1,c2);
if (result){
cout << "c1 c2体积相等" << endl;
}
else{
cout << "c1 c2不体积相等" << endl;
}
// 利用成员函数判断两个立方体体积是否相等
bool result2 = c1.isVSameByClass(c2);
if (result){
cout << "c1 c2体积相等" << endl;
}
else{
cout << "c1 c2不体积相等" << endl;
}
return 0;
}