Contents

Description

The purpose of this lab is to get you familiar with representing objects as classes in C++. By the end of the lab, you will be able to:

  1. create an interface (.h) and implementation (.cpp) file for a class
  2. compile a program that uses classes
  3. create constructors and instance methods for a class

This is a group lab, so you will have to partner up with someone else. Here are the pairings:

  1. Mike and Chris
  2. Hanah and Greg
  3. Emily and Mason
  4. Cam and Alex
(Back to top)

Tasks

For each of the following tasks, you will alternate the "driver"—the person who is actually programming. When someone is the driver, they are the only one allowed to touch the keyboard. If you want the driver to do something, you must communicate through talking or by drawing on a piece of paper.

Upon completing each part, have the professor check your work before you continue to the next part. Be sure to reference the OOP topic page as needed.

Task 1: A simple class

For this task, the partner whose first name comes first alphabetically will be the driver.

Back when we first learned about structs, we saw the following example:

Take all but the struct definition from this example and save it in a file called lab11-task1.cpp. Convert the Student struct into a class. The class interface should go into its own file named Student.h, and you need to include it at the top of lab11-task1.cpp. You do not need to worry about an implementation file for the class yet (we only use those if there are constructors or instance methods to implement). Keep in mind that the default access level for structs and classes is different, so you might need to use the public or private keyword in the class interface. See the OOP topic page for examples.

Your code should compile with this line:

g++ lab11-task1.cpp -o lab11-task1

Try running the code. It should exit immediately—there is nothing printed out from main. We'll fix that in a bit.

Task 2: Adding constructors

You should switch which partner is the driver at this point.

Copy your lab11-task1.cpp file to a new file named lab11-task2.cpp.

Add a two constructor declarations to the Student class interface. Now you should also add a file Student.cpp. This file should contain the definition of the constructors. See the section about classes on the OOP page for an idea of how the Student.cpp file should look. One constructor should be the default constructor—it shouldn't take any parameters but should set the data members to default values. The other constructor should take one parameter per data member.

Now change the code in main so that you use the constructor rather than setting each of the data members individually.

Once you have finished modifying main and the class interface and implementation files, you can compile. Since there are now two .cpp files we need in order to compile, we have to include both on our compilation line. You will not be able to compile from Sublime. Do this instead from a terminal:

g++ lab11-task2.cpp Student.cpp -o lab11-task2

Task 3: Adding instance methods

You should switch which partner is the driver at this point. Whoever programmed Task 1 should be programming this task.

Copy your lab11-task2.cpp file to a new file named lab11-task3.cpp.

Now add the following methods to your class. Be sure that for each there is a declaration in the class interface (i.e., in the .h file) and a definition in the implementation file (i.e., the class' .cpp file).

Modify main to invoke the print method on each Student instance created.

Compile the code like you did for Task 2 and try running it. Your compile line will be a little different because we are working with lab11-task3.cpp now. You should see a description of each student printed out.

Task 4: Restricting access to data members

You should switch which partner is the driver at this point. Whoever programmed Task 2 should be programming this task.

Copy your lab11-task3.cpp file to a new file named lab11-task4.cpp.

Now make the Student class data members private, but all the instance methods public. Add code to main to get and set various data members using the getters and setter.

Compile and run your code.

(Back to top)