Quick Tutorial for Section 7 (Tu@11:30am) Students:

Note that in the following, we use the terms "object" and "instance" somewhat interchangeably.

As we talked about in lecture, a class is like a C++ struct, except with more capabilities. Specifically, in addition to data, it also allows you to associate specific behaviors with the class, by defining functions for that class--what we call "methods" in object-oriented programming (OOP). So, while a struct has just data fields; a class has both data fields and methods. (See Slide #12, in the "Classes and Objects" lecture notes.)

When you define a class, you use the form given on Slide #13:


class Car
{
  public:
    bool AddGas(float gallons);
    float GetMileage();
    // other operations

  private:
    float m_currGallons;
    float m_currMileage;
    // other data
}

instead of starting with "struct Car" as you would when defining a struct, you would use "class Car", to indicate you are defining a new class. Inside the class definition, there are declared data members, just like a struct; the Car example has data members m_currGallons and m_currMileage. This is just like in a struct definition. However, we now also see what looks like function prototypes; for example:

    bool AddGas(float gallons);
    float GetMileage();

These are method member declarations. They declare the behaviors for this class.

Lastly, note that there are sections labelled public and private in the class definition: you can ignore these for the lab. We will discuss these later in class.

To create an object of the defined class, you just declare a variable of that type. For example, to create a Car class instance, you would just type:


    Car myFavoriteCar;

Once you've added this variable declaration, myFavoriteCar would be a Car object that we can put values into, and call methods on.

To access a data field, for example m_currGallons, we would use the '.' (dot) notation, just as with structs. So, we could say something like:


    cout << myFavoriteCar.m_currGallons;

(Actually, due to the private: label in the example in the slide, C++ would technically stop you from doing this, but if we remove the private, it would then work, so let's pretend for now).

To "invoke a method", as we like to say in OOP, we just use the same '.' notation, like this:


    myFavoriteCar.AddGas(10);

would hopefully add 10 gallons to the object myFavoriteCar.

To see how AddGas() is implemented, you need to look at the actual definition of the "AddGas()" method for the Car class. The class method definitions are located outside of the class definition. The only thing that we put inside the class definition itself is just the method declaration. This is like when we declare a function at the top of a file, but postpone the actual function definition until much later in the file. In fact, the full definition is usually in a separate file. We typically put the class definition in a ".h" file named after the class; so, for our Car example, the "class Car { ... }" part would go in the file "Car.h". Then, we would create a file called "Car.cpp" to hold all of actual definitions for the methods for the Car class. Now, since the method definitions are located outside of the actual class definition, we need to tell the compiler that a given function definition is to be a member of the Car class. You would preface it with the prefix "Car::". Otherwise, the compiler would think you are just defining yet another independent function, like you've been doing up to now.

For this lab, this is all you nee to know. You will not be defining any classes of your own, just using ones we are providing. You only need to know how to understand and use the Dog class, as defined in the files "Dog.h" and "Dog.cpp", which are given to you. Then, you will need to know how to create instances of this Dog class, and to then call methods on those instances.