LAB 10B - BOX CLASS
Concepts
Focus on three main concepts in this assignment: defining class constants, the difference between member variables vs. member functions, and the importance of private and public class members.
Member Functions
Consider a program that works with photographs. Photos have a height and a width. We might model such a class like this:
public:
Photo(); // Photo constructor function
int height;
int width;
};
Since photos have a height and a width, they also have an area. What is a photo's area? It's a value generated by multiplying a Photo object's height and width together. How might we enable our Photo class such that a Photo object is able to tell us its area? In other words, what do we need to do in order to be able to write code like the following?
cout << "The area of Photo p is" << p.Area() << endl;
To support the ability to write p.Area()
, we must define area as a member function, or "a function that belongs to objects of a class." We do this in two steps.
- Declare the function prototype in the header file
- Define the function implementation in the implementation file
In other words, in the header file photo.h, we should add:
public:
Photo(); // Photo constructor function
int Area(); // area member function
int height;
int width;
};
And in the implementation file, photo.cpp, we should add:
return height * width;
}
The function prototype in photo.h tells the machine, "Hey, Photo objects have a member function called area that returns an int." The function definition in photo.cpp tells the machine, "Hey, the definition of the Photo member function area() I told you about in photo.h works like this."
Look at the body of the area function above. Notice how the function, since it is declared "inside" the class, has direct access to everything else inside the class, such as height and width.
This is so important, we'd like you to read it again. Look at the body of the area function above. Notice how the function, since it is declared "inside" the class, has direct access to everything else inside the class, such as height and width.
When should you use member variables (e.g., height and width)
instead of member functions in your main.cpp
file? Today
you'll see one reason why most seasoned programmers use member functions.
Public vs. Private Members
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.
In Lab 10A, you defined a simple Money class that
used the public:
syntax to tell the machine that the
dollars
and cents
properties were "accessible from outside the object." For example,
consider the following code:
photo.h
public:
Photo();
int Area();
int height;
int width;
};
photo.cpp
height = 8;
width = 5;
}
That public:
thing you see in the header file tells the machine, "Hey computer, when a programmer instantiates a Photo object, they should be allowed to access the values of height and width." In other words, this mechanism allows you to do the following:
cout << myFamily.height; // accessing the value of the object's height
myFamily.height = 20; // assigning a value to the object's height
Do you see how you can access the height attribute of the Photo
myFamily
, as well as assign a new value to this data member?
More importantly, do you see the problem? Look at this:
Uh oh! Because the height attribute is public, any programmer using Photo objects can assign any integer value to that member variable, including ones that don't make sense, such as negative heights.
"Big whoop," you say, "I know that we can make data members private." And you'd be right (and deserve a gold star for paying attention). Let's change our class so that arbitrary values can't be assigned to data members.
photo.h
public:
Photo();
int Area();
private:
int height;
int width;
};
Ahh, there, now no one can assign values to Photo objects:
However, now you've introduced another problem. By declaring the data members to be private
, you've disabled all access to the member variables! This means that you can no longer do this:
The height attribute is declared to be private
, so there is no direct access to the attribute. Are you thinking, "I hate C++!"? Don't worry, here's how you can control access to data members: define member functions we call "getters and setters."
Accessors and Mutators, aka Getters and Setters
Let us restate the problem above: we do not want to allow direct access to member variables because invalid values can be assigned to them, causing the world to end (or, less dramatically, causing our program to be incorrect). We can declare a data member to be private, but then the programmer loses the ability to read the value of a data member as well.
The solution? Let us define two member functions that we use to manage reading and writing for each data member.
photo.h
public:
Photo();
int Area();
int GetHeight();
int GetWidth();
void SetHeight(int h);
void SetWidth(int w);
private:
int height;
int width;
};
photo.cpp
height = 8;
width = 5;
}
int Photo::GetHeight() {
return height;
}
int Photo::GetWidth() {
return width;
}
void Photo::SetHeight(int h) {
if (h > 0) {
height = h;
}
}
void Photo::SetWidth(int w) {
if (width > 0) {
width = w;
}
}
If you were to implement the code above, your programs could then do the following:
cout << "Height is " << p.getHeight();
p.setHeight(20); // height is now 20
p.setHeight(-69); // height is unchanged
Spend time looking at the code in photo.cpp above to see what those functions do. Notice how getHeight and getWidth are very similar. Almost every "getter" function you write will follow this pattern. Ditto for "setter" functions.
In order to understand how this works, you should realize that member functions have full access to everything declared inside the class; member functions are not subject to the public/private rules.
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!