Skill Building Exercises

 < Day Day Up > 



  1. Extend Person: Create a class called Faculty that extends the functionality of Person. (See chapter 11 for the complete code for the Person class). Give the Faculty class some unique attributes such as faculty number, department code, etc. Declare and implement any necessary derived class accessor and mutator functions. Write a driver program and test the Faculty class by creating Faculty objects and calling both derived class and base class functions via the Faculty objects. Use the Person/Student example presented in this chapter as a guide.

  2. Sensor Class: Create an abstract class called Sensor. In Sensor declare a pure virtual function named getReading() that returns a float. Create two classes that derive from Sensor named TemperatureSensor and OxygenSensor. In each of these classes implement the getReading() method. In TemperatureSensor the getReading() method should return a float value between absolute zero and +4000 degrees centigrade. Use the standard C library rand() function to generate a random float value within this range. In OxygenSensor implement the getReading() function to return a float value between 0 and 100. Again, use the rand() function to randomly generate the necessary values each time the getReading() funciton is called. Write a driver to test your sensor objects. Create a few TemperatureSensor and OxygenSensor objects and test the getReading() function. Declare a Senor base class pointer and dynamically assign the address of either a TemperatureSensor or OxygenSensor object. Call the getReading() polymorphically through the base class pointer.

  3. Research: Study the C++ stream classes. Draw the inheritance hierarchy. How does each derived class build upon the functionality of its base class?

  4. Pop Quiz: Explain the difference between function hiding and overriding.

  5. Research: Continue your study of the topic of inheritance. Your objective is to gain a comfortable feeling for the different ways inheritance is used to achieve object-oriented design goals. Study how languages other than C++ allow programmers to implement their inheritance designs. A good place to start your research is by reading the papers listed in the reference section at the end of this chapter. If you are a member of the Association of Computing Machinery (ACM) they have an excellent online digital library. The web in general has lots of material on this topic.

  6. Program: a. Create a class named MyBaseClass and declare a constructor, and destructor, and two other public interface functions named f(), and g() that return void. The only functionality each function should have is to print a simple message to the screen using iostream output. Leave the keyword virtual off the destructor, f(), and g() for now. Write a main() function to test MyBaseClass.

    b. Once MyBaseClass is tested, create a new class that derives from MyBaseClass named MyDerivedClass. Declare a constructor and destructor that print trace messages but no other functions. Test MyDerivedClass by declaring a MyDerivedClass object and calling the f() and g() functions.

    c. In the main() function declare a MyBaseClass pointer and use the new operator to create a MyDerivedClass object and assign its address to the pointer. Call the functions f() and g() via the pointer. Do not forget to delete the pointer at the end of the program! Run the program and note the trace messages. specifically the destructor messages. Are they all there? If not, why?

    d. Modify the MyBaseClass destructor and add the keyword virtual. Run the program again and note the trace messages. Are all the destructor messages there now?

    e. Modify the MyDerivedClass by adding a new function named f(). Make f() print a message that is different from the MyBaseClass version of f(). Run the program again and note the trace messages. Are they now different? If so, how? If not, what would you have to do to get the derived class version of f() to be called via the base class pointer? (Hint: virtual base class functions)

  7. Pop Quiz: Why can you not instantiate an abstract base class.

  8. Pop Quiz: Explain the role abstract base classes play in designing with inheritance. If an abstract base class has only a set of public pure virtual functions, what is it good for? If you find it hard to answer this question confidently revisit skill building exercise #5.

  9. Program: a. Create a set of classes named A, B, C, D, E, F, & G with the following inheritance relationship:

    Give each class a constructor and destructor only that print simple trace messages to the screen. Use non-virtual inheritance. Ensure your destructors are declared to be virtual. Test each of the classes individually by creating objects of each type. Study the constructor and destructor trace messages.

    b. Create a pointer to class A. Assign it the address of a dynamically created E object. Which objects are created?

    c. Next, assign to the A pointer the address of a dynamically created G object. Which objects are created? How many A objects are created?

    d. Next, assign to the A pointer the address of a dynamically created D object. Which objects are created? How many A objects are created?

    e. Modify only the B class to virtually inherit from A. Assign to the A pointer the address of a dynamically created G object. Did this result in a different set of trace messages? If so, explain what has changed?

  10. Program: a. Create an abstract class named AbstractClass that declares one pure virtual function named f(). Next, create three derived classes that inherit from AbstractClass named DerivedOne, DerivedTwo, and DerivedThree. In each of the derived classes implement the f() function to print a simple message to the screen. In a main() function create an array of three AbstractClass pointers and dynamically allocate one object of each derived class type and assign its address to an array element. Example:

    abstract_array[0] = new DerivedOne;

    b. Call the f() function via the pointers stored in each array element.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net