COSC 1320 - C++ Programming
Bob Comer, Professor, CIT/CSC


Exam 3 Review - Fall 2002
General Questions and Hints

Note: All chapter numbers refer to the Malik textbook.

Answers to these questions are here.

Points on the exam will be distributed roughly as follows:

Structs

30%

Classes

35%

Pointers

20%

Linked Lists

5%

Input Files

10%

Chapter 12 Records (Structs)

Questions:

1. Write the declaration for a struct called Item (to represent an inventory item) that has data members:

2. Write a declaration for a struct variable of type Item called myItem and then write code to set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

3. Write a function that takes as parameters a struct of type Item, an item number, a description, and a price. Your function should assign the item number, description, and price to the struct data members.

4. Write the declaration for an array of 150 Item structs called inventory. Then write code to set the item number of the first Item in inventory to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

5. Write a function that prints the descriptions of all the elements in array inventory where the price is 2.0 or over. You can assume that the array is full.

6. Write a function that returns a count of the number of elements in array inventory that have a price of 2.0 or greater.

Chapter 13 Classes

1. Create a simple class called Item (to represent an inventory item) with private data members:

In the class definition include prototypes for public member functions:

After the class definition, write the definitions for the member functions (do not put the function code inside the class).

2. Write client code to create an Item object called myItem. Then write code to set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

3. Write client code to declare an array of 150 Item objects called inventory. Then write code to set the item number of the first Item in inventory to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

4. Write a function that prints the descriptions of all the elements in array inventory where the price is 2.0 or over. You can assume that the array is full.

5. Write a function that returns a count of the number of elements in array inventory that have a price of 2.0 or greater.

6. Creating an Item object and setting it's data members to values specified by the client programmer takes 2 statements with the Item class code written so far. Add a second constructor to the class so that the client programmer can declare an Item object and specify initial values for it's data members in a single statement.

7. Write a sample call to the constructor written in the previous problem.

Chapter 14 Inheritance and Composition

Note: this exam will not cover inheritance. In the Malik textbook, I expect you to have read:

pages 636 - 637 Overview of Inheritance

pages 654 - 655 C++ Stream Classes

pages 660 - 665 Composition

pagse 666 - 667 Overview of OOP

Chapter 15 Pointers

This chapter covers pointers, dynamic memory allocation, and the relationship between one-dimensional arrays and pointers. Virtual functions will not be covered on the exam. In the Malik textbook, I expect you to have read pages 703 - 729.

1. I expect you to be able to manipulate the value of a variable of a simple type using a pointer. This is not particularly useful, it just demonstrates that you know how pointers work. For example, given integer variable num, write code to declare a pointer variable called numPtr and store the address of num in numPtr. Then write code to change the value of num to 5 and print it's value without using the variable name (use the pointer).

2. The following function (shown with sample call) uses reference parameters to exchange the values of the arguments in the call. Re-write the function to use pointers instead of reference parameters (re-write the sample call also). Note: when you want to change the values of the arguments in a function call, you really should use reference parameters, not pointers. This is just an exercise to show how pointers work.

Call:

int x = 10, y = 20;

swap( x, y );

function definition:

void swap( int& num1, int& num2 )
{
   int temp;
   temp = num1;
   num1 = num2;
   num2 = temp;
};

3. I expect you to be able to dynamically allocate memory for a value of a simple data type. For example, write code to dynamically allocate memory for a float value, store 6.5 in it, and then print it's value.

4. I expect you to be able to dynamically allocate an array of a simple data type. For example, write code to dynamically allocate an array of 5 integers, initialize the array to { 10, 20, 30, 40, 50 }, and then print the values in the array.

5. I expect you to know the relationship between pointers and arrays in C++. The Malik textbook has lots of information about arrays and pointers but the examples are lacking. Click here for some notes on manipulating arrays using pointers.

Questions 6 - 10 refer to this struct declaration:

struct Item
{
   int itemNo;
   char desc[41];
   float price;
};

6. Write the declaration for a variable called itemPtr that can hold the address of an Item struct. Then dynamically allocate an Item struct and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

7. Write a function that takes as parameters a pointer to an Item struct, an item number, a description, and a price. Your function should assign the item number, description, and price to the struct data members.

8. Write a declaration for an array of 150 pointers to structs of type Item called itemPtrs. Then write code to dynamically allocate an Item struct, store it's address in the first element of itemPtrs, then set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

9. Write a function that prints the descriptions of all the elements in array itemPtrs where the price is 2.0 or over. You can assume that the array is full (each element of the array points to an Item object).

10. Write a function that returns a count of the number of Items in array itemPtr whose price is 2.0 or greater. You can assume that each element of the array points to an Item object.

11. Write a declaration for a structure to hold a date as 3 integer values: the month, day, and year. Then write code to dynamically allocate a date, store some information in it, and print it out.

A2. Reviw the following Example C++ Program that dynamically allocates structs and stores their addresses in an array of pointers:

itemptr.cpp

The following questions (12 - 17) refer to this class declaration:

class Item
{
private:
   int itemNo;         // inventory item number
   char desc[61];      // item description
   float price;        // item price
public:
   Item();
   Item(int, char [], float);
   void set(int, char [], float);
   int getItemNo();
   const char *getDesc();
   float getPrice();
};

Item::Item()
{
   itemNo = 0;
   strcpy( desc, "");
   price = 0;
}

Item::Item(int item, char itemDesc[], float itemPrice)
{
   set(item, itemDesc, itemPrice);
}

void Item::set(int item, char itemDesc[], float itemPrice)
{
   itemNo = item;
   strcpy( desc, itemDesc );
   price = itemPrice;
}

int Item::getItemNo()
{
   return itemNo;
}

const char * Item::getDesc()
{
   return desc;
}

float Item::getPrice()
{
   return price;
}

12. Class objects can be dynamically allocated similar to the way structs are dynamically allocated. Given the Item class declared above, write client code to create a variable to hold the address of an Item object. Then dynamically allocate an Item object and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

13. Write client code to declare an array called itemPtrs of 150 pointers to Item objects.

14. Write client code to dynamically allocate an Item object, store it's address in the first position of array itemPtrs, and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

15. Write a function that prints the descriptions of all the elements in array itemPtrs where the price is 2.0 or over. You can assume that the array is full (each element of the array points to an Item object).

16. Write a function that returns a count of the number of Items in array itemPtr whose price is 2.0 or greater. You can assume that each element of the array points to an Item object.

17. Given that the Item class contains a second constructor that allows the client programmer to declare an Item object and specify it's initial values in a single statement, write a statement that dynamically allocates an Item object and initializes it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.

A1. Review the following Example C++ Program that dynamically allocates class objects and stores their addresses in an array of pointers:

itemptr2.cpp

Chapter 17 Linked Lists

You should know what a linked list is, how it works, and when it might be used. You will not have to write any code to manipulate a linked list. In the Malik textbook, I expect you to have read this introductory information on linked lists (pages 843 - 853).

Questions:

  1. Under what circumstances might it be better to use a linked list instead of an array of structs or objects? Under what circumstances might it be better to use an array of structs or objects instead of a linked list? Issues: max size, shrink/grow, editing (insert/delete), direct access versus traversal.

Chapter 3 File Input

Questions:

1. Given the following declarations:

struct Pokemon
{
   char name[21];
   char type[21];
   short strength;
};

Pokemon pokedex[150];

Write code to read data from file c:\pokemon.txt and load it into the array until the end of the file is reached, or the array is full, whichever comes first. Include code to open the file.

2. Write code to take the data in array pokedex and write it into file d:\pokemon.txt. Include code to open the file.


Return to C++ Home Page

Copyright: Ó 2002 by the Austin Community College.
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: December 6, 2002