Need help with Constructors?

Hello all. Im trying to creat two box in this program using the default constructor. When i call to try and display the info, it says that x, y, and z are not declared in this scope. any idea why? also, lets say i wanted to have the user cin the length, height, and width using the void setBox function, how would i be able to do that? thanks for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;

class Box{
public:
   Box();
   void setBox(float x, float y, float z, string color);
   float Volume();
   bool CheckBox(bool x);
   bool isACube(bool);
   bool compareBox(bool);
   float displayNum(float x, float y, float z);
   string displayColor (string color);

private:
    float l, h, b;
    float volume;
    string color;
};

int main (){
 Box b1(), b2();
 b2.displayNum(x, y, z);
 return 0;


}

Box::Box(){
    l=1;
    h=1;
    b=1;
    color="black";
}

void Box::setBox(float x, float y, float z, string c){
    l=x;
    h=y;
    b=z;
    color=c;
}

float Box::Volume(){
    volume= l*h*b;
}

bool Box::CheckBox(bool x) {
    if ((l>0) && (h>0) && (b>0) && (color== "yellow" || "red" || "black" || "blue"))
     x=true;
    else
     x=false;
 }

bool Box::isACube(bool y) {
    if ((l==h) && (l==b))
     y=true;
    else
     y=false;
}

float Box::displayNum(float x, float y, float z) {
    cout << "The length, height, and width are as follows (respectively): " << x << " " << y << " " << z << "." << endl;
}

string Box::displayColor(string color) {
    cout << "The color is: " << color << "." << endl;
}

Last edited on
1.) The Box objects declared on line 24 aren't actually objects, but function prototypes. Remove the parentheses.

2.) The if control structure on line 50 will always execute. The reason for this is that the individual conditions "red", "black" and "blue" are not being compared against anything explicitly, and will therefore be implicitly compared to true.
Since any of these strings are considered to be non-zero, the expression evaluates to true.
Computers don't think, so you need to be explicit with what you want to happen:

if ((l>0) && (h>0) && (b>0) && (color == "yellow" || color == "red" || color == "black" || color =="blue"))
Last edited on
i was able to get it working, had a few stupid errors thanks for the help though
Topic archived. No new replies allowed.