No_goddess 2016-09-25 12:15 采纳率: 33.3%
浏览 933

c++类中如何对函数进行获取和更改

类中
set函数

get函数
的结合如何使数据进行查看和更改,请大致写出思路,谢谢帮助
#include
using namespace std;
class Box
{
public:

Box(float = 1, float = 1, float = 1);

virtual ~Box();

void Show();

private:
float length, width, height;
};

#include "Box.h"
using namespace std;

/*
Box::Box()
{
length = 1; width = 1; height = 1;
cout << "Box("<<length<<","<<width<<","<<height<<")";
cout << "is constructed!" << endl;
}

*/

Box::Box(float L,float W,float H)
{
length = L; width = W; height = H;
cout << "Box(" << length << "," << width << "," << height << ")";
cout << "is constructed!" << endl;
}

void Box::Show()
{
cout << "length:" << length << endl;
cout << "width:" << width << endl;
cout << "height:" << height << endl;
cout <<"tiji" << length*width*height << endl;
cout << "mianji" << (2*((length*width)+(width*height)+(length*height))) << endl;
}

Box::~Box()
{
cout << "Box(" << length << "," << width << "," << height << ")";
cout << "is constructed!" << endl;
}

#include
#include "Box.h"
using namespace std;

int main()
{
Box box1;
box1.Show();
box1.~Box();

Box box2(4,2,3);
box2.Show();


system("pause");

}

  • 写回答

3条回答

  • leewers 2016-09-25 13:55
    关注
    float Box::getLength(){
        return length;
    }
    
    float Box::getHeight(){
        return height;
    }
    
    float Box::getWidth(){
        return width;
    }
    
    void Box::setLength(float length){
        this->length = length;;
    }
    
    void Box::setHeight(float height){
        this->length = height;
    }
    
    void Box::setWidth(float width){
        this->length = width;
    }
    
    void Box::set(float length = 1, float width = 1, float height = 1){
        this->length = length;
        this->width = width;
        this->height = height;
    }
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)