LAB 11C - BOX CLASS
Concepts
Today we focus on the const
concept.
In today's lab, you will define and implement a Box class such that it passes the test suite provided. We suggest you first explore the test functions to see the API that your Box class must support. For example, every Box instance must have a height, width, and depth; in addition, you must implement a default constructor and a non-default constructor.
Things That Are "Constant" Across All Instances of a Class
Let's say you went to the planet Womanz, where all living beings are female. (The planet Womanz is way better than Earth). If you created a Woman class to model beings from planet Womanz, how would you model the gender of Woman objects?
In this case, gender will always be "female" for every Woman instance. You might think of gender as being "constant" across all instances of Woman.
How do we tell the machine about a fact that is "universal" or should not change for every instance of a class? By declaring a class constant using a particular syntax.
public:
static const bool GENDER = true; // let true mean female, false mean male
};
To access the constant, you must remember that it is declared "inside" the class. Hence, from within the class you can access GENDER
directly. For example, consider an IsFemale()
member function:
return GENDER;
}
A bit of a silly function, but you see how the member function has direct access to the variable GENDER
? Again, this is because member functions have full access to everything declared inside the class.
In contrast, from outside the class, you must use the :: (scope resolution) operator. Here's an example.
By typing Woman::GENDER
you're telling the computer, "Give me the value of the GENDER variable that is static (unchanging) for the class."
The keyword static
means different things in different contexts. For now, just remember that when you want to define a constant in a class, you must use static const [datatype] [name] = value
.
Remember, the variable is an attribute of the class not instances of the class (objects). So the following isn't correct:
bool areWomenFemale = adaLovelace::GENDER;
areWomenFemale = adaLovelace.GENDER;
Classes can have constants that are "universal" for all instances. But such a constant is an attribute of the class, not instances of the class. In the above incorrect code, an attempt is made to access the GENDER attribute of a Woman object. Again, the class constant is accessed via the class:
Instructions
As mentioned previously, you need to define and implement a Box class in this lab
such that it passes the test suite provided. We suggest you first explore the test
functions to see the API that your Box class must support. Specifically, every Box
instance must have (1) a height, width, and depth that are private
(2) a default constructor and a non-default constructor, (3) accessor functions
(getters and setters) for all properties, (4) a member function called
volume
, and (5) a class variable (using static const
)
called DEFAULT_DIMENSION. (We will discuss static const
variables next week.)
An important part of this assignment is reading code, so be sure to
take a look at test.cpp
to see how your Box must behave. When
correct, your Box should pass all the unit tests, and you should not see
"FAILED" printed on the console.
To get started, download the project template
from here. To compile the project successfully,
you need to complete the following two steps. First, define your Box class prototype
in box.h
; the prototype needs to include the properties of the Box, the
static const
variable, the two constructors, and member function
prototypes for the six accessor functions and volume
. Second, for each
function, add an empty function definition in box.cpp
. Once these two
steps are done correctly, your project will compile/execute (but few tests
will PASS). Your job is then to get all tests to PASS. Have fun with your Box!