Course Home | Homework | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science A220/P126
Data Structures and Object-Oriented Programming

Michael Goldwasser

Spring 2005

Dept. of Math & Computer Science

Homework Assignment 01

Object-Oriented Programming

Contents:


Overview

Our goal of this homework is to review some of the syntactic issues of object-oriented programming in C++ which may or may not be review, depending on a student's previous background. We will use Ch.  as the reading for this assignment, though some of the issues in that chapter are subtle and will need to be revisited later in the course.

Topic: Object-Oriented Programming
Related Reading: Ch. 1
Due: Friday, 21 January 2005, 1: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 variants in argument passing, as described in Ch. 1.4.1 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;
      }
      
    

  2. (8 points)

    This problem explores access control specifiers, as described in Ch. 1.5.1 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;
    
    Which of the following statements are allowed?
    1. hyundai.price = 4999.99;
    2. jaguar.profit = 4000.00;
    3. jaguar.setPrice(30000.97);
    4. aPrice = jaguar.getPrice();
    5. aProfit = jaguar.getProfit();
    6. aProfit = hyundai.getProfit();
    7. if (hyundai==jaguar) cout << "Want to swap cars?";
    8. hyundai = jaguar;

  3. (6 points)

    This problem explores static vs. dynamic allocation of objects and the use of the assignment operator, and the use of templates, namely the vector class from the Standard Template Library (STL), Most of this material is covered in some form as part of of Ch. 1.5 of the text. Pay particular note to the example on the top of page 38, and on page 45.

    Consider the following block of code as a starting point:

    #include <iostream>
    #include <vector>
    using namespace std;    
    
    vector<int> a;
    vector<int> b(100);
    b.at(30) = 2222;
    vector<int> c(200);
    c.at(30) = 3333;
    vector<int> d(b);
    vector<int> *e = new vector<int>(100);
    vector<int> *f = new vector<int>(b);
    

    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.

    In fact, to help you out, here is a program allocation.cpp which will simulate this entire problem for you. Simply compile and execute the code and the main routine will perform a loop, independently simulating each of the six problem parts.

    1. cout << b.at(30) << endl;
      cout << c.at(30) << endl;
      c = b;
      cout << b.at(30) << endl;
      cout << c.at(30) << endl;
    2. cout << b.at(30) << endl;
      cout << c.at(30) << endl;
      b = c;
      cout << b.at(30) << endl;
      cout << c.at(30) << endl;
    3. cout << a.at(30) << endl;
      a = b;
      cout << a.at(30) << endl;
    4. cout << b.at(30) << endl;
      cout << d.at(30) << endl;
      b.at(30) = 5555;
      cout << b.at(30) << endl;
      cout << d.at(30) << endl;
    5. e->at(30) = 6666;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;
      f = e;
      f->at(30) = 7777;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;
    6. e->at(30) = 8888;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;
      *f = *e;
      f->at(30) = 9999;
      cout << b.at(30) << endl;
      cout << e->at(30) << endl;
      cout << f->at(30) << endl;

Extra Credit

  1. (2 points)

    Reinforcement Exercise R-1.2 on page 57 of the text.


Michael Goldwasser
CS A220/P126, Spring 2005
Last modified: Thursday, 13 January 2005
Course Home | Homework | Programming | Schedule & Lecture Notes | Submit