312-174

Lab 9

Spring 2000

Due (PreLab, bridge): No prelab or bridge this week. Study for the exam instead.

Due (In-Lab/PostLab): Day of the lab.

Goals:

  1. Understand inheritance.

  2. Understand virtual functions.

  3. Review pointers.

Requirements.

  1. Note that this lab uses code from the Dale textbook, not from the Roberge lab book. This lab is designed to be completed within the lab period. You may work in pairs for this lab.

    As you finish each required item below, have me or a TA check your work and sign off that you have completed the lab. You must hand in a completed lab sheet with the items signed off. You do not have to put any of your code on Nova.

  2. Activity 1. In this activity you are to type in and modify the code on pages 368, 369, and 370 of the Dale textbook. Note that there are some errors in the code in the textbook, so I have supplied corrections below.

    1. First type in and test the code. You should have a class ItemType and a class NewItemType. You must do the following when you create this code:

      • Ignore the dots in the code. We'll just make simple classes and functions.

      • You must create a constructor for each class that takes a string and stores the string in the private data. Note that the constructor for the NewItemType must call the constructor for ItemType in the NewItemType constructor. You can achieve this with code similar to the following:

        NewItemType::NewItemType (string First, string Last):ItemType(Last)
        {
        	// initialization for NewItemType private member
        }
        

      • You can use the string class that is built into Borland C++. You will have to include the string include file.

      • Change the lines in ItemType::CompatedTo from

        result = strcmp(lastName, item.lastName);
        if (result < 0)
        	return LESS;
        else if (result > 0)
        	return GREATER;
        else
        	return EQUAL;
        

        to:

        if (lastName < item.lastName)
        	return LESS;
        else if (lastName > item.lastName)
        	return GREATER;
        else return EQUAL;
        

      • RelationType is an enumerated type. See page 84 of your textbook.

      • You must add another virtual function to the ItemType class. The definition is:

        virtual string getName() const;
        

        and its definition is:

        string ItemType::getName() const
        {
        	return lastName;
        }
        

      • The definition of ComparedTo for NewItemType is wrong in the text. This is because the derived class doesn't have direct access to the private data of the base class. An even bigger problem is that the parameter of CompatedTo in class NewItemType must be the same as the parameter of ComparedTo in class ItemType otherwise the compiler will see these two functions as different functions and not overriden function. Instead of Dale's code do this:

        RelationType NewItemType::ComparedTo(NewItemType item) const
        {
        	RelationType rslt1;
        	
        	rslt1 = ItemType::ComparedTo(item);
        	if (result == LESS)
        		return LESS;
        	else if (result == GREATER)
        		return  GREATER;
        	else 
        	{
        		if (firstName < item.getName())
        			return LESS;
        		else if (firstName > item.getName())
        			return  GREATER;
        		else return EQUAL;
        	}
        }
        

      • Finally, you must override the getName() function from ItemType class. It's definition will be:

        string NewItemType::getName() const
        {
        	return firstName;
        }
        

      • You must create a file with a main function. This file will also contain the PrinteResult function that's given in the book. The main function must contain the code given on the top of page 370. The declarations should look like:

        ItemType item1("John"), item2("Chuck");
        NewItemType item3("Dave", "Jones"), item4("Tom", "Jones");
        
      • Don't forget to include your .h file that contains your classes. Also include iostream.h.

    2. Show your results so far to a TA.

    3. Now change this code by adding the LastItemType class. This class will inherit from the NewItemType class.

      • This class should have a constructor that takes the first name, last name, and middle name.

      • It must have a private data type named middle which contains a string.

      • It must have the virtual function ComparedTo that receives an ItemType. This function should work identically to the function of the same name in NewItemType except that if the first and last name are both equal, it should compare the middle name and return LESS, GREATER, or EQUAL based on a comparison of this string.

      • You must also override the function getName().

      • Next, change the main function to add two new variables, item5 and item6 both of type LastItemType. Initialize the first variable to contain "Adam Jones" and "Greg" and the second to contain "Adam Jones" and "Bob".

      • Call PrintResult in the main function with item5 and item6.

    4. Show the TA the result of your function. Although this technique of virtual functions worked fine for NewItemType, it will fail for some data with LastItemType. Explain to the TA why this is true.

  3. Activity 2. Next enter in the code on page 371 of the Dale book. Make the following changes to this code. Note that the code in the book is correct, I'm just asking you to extend it.

  4. There is no in-lab or post-lab exercise this week.

Late labs will be penalized as described in the study guide.

Additional requirements:

  1. None.

  2. Written answers must be legible. If I can't read an answer, I will mark it wrong. Print or type if you must.

Hints:

Return to Student Pages

Return to John Barr's Home Page

Last Modified: 10 March 2000

THIS PAGE MAINTAINED BY:
John Barr, Ithaca College