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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序