Assignments | Class Photo | Computing Resources | Course Home | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 180
Data Structures

Michael Goldwasser

Fall 2008

Dept. of Math & Computer Science

Homework Assignment 02

Object-Oriented Programming

Contents:


Overview

Our goal of this homework is to review some of the subtleties of object-oriented programming in C++, as seen in our walkthrough of the credit class and our "Objects Demo"

Topic: Object-Oriented Programming
Related Reading: Much of Ch. P and Ch. 1 as well as class notes
Due: Friday, 19 September 2008, 10:00am

Please make sure you adhere to the policies on academic integrity.


Practice Problems


Problems to be Submitted (20 points)

  1. (6 points)

    This problem explores access control specifiers, as describe in Chapter~ 1.3 of the text.

    Suppose your program contains the following class definition:

      class Automobile {
      public:
        double getPrice();
        double getProfit();
        void setProfit(double newProfit);
      private:
        double price;
        double profit;
        void setPrice(double newPrice);
      };
    
    and assume that the main part of your program contains the following declarations, and that a constructor sets all values to all member variables to some default values.
      Automobile hyundai, jaguar;
      double cost, markup;
    
    For each of the following statements, explain clearly whether it would be legal or illegal based upon the above class definition.
    1. hyundai.price = 4999.99;
    2. jaguar.setPrice(50000.00);
    3. hyundai.setProfit(100);
    4. cost = hyundai.getPrice();
    5. markup = jaguar.getProfit();
    6. if (hyundai==jaguar) { cout << "Want to swap cars?"; }

  2. (6 points)

    This problem explores variants in argument passing, as described in Ch. P.6 of the text.

    What (if anything) is different about the behavior of the following three functions f(), g() and h()?

      void f(int x) {
        x = x + 1;
        cout << x;
      }
      
      void g(int& x) {
        x = x + 1;
        cout << x;
      }
      
      void h(const int& x) {
        x = x + 1;
        cout << x;
      }
      
    

  3. (8 points)

    This problem explores static vs. dynamic allocation of objects and the use of the assignment operator and copy constructor. Most of this material is covered in some form as part of of Ch. P.5 of the text.

    Assume that we begin with the definition of a basic Thing class (similar to the one which we saw in class, but without all the extraneous chatter), and then the following declarations within the main function:

    #include <iostream>
    #include <string>
    using namespace std;    
    
    class Thing {
      private:
        string book;
      public:
        Thing(string initialBook = "Cat in the Hat") : book(initialBook) { }
        string getBook() const { return book; }
        void setBook(string bk) { book = bk; }
    };    
        
    int main() {
        Thing a;
        Thing b("Snow");
        Thing c(b);
        Thing *d = &c;
        Thing *e = new Thing();
        Thing *f = new Thing(b);
    
        // ... more to come ...
    }    
    

    To begin, we want you to draw a diagram using a style similar to examples in class on the blackboard, showing the underlying memory configuration that results from the above statements. Although you could experiement with such code on a computer, you are welcome to do this as a hypothetical. To get you going, here is the style of picture that we'd have in mind after the first two declaration.

    Note that the "memory address" we give on the right have no basis in reality. The actual addresses and the placement of the objects would be system dependent (and typically would be displayed in hexadecimal).

    And now, onto the real problem.

    1. Complete the memory diagram to reflect the state of affairs after all six of the original declaration statements above.

      Then predict the output that would be given at this point by the following code fragment. Do not simply give the generated output, but explain why such output is generated.

      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;

    For each subsequent part of this problem, consider the new line of code as if it were executed immediately following the initial block of code given above (that is, do not let the code from part ii affect your answer to part iii, etc.).

    1. Update your initial memory diagram to reflect the additional command
      b.setBook("Hop on Pop");
      and then explain what output would be generated by the following.
      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;
    2. Update your initial memory diagram to reflect the additional command
      c.setBook("Hop on Pop");
      and then explain what output would be generated by the following.
      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;
    3. Update your initial memory diagram to reflect the additional command
      a = b;
      and then explain what output would be generated by the following.
      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;
    4. Update your initial memory diagram to reflect the additional command
      b = a;
      and then explain what output would be generated by the following.
      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;
    5. Update your initial memory diagram to reflect the additional commands
      b = a;
      b.setBook("Hop on Pop");
      and then explain what output would be generated by the following.
      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;
    6. Update your initial memory diagram to reflect the additional commands
      e = d;
      e->setBook("Hop on Pop");
      and then explain what output would be generated by the following.
      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;
    7. Update your initial memory diagram to reflect the additional commands
      *e = *d;
      e->setBook("Hop on Pop");
      and then explain what output would be generated by the following.
      cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;

Extra Credit

  1. (2 points)

    TBA


Michael Goldwasser
CSCI 180, Fall 2008
Last modified: Thursday, 18 September 2008
Assignments | Class Photo | Computing Resources | Course Home | Lab Hours/Tutoring | Schedule | Submit