小霉霉 2021-09-05 15:04 采纳率: 91.7%
浏览 24
已结题

这个哪里出现问题了呢……

#include
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 calulateS()//获取立方体面积
{
return 2m_lm_w+2m_lm_h+2m_hm_w;
}
int calulateV()//获取立方体体积
{
return m_lm_wm_h;
}
//利用成员函数判断两个立方体是否相等
bool issamebyclass(cube &c)//传入一个参数即可,和函数内部的值进行比较
{
if(m_l==c.getl()&&m_w==c.getw()&&m_h==
c.geth())//这里可以直接访问m_l,因为类内可以访问
{
return true;
}
else
{
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())//在这里set等都是函数,后面记得加括号
{
return true;
}
return false ;//如果不加上这一句,可能没有返回值
}
int main()
{
cube c1;
c1.setl(10);
c1.setw(20);
c1.seth(5);
cout<<c1.calulateS()<<" "<<c1.calulateV();//这里要加上在什么对象下的函数
cube c2;
c2.setl(10);
c2.setw(20);
c2.seth(5);
bool res=issame(c1,c2);
if(res)
{
cout<<"两个立方体是相等的";
}
else
{
cout<<"两个立方体是不相等的";
}
bool res=c1.issamebyclass(c2);
if(res)
{
cout<<"两个立方体是相等的";
}
else
{
cout<<"两个立方体是不相等的";
}
}

  • 写回答

1条回答 默认 最新

  • 幸福快乐着 2021-09-06 16:40
    关注
    
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    class cube
    {
    private:
        int m_l;
        int m_w;
        int m_h;
    public://使设置的长宽高公共化
        void setl(int l);//设置长
        int getl();//获取长
        void setw(int w);
        int getw();
        void seth(int h);
        int geth();
        int calulateS();//获取立方体面积
        int calulateV();//获取立方体体积
        //利用成员函数判断两个立方体是否相等
        bool issamebyclass(cube &c);//传入一个参数即可,和函数内部的值进行比较;
        //利用全局函数判断两个立方体是否相等
        bool issame(cube &c1, cube &c2);//引用是用的原始数据,不是拷贝的数据
    };
    
    int main()
    {
        cube c1;
        c1.setl(10);
        c1.setw(20);
        c1.seth(5);
        cout << c1.calulateS() << " " << c1.calulateV();//这里要加上在什么对象下的函数
        cube c2;
        c2.setl(10);
        c2.setw(20);
        c2.seth(5);
        bool res = c1.issame(c1, c2);
        if (res)
        {
            cout << "两个立方体是相等的";
        }
        else
        {
            cout << "两个立方体是不相等的";
        }
        bool res = c1.issamebyclass(c2);
        if (res)
        {
            cout << "两个立方体是相等的";
        }
        else
        {
            cout << "两个立方体是不相等的";
        }
    }
    
    void cube::setl(int l) //设置长
    {
        m_l = l;
    }
    
    int cube::getl() //获取长
    {
        return m_l;
    }
    
    void cube::setw(int w)
    {
        m_w = w;
    }
    
    int cube::getw()
    {
        return m_w;
    }
    
    void cube::seth(int h)
    {
        m_h = h;
    }
    
    int cube::geth()
    {
        return m_h;
    }
    
    int cube::calulateS() //获取立方体面积
    {
        return 2 * (m_l*m_h) + 2 * (m_h*m_w) + 2 * (m_l*m_w);
    //    return 2m_lm_w + 2m_lm_h + 2m_hm_w;
    }
    
    int cube::calulateV() //获取立方体体积
    {
        return m_h*m_l*m_w;
    }
    
    bool cube::issamebyclass(cube &c) //传入一个参数即可,和函数内部的值进行比较
    {
        if (m_l == c.getl() && m_w == c.getw() && m_h ==
            c.geth())//这里可以直接访问m_l,因为类内可以访问
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    bool cube::issame(cube &c1, cube &c2) //引用是用的原始数据,不是拷贝的数据
    {
        if (c1.getl() == c2.getl() && c1.getw() == c2.getw() && c1.geth() == c2.geth())//在这里set等都是函数,后面记得加括号
        {
            return true;
        }
        return false;//如果不加上这一句,可能没有返回值
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月16日
  • 已采纳回答 9月8日
  • 创建了问题 9月5日

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看