Homework 26: Box Test
Concepts
Today you will define what it means to be a "Box" to the machine. Focus on two main concepts for this assignment: how to define classes with public member variables and how to define and implement constructors.
Programming to the Domain
When we say the word "domain" we really mean "subject matter" or "topic." What then, does it mean to "program to the domain?" Consider these two snippets of code:
int alicePages = 200;
string favorite = "Great Mambo Chicken and the Transhuman Condition";
int favoritePages = 238;
cout << aliceTitle << " " << alicePages;
Notice how this first snippet above is really about programming with strings and ints.
Book favorite("Great Mambo Chicken and the Transhuman Condition", 238);
cout << alice << endl;
Notice how this second snippet is really more domain-specific. It's not about strings and ints, which have nothing to do with books. This code is about books.
Whenever possible, empower yourself by writing programs at higher levels of abstraction that are more specific to the domain of the problem you are trying to solve or model. Wherever possible, create programs about pipes, water, money, books, hydraulics, gates, bridges and zombies -- not about chars, ints and strings.
Classes
In lecture today you learned about C++ classes. Note that classes are the primary mechanism in OO languages that allow you to define more abstract datatypes (like tables, books, boxes, and zombies) to the machine, and thereby create programs that use these abstractions. Today, focus on your ability to define classes using an header file (ending in .h
) and an implementation file (ending in .cpp
).
Perhaps the most tricky thing to remember about the syntax for class definitions is the trailing semicolon at the end of your class definition's closing brace.
}
};
There you have it, the world's most simple definition of a Car, which a C++ compiler can comprehend.
Member Variables aka Data Members aka Attributes
Regardless of what you call them, when we want to define attributes that objects can have, we use variables inside the class definition. For example:
public:
int horsepower;
};
Now we are telling the computer, "Hey computer, Cars are things with an attribute called horsepower, which is an integer." The public:
thing you see there means that programmers can access a Car object's horsepower using the "dot-operator" (aka "member access operator"). For example:
junkTruck.horsepower = 100;
Assuming that every Car instance has a horsepower attribute, we can access it just like the above snippet of code illustrates.
Constructors
Consider what the Car example above does. It has merely declared the fact that there is such a thing as a Car. But how do we "create" or instantiate Car objects? We do so through a family of special functions we call constructors. Realize that by default, a C++ compiler provides a "hidden" default constructor when you do not define one. In other words, with just the above class definition, we can still instantiate a Car.
But what if you wanted to do the following?
cout << junkTruck.horsepower;
What is the value of horsepower
? We don't know -- it doesn't have a default value. Although the C++ compiler provides you the ability to create a default object, it's not smart enough to initialize all the attributes of your objects for you.
Let's imagine that you'd like all Car objects to have a default horsepower of 100 when instantiated. To do this, we just have to define a default constructor ourselves.
To do so, we must take two steps:
- Declare the constructor in the header file Car.h
- Implement the constructor in the implementation file Car.cpp
In the end, the file Car.h would look like this:
public:
Car(); // the prototype of the constructor "function"
int horsepower;
};
And Car.cpp would look like this:
Car::Car() {
horsepower = 100;
}
See the strange :: syntax? This is like saying, "The Car class' function called Car() is defined as..." followed by the function body. Notice that constructors are just functions, but they're "special" in that:
- They always have the exact same name as the class to which they belong
- They have no explicit return type
- They are automatically called upon instantiation
Now that you have defined your own default constructor, the next time you try the following, we would see 100 printed to the console.
cout << junkTruck.horsepower;
In the example above, we instantiate a Car called junkTruck, which is created via the default constructor. What does the constructor do? It initializes the object's horsepower to 100.
How would you create a non-default constructor, allowing us to assign a specific horsepower value upon instantiation?
Solve the assignment below to find out how!
Instructions
Define and implement a Box class such that it passes the test suite provided. 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; you must implement a default constructor and a non-default constructor.
One 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.
The best way to learn about classes is to just dive in. If you get stuck, be sure to ask questions on Piazza!
Requirements and Rubric
A friendly message from The Terminator, our grading program
Hello ag *bzzzt* again. I will check for the following:
Your program must not print "FAILED" in order to receive full credit.
You must not modify main.cpp (except adding your name), test.cpp or test.h.
Your class definitions should be in box.h and box.cpp, as provided.
I will *bzzzt* try to break your Box with my tests. Can you *bzzt* defeat me?
This work is worth 90 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 5 | |
Correct submission of src directory as a .zip file. | 5 | |
Declares Box class in box.h | 10 | |
Defines Box constructors in box.cpp | 10 | |
Passes all tests (20 pts each) | 60 |
Concepts Exercised: classes, objects, unit testing, reading code, application
© 2011 Yong Joseph Bakos.