【C++学习】 设计案例1:设计立方体类

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值