CSE 4208: Studio 9

Pointer to Class Member


Today's lecture and studio focus on pointers to non-static class member variables and public static member functions that return them (to support controlled violations of encapsulation), pointers to class member functions, and different kinds of callable objects based on standard library templates that can be used to invoke member functions. In this studio you will explore different ways that pointers to class members, and templates from the standard C++ libraries, can be used to support both object-oriented and functional programming paradigms.


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 and the source (.cpp) and header (.h) files from your Studio 8 directory into that directory and rename any of them as needed:

    e.g., mv studio8.cpp studio9.cpp

    and then remove any contents from the program's main function except for the statement at the end that returns a descriptively named symbol whose value is 0 (to indicate success).

    Also update the CMakeLists.txt as you work on this studio so that it will build an executable program called studio9 from the source and header files in that directory.

    Add a public member variable of type std::string to both the base and derived classes from the previous studio, and modify the default constructor of each class so that it initializes its member variable with the name of the class.

    Modify the main function so that it: (1) declares an object of the base class type and an object of the derived class type; (2) declares a pointer to a const string member of the derived class that is initialized to point to the string member that was declared in the derived class, declares a pointer to a const string member of the derived class that is initialized to point to the string member that was declared in the base class, and declares a pointer to a const string member of the base class that is initialized to point to the string member that was declared in the base class; and then (3) prints to the standard output stream the string that is obtained by fetching the members pointed to by the first two pointers from the derived class object, and by fetching the member pointed to by the third pointer from the base class object.

    Build and run your program, and as the answer to this exercise please show (1) the output your program produced, and (2) the code for your program's main function.

  3. Modify your base and derived classes so that their string member variables are declared private, and try to build your program (which should fail since the main function no longer should be able to access those private member variables).

    Then, add a public static member function to each class, which returns a pointer to (const) string member of that class and use those functions to intialize the pointers in your program's main function.

    Build and run your program and confirm that it produces the same output as in the previous exercise. As the answer to this exercise please show the modified statements in your main function that use the static member functions to initialize the pointers to members.

  4. Make sure that the public member functions (from the previous studio, not the static ones you just added) in your base and derived classes are virtual and that each of them prints out a statement indicating the class to which it belongs and the name of the member function.

    Then, in your main function replace the pointers to data members with 3 pointers

    Modify the output statements in your main function so that they reflect which pointer the function is invoked through. Then use each of them to invoke the member function it points to on the appropriate object as follows: the pointer to the base class member function is used to invoke its member function on the object of the base class type, and the other two are used to invoke their member functions on the object of the derived class type.

    Build and run your program and as the answer to this exercise please show (1) the output the program produced, and (2) the modified statements in your main function that declare, initialize, and use the pointers to member functions.

  5. Modify your base and derived classes so that their public member functions (which you invoked through pointers to member functions in the previous exercise) are not virtual.

    Build and run your program and as the answer to this exercise please explain briefly how the output that your program produced differed from the output that was produced in the previous exercise.

  6. In your program's main function, replace the pointers to member functions that you had declared in the previous exercise, with callable objects based on the standard function template from the <functional> library, and initialize and use them to invoke the same member functions on the same objects as in the previous exercise.

    Build and run your program and confirm that it produces the same output as in the previous exercise. As the answer to this exercise please show the modified statements in your main function that use the function template to declare and initialize the callable objects.

  7. In your program's main function, replace the callable objects you had declared in the previous exercise with callable objects based on the standard mem_fn template (which is also provided by the <functional> library) and initialize and use them to invoke the same member functions on the same objects as in the previous exercise.

    Build and run your program and confirm that it produces the same output as in the previous exercise. As the answer to this exercise please show the modified statements in your main function that use the mem_fn template to declare and initialize the callable objects.

Things to Turn In:

For this studio, please turn in the following: