Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Constructors

Status
Not open for further replies.

blazero

Programmer
Feb 7, 2004
9
US
I'm a newb to C++, but am experienced enough to know that something isn't right when constructors cause distortion in variable values.

I am just trying out constructors with a simple "box" class:

class box {
double length;
double width;
double height;

double volume() {
return length*width*height;
}
};

I made an instance, declared as

box bPack = { 80.0, 50.0, 40.0 };

and the values were fine and output was successful, volume was 160000.

With the constructor, as follows:

class box {
double length;
double width;
double height;

box(double lengthV, double widthV, double heightV) {
length=lengthV;
width=widthV;
height=heightV;
}

double volume() {
return length*width*height;
}
};

Caused pure insanity. length is an arbitrary negative number between -800000 and the maximum for an ordinary long variable. width is always zero. height is just like length, but sometimes positive. Here's the really weird part: the volume is a non-zero value every time, despite width's value.

Does anyone know what is wrong? This issue is greatly hindering my learning of C++.
 
Hi,

You would better have posted your question in a tek-tip C++ forum(Microsoft or Unix).

ok, firstly you should set your class' attributes as private, and your routine(creator and length) as public, and initialize the attributes inside the initialization list clause :

Code:
class box
{
public:
    box(double lengthV, double widthV, double heightV) :
      length (lengthV),
      width (widthV),
      height (heightV)
    {
    }

    double volume() {
        return length*width*height;
    }

private:
    double length;
    double width;
    double height;
};

Your first way to instantiate is weird, and it's not the normal manner to follow, try this :

Code:
#include <assert.h>
//...
box bPack (80.0, 50.0, 40.0);//call to the constructor
//check for volume correctness :
assert (box.volume () == 160000.0);

You should also define accessors and setters for the three attributes of your box class, and organize features by categories(just a matter of style), like :
Code:
class box
{
public://-- creation
    box(double lengthV, double widthV, double heightV) :
      length (lengthV),
      width (widthV),
      height (heightV)
    {
    }

public://-- access
    double volume() {
        return length*width*height;
    }

    double length () const
    {
       return length;
    }

    double width () const
    {
       return width;
    }

    double height () const
    {
       return height;
    }

public://-- element change
    void set_length (double lengthV)
    {
       length = lengthV;
    }

    //etc. same for width, height

private://-- attributes
    double length;
    double width;
    double height;
};


--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top