gpt4 book ai didi

C++ 派生类覆盖构造函数值更改

转载 作者:行者123 更新时间:2023-11-30 05:34:49 24 4
gpt4 key购买 nike

我无法根据以下代码在派生类中获取 2 个函数来更改派生类中的值:-

#include <iostream>
#include <iomanip> // math related, e.g. setprecision(8);

using namespace std;

// class with setter functions
class BoxVol{ // a 'base' class, a class inheriting this one is a 'derived' class having an 'is a BoxVol' relationship.
public: // private or protected
BoxVol(){ // constructor 1
height=2.0; width=2.0; length=2.0;
}
BoxVol(double w, double h, double l){ // constructor 2
width1=w; height1=h; length1=l;
cout << "From BoxVol constructor, width = " << width1 << endl;
}
void setWidth(double w){
width = w;
}
void setHeight(double h){
height = h;
}
void setLength(double l){
length = l;
}
// above, using default constructor, requires setters to set params
// Next uses a defined constructor where params are passed with the constructor declaration:

protected:
double width; double height; double length;
double width1; double height1; double length1;

};

struct X{
// ...
};

// another class with setter function
class BoxDensity{ // another base class

public: // default is private
BoxDensity(){ // constructor 1
density=1.0;
}
BoxDensity(double d){ //constructor 2
density=d;
cout << "From BoxDensity constructor, density = " << density << endl;
}
void setDensity(double d){
density=d;
}
protected: double density;
};

// derived (inheriting) class with getter functions
class BoxInfo: public BoxVol, public BoxDensity { // multiple inheritance
public:
double getVol(){
vol=width*height*length;
return vol;
}
double getDensity(){
d=getVol()*density;
return d;
}
double getVol1(double w, double h, double l){
BoxVol Box3Vol(w,h,l);
cout << "From getVol1(w,h,l), width = " << width1 << endl;
vol1=width1*height1*length1;
return vol1;
}
double getDensity1(double d){
BoxDensity Box3Density(d);
cout << "From getDensity1(d), density = " << density << endl;
d1=vol1*density;
return d1;
}
protected:
double vol,vol1,d,d1;
};

int main( ){
BoxInfo Box1Specs, Box2Specs, Box3Specs; // Declare 2 instances of BoxInfo
double w=1.0,h=1.0,l=1.0,d=1.0;
double volume=1.0, boxwt=1.0;
// private and protected members can not be accessed directly using direct member access operator (.)
Box1Specs.setHeight(5.0); Box1Specs.setLength(6.0); Box1Specs.setWidth(7.0);
Box1Specs.setDensity(2.1);
volume = Box1Specs.getVol(); boxwt=Box1Specs.getDensity();
cout << endl;
cout << "Volume of Box 1 : " << volume << " cubic cm; Weight of Box1: " << boxwt << " grams."<<endl;
Box2Specs.setHeight(15.0); Box2Specs.setLength(16.0); Box2Specs.setWidth(17.0);
volume = Box2Specs.getVol();
cout << "Volume of Box 2 : " << volume <<endl;

setprecision(8);
cout << endl;
cout << "For Box 3 enter values for: width height length, spaced -> ";
cin >> w; cin >> h; cin >> l;
cout << "width: " << w << ", height: " << h << ", length: " << l << endl;
cout << endl;
cout << "For Box 3, enter it's density -> "; cin >> d;
cout << "Density: " << d << endl;
cout << endl;
volume=Box3Specs.getVol1(w,h,l); boxwt=Box3Specs.getDensity1(d);
cout << endl;
cout << "Volume of Box 3 : " << volume << " cubic cm." << endl;
cout << "Weight of Box 3: " << boxwt << " grams." <<endl;

return 0;
}

当我使用原始变量(宽度、高度、长度、密度)时,将使用默认构造函数中的值,而不是其他构造函数中的值。如果我创建新变量,如将 1 添加到上面的原始名称,那么返回的是一些双最大值或最小值。有人可以运行这段代码来亲自查看输出,然后看看他/她是否能发现其中的谬误。谢谢。

最佳答案

程序的简短分析是这样的:它有未定义的行为。当您调用 Box3Specs.getVol1(w, h, l) 时,将使用默认构造的对象。默认构造函数仅设置成员 widthheightlength 但不设置 width1height1,或 length1。函数 getVol1() 使用后三个。由于这些未初始化,因此行为未定义。

getVol1() 确实构造了一个对象 Box3Vol,该对象将被正确初始化但未被使用。您可能打算使用类似的东西

*this = Box3Vol(w, h, l);

您应该查看什么是成员初始值设定项列表,我认为您需要摆脱重复的成员集:它们似乎会导致对BoxVol 的混淆实体是。由于没有描述它的含义,因此无法确定。

关于C++ 派生类覆盖构造函数值更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com