Course Home | Homework | Lab Open Hours | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science 180
Data Structures

Michael Goldwasser

Spring 2007

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: Monday, 5 February 2007, 2:10pm

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:
        void setPrice(double newPrice);
        void setProfit(double newProfit);
        double getPrice();
      private:
        double price;
        double profit;
        double getProfit();
      };
    
    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 aPrice, aProfit;
    
    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.profit = 4000.00;
    3. jaguar.setPrice(30000.97);
    4. aPrice = jaguar.getPrice();
    5. aProfit = 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 ...
    }    
    

    For each part of this problem, assume the given lines of code immediately follow the initial block of code given above (however each of the following problems is independent; that is, the code given for part 'i' should not effect part 'ii')

    Your goal is to explain the behavior of each code fragment. You are not simply to describe the generated output but must also explain why such output is generated.

    1. 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. b.setBook("Hop on Pop");
      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. c.setBook("Hop on Pop");
      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. b = a;
      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. a = b;
      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. b = a;
      b.setBook("Hop on Pop");
      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. e = d;
      e->setBook("Hop on Pop"); cout << a.getBook() << endl;
      cout << b.getBook() << endl;
      cout << c.getBook() << endl;
      cout << d->getBook() << endl;
      cout << e->getBook() << endl;
      cout << f->getBook() << endl;
    8. *e = *d;
      e->setBook("Hop on Pop"); 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, Spring 2007
Last modified: Tuesday, 06 February 2007
Course Home | Homework | Lab Open Hours | Programming | Schedule & Lecture Notes | Submit