Saint Louis University |
Computer Science A220/P126
|
Dept. of Math & Computer Science |
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.
class Top { public: Top (); int look(); private: int value; void change(int v); }For each of the commented statements below, indicated whether they are legal or illegal.
int main() { Top t; t.value = 3; // legal or illegal? int k = t.look(); // legal or illegal? t.change(3); // legal or illegal? return 0; }
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; }
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?
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.
cout << b.at(30) << endl;
cout << c.at(30) << endl;
c = b;
cout << b.at(30) << endl;
cout << c.at(30) << endl;
cout << b.at(30) << endl;
cout << c.at(30) << endl;
b = c;
cout << b.at(30) << endl;
cout << c.at(30) << endl;
cout << a.at(30) << endl;
a = b;
cout << a.at(30) << endl;
cout << b.at(30) << endl;
cout << d.at(30) << endl;
b.at(30) = 5555;
cout << b.at(30) << endl;
cout << d.at(30) << endl;
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;
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;