CS1030 -- Lab 7: Polymorphism

Spring 2005
Objectives Addressed
Part B

You should create a class called Baser that has the same member functions as the Base class from homework number 7. The destructor and up should be declared as virtual functions. The bam function should be declared to be a pure virtual function. All of the implemented functions in the Baser class should display "Baser::FuncName" whenever they are called.

Next you should modify the Base class so that it inherits from the Baser class.

On a piece of paper, write what you expect the following code to display. Do this before proceeding.

#include "Baser.h"
#include "Base.h"
#include "Derived.h"

int main() {
  Baser *brPtr = 0;
  Base *bPtr = 0;
  Derived *dPtr = 0;

  Derived derived;
  Base base(derived);

  brPtr = &base;
  brPtr->up();
  brPtr->down();
  brPtr->bam();

  bPtr = &base;
  bPtr->up();
  bPtr->down();
  bPtr->bam();

  bPtr = &derived;
  bPtr->up();
  bPtr->down();
  bPtr->bam();

  dPtr = &derived;
  dPtr->up();
  dPtr->down();
  dPtr->bam();

  return 0;
}

Once you have written on paper what you expect to be displayed, run the above program and then compare your expected results to the actual results. If anything doesn't match, try to figure out what you missed. Once you think you understand what happened, call me over so I can pepper you with questions.

Part C

Write a program that creates a vector (use your vector implementation from lab 6) of 3,213 Baser pointers. Make each pointer point to an object from the Base or Derived (chosen at random). Next you should traverse the vector and call bam on the 1st, 4th, 7th, etc... elements, call up on the 2nd, 5th, 8th, etc... elements, and call down on the 3rd, 6th, 9th, etc... elements. Before the program ends, you should clean up any dynamically allocated memory.

Lab submission (due at the end of lab)

Before you leave lab, you should submit your source code file for Part C.

Acknowledgment

This laboratory assignment was developed by Dr. Chris Taylor.

Last Updated: Thursday, 01-Jan-2015 14:31:53 EST