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++.
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++.