#include<iostream>
using namespace std;
class Cube {
public://行为
void setm_L(int L)
{
m_L = L;
}
int showm_L()
{
return m_L;
}
void setm_W(int W)
{
m_L = W;
}
int showm_W()
{
return m_W;
}
void setm_H(int H)
{
m_H = H;
}
int showm_H()
{
return m_H;
}
bool IsSamebyclass(Cube& c2)//成员函数判断
{
if (showm_H() == c2.showm_H() && showm_W() == c2.showm_W() && showm_L() == c2.showm_L())
{
return true;
}
return false;
}
//属性
private:
int m_L;
int m_W;
int m_H;
};
bool IsSame(Cube &c1,Cube &c2)//没有&是值传递,用引用的方式传递就不会再拷贝出一份数据,而是用原来的
{
if (c1.showm_H() == c2.showm_H() && c1.showm_W() == c2.showm_W() && c1.showm_L() == c2.showm_L())
{
return true;
}
return false;
}//全局函数判断
int main()
{
Cube c1;
Cube c2;
c1.setm_L(10);
c1.setm_W(10);
c1.setm_H(10);
c1.showm_L();
c1.showm_W();
c1.showm_H();
c2.setm_L(10);
c2.setm_W(10);
c2.setm_H(10);
bool ret=IsSame(c1, c2);
if (ret) cout << "一样" << endl;
else cout << "不一样" << endl;
bool ret2 = c1.IsSamebyclass(c2);
if (ret2) cout << "class一样" << endl;
else cout << "class不一样" << endl;
return 0;
}
【C++学习】 设计案例1:设计立方体类
最新推荐文章于 2024-10-09 10:07:13 发布