LAB 11A - MONEY CLASS

Concepts

Today you will define what it means to be "Money" to the machine. Focus on two main concepts for this assignment: (1) define classes with public member variables and (2) define and implement constructors.

You will need to submit your class prototype file, your class implementation file, and your main.cpp file for this lab with Homework 11. Directions on how to do this are in Homework 11.

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:

string aliceTitle = "Alice in Wonderland";
int alicePages = 200;
string favorite = "Great Mambo Chicken and the Transhuman Condition";
int favoritePage = 238;
cout << aliceTitle << " " << alicePages;

Notice how this first snippet above is really about programming with strings and ints. Now let's look at a different snippet of code:

Book alice("Alice in Wonderland", 200);
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

We are learning about C++ classes in lecture this week. 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 a 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.

class Car {

}

class Car {

};

There you have it, the world's most simple definition of a Car, which a C++ compiler can actually comprehend.

Member Variables aka Data Members aka Attributes aka Properties

Regardless of what you call them, when we want to define properties that objects can have, we use variables inside the class definition. For example:

class Car {
    public:
        int horsepower;
};

Now we are telling the computer, "Hey computer, Cars are things with a property 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:

Car junkTruck;
junkTruck.horsepower = 100;

Assuming that every Car instance has a horsepower property, 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 that 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 instantiate a Car.

Car junkTruck;

But what if you wanted to do the following?

Car junkTruck;
cout << junkTruck.horsepower;

What is the value of horsepower? We don't know, as we didn't give it 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 properties of your objects.

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.

Defining a default constructor requires two steps:

  1. Declare the constructor in the header file Car.h
  2. Implement the constructor in the implementation file Car.cpp

In the end, the file Car.h would look like this:

class Car {
    public:
        Car(); // the prototype of the constructor "function"
        int horsepower;
};

And Car.cpp would look like this:

#include "Car.h"

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:

Now that you have defined your own default constructor, the next time you try the following, we would see 100 printed to the console.

Car junkTruck;
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?

Car goodTruck(400);

Solve the assignment below to practice how!

Instructions

Download Lab11A.zip to your C++ directory. Load it into Visual Studios, and notice that there are three files in your source directory: main.cpp (which you will not modify), money.h (which is where you define your Money class), and money.cpp (which is where you implement your Money class member functions).

Define and implement a Money class such that the provided main.cpp will compile and execute appropriately. Specifically, your Money class should have two integers (called dollars and cents), two constructors (one default and one non-default), and four public member functions (GetDollars(), GetCents(), SetDollars(int), and SetCents(int)). It should be obvious what the member functions do from looking at the main.cpp file provided but, if not, ask. Two of the member functions (the accessor functions) should return an integer; the other two member functions (the mutator functions) have no return.

We suggest you first define your class, and then add empty member functions (to get the code to compile). Then write the "guts" for each member function. When you are done, the output should look like the following screen.

Who doesn't love money?

I have $999.99
You have $987.65

I have $12.34 more money than you!

In your first class definition (today's lab), you can let your data members and functions be public. Starting next class, we'll want to keep data members private.