CSE 4208: Studio 8

Object Oriented Programming


Today's lecture and studio focus on inheritance-related relationships between classes (and their member functions) and the effects those relationships have on how those classes can be used. In this studio you will explore different features of inheritance polymorphism in C++, which along with inheritance for re-use of implementation will be an important topic (as will interface polymorphism provided by templates) in the lab assignments and throughout the rest of this course.


Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.

As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then upload a .txt file containing your answers on the Canvas page for this studio assignment. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.
  2. First, ssh into shell.cec.wustl.edu using your WUSTL Key id and password and then use qlogin to log into one of the Linux Lab machines and then confirm that the version of g++ there is correct (as you did in Studio 0). Then cd into your directory for this course this semester, create a new subdirectory for this studio, and cd into it.

    Copy the CMakeLists.txt from your Studio 0 directory into that directory and update it as you work on this studio so that it will build an executable program called studio8 from the source and header files that you will write.

    Add a new header file and source file to your directory, and in them declare and define a class that has a public default constructor and public destructor, in which each of them prints out a statement indicating the class name and the method name (e.g., BaseClass::BaseClass() in the constructor and BaseClass::~BaseClass() in the destructor), to the standard output stream.

    Add another header and source file to your directory and in them declare and define a class that is derived from the other class (which is its base class) via public inheritance, with the derived class also having a public default constructor and public destructor, each of which also prints out its class name and method name (e.g., DerivedClass::DerivedClass() in the constructor and DerivedClass::~DerivedClass() in the destructor), to the standard output stream.

    Add another source file to your directory and in it define a main function that declares one object of each of the class types and then returns a descriptively named symbol whose value is 0, to indicate success. Build and run your program, and as the answer to this exercise please (1) show the output your program produced, and (2) explain briefly why the program produced the output it did. Hint: you should see more constructors and destructors being called than just one of each per object.

  3. Add a non-virtual public member function to each of the classes (with the same member function name in each class) that takes no parameters and has a void return type. The definitions of that member function for each class (which should be in your source files) should again print out a statement indicating the class to which it belongs and the name of the member function.

    In your main function, after the declarations of the objects you added in the previous exercise, add two references to the base class type and one reference to the derived class type. Initialize one of the base class references using the base class object from exercise and initialize the other base class reference and the derived class reference using the derived class object.

    Invoke the public method you added for this exercise on both of the objects and on all three of the references. Put output statements before each of the calls to the function, so you can tell which of them produced which output. Build and run the program, and as the answer to this exercise show the output your program produced.

  4. In each of the classes put the keyword virtual before the declaration of the member function you added in the previous exercise. Build and run the program again, and as the answer to this exercise please (1) show the output that the program produced and (2) explain briefly how and why the program's output differed when you used the keyword virtual vs. when you didn't.

  5. Add two pointers to the base class type and one pointer to the derived class type to your main function. Initialize one of the base class pointers using the address of the base class object and initialize the other base class pointer and the derived class pointer using the address of the derived class object, and then call the (virtual) member function through each of the pointers. As the answer to this exercise please explain briefly whether or not the calls using the pointers behave similarly to the calls using the references, and why you think that is so.

  6. At the top of the source file for your main function, include the <memory> library header file and also add appropriate using statements to give access to the shared_ptr and make_shared templates within the standard namespace.

    Then, replace the contents of your main function from the previous exercise with declarations of two shared_ptr variables parameterized with the base class type and one shared_ptr variable parameterized with the derived type. Initialize one of the shared pointers to the base class type with a call to make_shared parameterized with the base class type, and use calls to make_shared parameterized with the derived class type to initialize the other two shared pointers. Your main function should then call the (virtual) member function through those shared pointers and then return a descriptively named symbol that is valued 0 to indicate success.

    Build and run your program, and as the answer to this exercise please show the output it produced.

  7. Replace the shared pointers in your main function with two (plain old C++) pointers to the base class type and one (plain old C++) pointer to the derived class type. Initialize the pointers using calls to the new operator, which creates a new instance of the base class type (for one of the base class pointers) or of the derived class type (for the other base class pointer and the derived class pointer). After all three of the pointers are initialized use them to call the (virtual) member function, but do not call delete on the pointers to free the memory.

    Build and run your program, and as the answer to this exercise please say what the main difference you saw in the output was, compared with what you saw in the previous exercise.

  8. Just before your main function returns, use the delete operator on each of the pointers, to free the memory to which it points. Compile your code, and note the warnings that the compiler gives you. Then run your program and as the answer to this exercise show the output it produced.

  9. Make the destructor of each class virtual, and compile and run your code, and note the output that was produced this time. As the answer to this exercise, please explain briefly why you think the destructor must be virtual when using (plain old C++) pointers and the new and delete operators with polymorphic objects in C++, based on the differences in output you saw between this exercise and the previous one.

    Optional Enrichment Exercises

  10. In your main function, add try/catch logic, with separate catch blocks for objects of the derived and base class types. Try throwing and catching objects of the derived and base types, and in each catch block print out a string indicating which catch block caught the exception and then call the virtual method on the object you caught. As the answer to this exercise, please answer the following questions: (a) what happens if you catch the exceptions by value vs. by reference? (b) what happens if you reverse the order of the catch blocks, both when catching by value and catching by reference?

  11. What happens to the behavior of the program when you change the relationship between the base and derived classes from public inheritance to protected or private inheritance? As the answer to this exercise, please answer the following questions based on changing the kind of inheritance: (a) does the behavior of the virtual member function change when it is called in the different cases you examined in the earlier exercises? (b) does the behavior of the virtual destructor change? (c) using the scoping operator to specify the specific class version of the method, which methods of which classes can or cannot be accessed on derived class objects in your program's main function?

Things to Turn In:

For this studio, please turn in the following: