C ++派生类重写构造函数值更改

时间:2015-12-04 10:44:05

标签: c++ constructor derived

我无法在派生类中获得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一样,那么返回的是一些double max或min值。 有人可以运行此代码来查看自己的输出,然后看看他/她是否可以发现其中的谬误。谢谢。

2 个答案:

答案 0 :(得分:1)

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

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

的内容
*this = Box3Vol(w, h, l);

您应该查看成员初始化列表是什么,我认为您需要摆脱重复的成员集:它们似乎会导致BoxVol实体的混淆。由于没有描述它的意义,但不可能确定无疑。

答案 1 :(得分:0)

在派生类的“成员初始化列表”中调用基类构造函数。

BoxInfo(double w, double h, double l,double d):BoxVol(w,h,l),BoxDensity(d)

使用成员初始化列表,可以将值更改为基类构造函数。请记住,首先调用基类构造函数。如果没有成员初始化列表,则调用base的默认构造函数,您无法更新值。