BIG C++
Cay Horstmann & Timothy Budd

Laboratory Notebook
Chapter 10 - Pointers


Your name:
Your email address:
Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.


Lab Objectives

To gain experience with


P1. Pointers

Write a program that creates a vector ring of pointers to Circle objects, that is, a vector<Circle*> ring;. Your job is to fill rings with five pointers to circles that are configured like the interlocking Olympic rings.

Initialize the vector by allocating five circles on the heap with new and calling push_back to append the resulting pointers to the vector.

Write a loop that displays all elements in rings on the graphics screen

Write a loop that deletes all heap objects whose pointers are stored in rings

Combine the code into a main program that prints out the Olympic rings.

Note: In this example, there was no advantage to using a vector<Circle*>. It would have been easier to use a vector<Circle>. The purpose of this exercise was to get you used to the pointer notation and dynamic allocation.

This is a worthwhile exercise since the next lab (on inheritance) uses a vector<Card*> to hold a vector of cards (such as an ID card, a phone card, and so on). In that lab, it is essential that you use a vector of pointers--a vector<Card> will not be sufficient.


R1. Pointers

The following program contains several errors in the way that the pointers are dereferenced. Please correct the program and paste your corrected version in the textarea below.



P2. Uses of Pointers

Create a class called Student_Club, which will store information on student clubs. The Student_Club class should have as fields President, Vice-President, Secretary, Treasurer, all of which should be pointers to a student object. In this way, a student could hold offices in many different clubs and we would not have to keep separate information about that student in each instance of Student_Club. Your class definition should also include constructors as well as accessor functions.


R2. Arrays and Pointers

Rewrite the following program to use pointers instead of arrays:



P3. Arrays and Pointers

Using the Student_Club class created in P3, add one additional field, members, which should be an array of pointers to student objects.


P4. Pointers to Character Strings

Write a function Count_Cut(char x[]) that accepts a character string as input, counts the number of characters and returns the left half.


R3. Pointers to Character Strings

Look at the following examples and write your answer in the column next to the example.

string x = "Kathleen";

string p=x;

p[0] = 'S';
after executing:

x = ___________________

p= ___________________
string x = "Boston";

string p = "Massachusetts";

x[0] = p[0];
after executing

x = ___________________

p = ___________________
char o[] = "Henry";

char* l = o;

o[0] = 'D';
after executing

l = ____________________

o = ____________________
char* o = "The rain in Spain falls mainly on the plain."

char* p = "Hello, World!";

o[12]=p[0];

p[7] = o[12];
after executing

o = ____________________

p = ____________________


Do not forget to send your answers when you are finished.