Midterm Exam CSCI 6626 / 4526 .ZIP

Midterm Exam CSCI 6626 / 4526

1. Looking at main.cpp. (21 points)
This problem and the next several relate to the program that accompanies this exam. The line numbers in the exam questions refer to the numbered lines in that program. The program has a main program and two classes.
(a) (3) Line 13 creates a ParkingLot object. List the data members of this object.
(b) (4) Line 15 prints a \Normal termination" message. Just before that, there is output from a destructor. However, we do not call delete anywhere here. Why is that output printed?
(c) (3) What is the purpose of line 37? What is the answer?
(d) (3) List the line number(s) of everything in main.cpp that causes a Car object to be constructed.
(e) (4) This le contains a unitTest() function. What is a unit test supposed to do?
(f) (4) This le contains a leave() function. What is its purpose? Why is there not prototype for it?
2. The ParkingLot Class. (32 points)
(a) (4) Lines 62, 63, and 89 are necessary to make this application compile and link. Explain why.
(b) (2) Explain what you could write instead of these three lines, to accomplish the same purpose. Where would you write it?
(c) (4) On line 72, there is a const in the middle of the prototype, between the ) and the
f. What does that const mean? Under what conditions should we put a const in this position in a prototype?
(d) (4) Line 72 de nes an inline function, isFull(). Explain the design principle that caused me to put this function in the private part of the class.
(e) (3) The code in the ParkingLot destructor (line 76) has been blanked out. Please write an appropriate destructor.
(f) (4) Line 80 has two consts on it. What is made constant by the rst const? By the second one?
(g) (3) Give the line numbers of all the inline functions that are de ned in this class.
Midterm Exam: October 24, 2016 CSCI 6626 / 4526 2
(h) (4) The ParkingLot constructor is on lines 97..99. It has three ctors. In some circum-stances, ctors are required, at other times they are optional, and the initializations could be written as assignments in the body of the function. At least one ctor is required here.
Which one? Why is it required?
(i) (4) Find an example of using delegation somewhere in the le ParkingLot.cpp (lines
96..151). Give both of the line numbers involved in the delegation.
3. The Car Class. (26 points)
(a) (2) Lines 161..162 de ne an enumerated type. What is the integer code for the symbol mkNONE?
(b) (4) Explain how a static data member is di erent from a non-static member. Please note: static is not a synonym for const.
(c) (2) Line 163 is a static data declaration. Where is the initialization for this variable?
Give the line number(s).
(d) (4) Lines 180 and 181 contain two very similar de nitions of operator<<. One of these de nitions is used on line 148. Which one? Explain how the compiler chooses the correct de nition.
(e) (4) The function on lines 204..210 translates a string into an enum constant. How does it work? Why does it use a cast?
(f) (4) This class has no set functions. How does the data get into the objects? What design principle tells us to do it without setters?
(g) (2) Identify a function in Car.cpp that probably should be inline. Explain why.
(h) (4) Rewrite the constructor on line 195..197 without a ctor.
4. Special Functions (20 points)
(a) (2) What is a default constructor? Write an appropriate default constructor for the ParkingLot class.
(b) (4) Suppose the LAZ company commissioned this pilot project and now wants to extend it to model many parking lots stored in an array. This requires a default constructor for ParkingLot. Write an appropriate default constructor.
(c) (2) Copy constructors are used to implement calls on some of the Car functions. Name one Car function where a copy constructor will be used.
(d) (4) What kinds of constructors are \deleted" on lines 172..173?
(e) (4) The \delete" keyword on lines 174..175 means that we cannot do some kinds of things. Explain something we cannot do because this keyword is used on these lines.
(f) (4) I have tried to teach you the concept of tracking who has \custody" of an object.
Suppose you have a class with dynamic extensions. You can de ne move assignment to use when custody of a class object must change. How do you write your code to trigger calling move assignment instead of the usual copy assignment?

5. C++ and OO Programming (8 points)
In some situations, programming is just a means to solving a problem. Object-oriented programming aims to produce software that not only solves a problem but also has other desirable characteristics. The designers of C++ have attempted to create an efficient and exible language to support OO design.
(a) (4) Explain a feature of C++ that is di erent from C or Java or Python, but is very helpful for intermediate and advanced students.
(b) (4) List a di erent feature or characteristic of C++ that is important for large software projects designed and built by teams of professionals.
6. Disasters (12 points)
(a) (4) What is a dynamic extension of an object? Do Car or ParkingLot have dynamic extensions?
(b) (4) Explain the \shallow copy, deep delete" problem. Draw a diagram of an object and a copy of this object that illustrates your explanation.
(c) (4) Consider a program that contains the array and three pointers shown here:
p
q
s
Explain a way to crash the program by calling delete. What kind of error will this
cause?
Powered by