C++ Seminar: 1094 Session 4

Handouts: None!

Your goals for today: Quick review of Day 5 and the start of Day 6, then work through Day 6 and Day 7.


Review: Day 5 and the first part of Day 6

We'll briefly step through the C++ function stuff from Day 5, giving you a chance at some debugging, and then recap the idea of a class from Day 6. Please ask questions!

  1. Some things to remember about functions:
  2. Let's try a couple of debugging exercises left over from Day 5 (so you'll find the programs under Session 3, Day 5).
  3. In Day 6, we introduced the "class", which contains data and functions that operate on that data. So far, we've only seen examples of the member data. Let's introduce other aspects of classes.

Day 6: Understanding Object-Oriented Programming (cont.)

Here we'll continue with Day 6. Hopefully you've had a chance to read this chapter in the online version of Day 6 (please read this later if you haven't yet).

  1. In Session 3, you looked at List0601.cpp, which introduced a class named Cat that had two public variables for a particular cat's age (itsAge) and weight (itsWeight). Since they were public, they could be modified directly by any other function. What we'll do now is hide or "encapsulate" those variables, by declaring them private. [By default, variables are private, but always declare them explicitly private to avoid confusion.] At the same time, we have to introduce functions that set and get the variables. These are called "public accessor methods". Appropriate declarations for the Cat class are shown in List0602.cpp; for each class function there is a function prototype that appears in the class definition. The keyword unsigned simply means that the integer cannot be negative. There's not much to see or do yet; we need to define the actual functions ("methods"), which we'll do next. But note how functions and data are mixed, but also divided into public: (accessible from outside the class) and private: (accessible only inside the class). There is also an unspecified function called Meow. Why do you think SetAge and SetWeight are declared as void?



  2. We take the next step in List0603.cpp. It has a simplified class definition for Cat (which has the cat's age stored but not its weight) but also definitions for the SetAge "public assessor" and the Meow "method" (which is just a name for a function associated with a class). Take a look at the code. Note that the functions have to be defined with Cat:: prepended to their names (this should remind you of std::cout and std::endl).



  3. Next we consider "constructors" and "destructors". There are functions associated with a given class that are invoked when an object of that class is created and destroyed. An example to follow along with is in List0604.cpp.



  4. Let's pause (no pun intended) and do a debugging exercise. Download and try to compile List0605.cpp. You'll find several errors that you have to diagnose and fix. One of them involves a member function defined as const. You should add const to the (end of the) declaration of any member function that does not change the value of any member variables. This is not required, but is good programming practice that can catch dangerous bugs.



  5. Let's talk about header files. The standard approach is to put the declarations in a header file ending in .hpp and the class definitions in a file ending in .cpp, both starting with the class name. Take a look at Cat.hpp and Cat.cpp. The header file is included with the
    #include "Cat.hpp"
    line. The use of ""'s rather than <>'s with the include statement tells C++ to look in the current directory for the file. In general, we'll want the main function and any other functions not in the Cat class to be in a separate file (or files) from Cat.cpp. This will require us to learn how to build a Dev-C++ project (later!).



  6. Ok, it's time to plan out a bigger example, which involves building up a complicated class by starting with simpler classes and including them when you declare the more complex class. Imagine we want to manipulate rectangles, which are defined in terms of x-y coordinates. So design a class Rectangle, which uses the class Point. [When you get stuck, take a look at Rectangle.hpp and Rectangle.cpp, which are possible solutions.]



  7. Here are some of the Day 6 Quiz questions to try (answers are in the handout from Session 1):
    1. What is the dot operator and what is it used for?
    2. What is the difference between public and private data members?
    3. Can member functions be private?
    4. Can member data be public?
    5. If you declare two Cat objects, can they have different values in their itsAge member data?
    6. Do class declarations end with a semicolon? Do class method definitions?
    7. What function is called to initialize a class?




  8. Exercise: Look at Employee.hpp and Employee.cpp. Change the Employee class so that you can initialize itsAge, itsYearsOfService, and itsSalary when you create an employee. Try it out with the numbers used in main (so by initializing the two employees this way you should eliminate six calls to accessor functions).



  9. Exercise: Look at Ex0608.cpp. What three bugs in the code should the compiler find? Answer first, then compile, then fix it.




Day 7: More on Program Flow

This Day focuses on loops and alternatives to if ... else statements. You can find more information about these things in the online version of Day 7.

  1. A useful way to repeat a sequence of statements as long as some condition is true is to use a while loop. The condition can be any expression that evaluates to either true or false. Take a look at List0702.cpp as a first example. We want to evaluate a print (cout) statement 5 times, so we assign an int variable called counter and increase it by one each time we print, checking each time to see if it is less than 5. Note carefully what the value of counter is just before exiting the loop and what it is outside. Can you explain these values? Try modifying the program so that it counts up to 10 by 2's (so you have to use something besides the counter++; statement).



  2. A more complicated example using while is given in List0703.cpp. Check out the expression that is tested for the while statement. The operator && is the logical "and" operator (so it returns true only if both sides are true). When does the while loop stop? Sometimes you want to "break out" of the while loop before its condition is true. This is done with the break statement, which kicks you immediately out of the while loop. An example building on List0703.cpp is given in List0704.cpp. Compile and run it, then check that you can explain the result. Make it write a dot every 1000 lines. What does the % operator do?



  3. The continue statement is used when you don't want to break out of the while loop but you want to skip the rest of the statements in the block being executed and just continue with the loop. Look at List0704.cpp for an example. It's not important that you master all of these details now, just that you are aware these options exist. If you need them, look up a sample program like this or check a C++ reference. To check that you can follow what is going on in this example, modify the program so that rather than skipping every skip numbers, it skips odd numbers and stops when large is less than 5.



  4. Sometimes you want the condition to be tested to appear after the block of statements to be executed repeatedly. Then you want a do while construction. Look at List0707.cpp for an example. Change this example so that every time through the loop it reads in an integer from the user (using cin) and prints out the input number until the number 13 is entered, at which point the loop should be exited and the program stops.



  5. A "for loop" is used to step through the value of some integer parameter (which is called counter in the examples here) that keeps track of how many times a block of statements (which should be enclosed in {}'s) is executed. The basic form is given in List0709.cpp. In the ()'s after for are three sets of statements, separated by semicolons. The first one shows the start value (counter = 0), the second one shows the test condition (counter < 5 means to keep going as long as the value of counter is less than 5), and the third one tells what to do every time through the loop (counter++ means to increment counter by 1 each time).



  6. In a previous session, you were asked to find Fibonacci numbers by recursion. (Recall that the Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, ..., with each new number the sum of the two previous numbers.) Devise a way to calculate the n'th Fibonacci number using a for loop instead. Check your solution against List0715.cpp.



  7. Our final new programming element is the switch statement, which tells C++ to take different actions based on the value of a test variable. An example is given in List0716.cpp. In this example, number is the test variable. Each case statement corresponds to a possible value of number (i.e., it can be 0, 5, 4, 3, 2, or 1). If it matches one of the listed values, the statements following that case is executed (and if there is a break statement the program exits the block marked by {}'s). If it doesn't match one of the possibilities, the statement following default: is executed.



  8. Exercise: What's wrong with this code:
     for (int counter = 0; counter < 10; counter++);
     {
        cout << counter << " ";
     }




  9. Exercise: What's wrong with this code:
     int counter = 100;
     while (counter < 10)
     {
        cout << "counter now: " << counter;
        counter--;
     }





C++ Seminar: 1094 Session 4. Last modified: 07:31 pm, November 11, 2006.
furnstahl.1@osu.edu