CS 160 - Programming Concepts and Applications

Summer II 2018 - Lab 6B - Money Class

Quick Links: Canvas | John Cabot | Piazza | zyBooks

|   Home |  Contact |  Syllabus |  Assignments |  Schedule |  Resources   |

This lab is due by Monday, July 30, 2018, 11:59 PM.


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 declaration file, your class definition file, and your main.cpp file for this lab with Assignment 7..


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.title << " " << alice.pages << 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, and bridges -- 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, and boxes) to the machine, and thereby create programs that use these abstractions. Today, focus on your ability to define classes using a declaration file (ending in .h) and a definition file (ending in .cpp).

Perhaps the most tricky thing to remember about the syntax for class declarations is the trailing semicolon at the end of your class declaration's closing brace.

class Car {

}

class Car {

};

There you have it, the world's most simple declaration 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 declare properties that objects can have, we use variables inside the class declaration. 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:
  • 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.

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 MoneyClass.zip and add the files to your project. Notice that there are three files in the zip file: main.cpp (which you will NOT modify), Money.h (which is where you declare your Money class), and Money.cpp (which is where you define your Money class member functions).

Declare and define 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) and two constructors (one default that sets the amount to 999.99 and one non-default).

We suggest you first declare your class, and then add empty constructors (to get the code to compile). Then write the "guts" for each constructor. When you are done, the output should look exactly like the following:

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 declaration (today's lab), you can let your data members be public. Starting next class, we'll want to keep data members private.


Lab Submission



You will submit your solution to this lab with the rest of Set6. Detailed instructions for doing this are posted in Assignment 6.


This lab is due by Monday, July 30, 2018, 11:59 PM.

Last Updated: 06/07/18 06:38


Valid HTML 4.01 Strict Valid CSS! Level Triple-A conformance, W3C WAI Web Content Accessibility Guidelines 2.0