E N D
[5] Suppose there are only two constructors for a class, one that passes in a single integer parameter called amount, for setting an amount data member, and another that passes in a single integer parameter called code, for setting a code data member. Is this class valid? Why? no 由於兩個 constructor 的 parameter 都為一個 int,所以 overload 會輸入一個 parameter int 不知道要指向哪個 constructor.
2. [5] Suppose we write a function for a class which is not supposed to change any data members in the class. When we test the class, we get a runtime error. After tracking down the source of the runtime error, we realize that this function mistakenly changed the value of a data member. How will you do to fix the bug? 把 function const
3. [10] Suppose you find a class that matches your need. In this class, no constructors are provided. A function, called setValue, for setting the value of a data member is provided. In your application, for this particular data member, after the setValue function has been used once, there is no reason to use it again. You use this class very often in the program, so you want to save time from having to use a call to the setValue function every time. How will you do to achieve your goal? 建立一個 constructor 直接把 setValue的值輸入。
4. [10] Suppose you develop a WashingMachine class and we have a function called raisePrice, which passes in an amount to raise the price by. After the class has been in use by a number of clients, you wish to add more to the raisePrice function, passing in two parameters, the amount to raise the price by and the date the price is being raised. You plan to replace the old one-parameter raisePrice function by a new two-parameter raisePrice function in the WashingMachine class. It this a good idea? Why? No 由於 one-parameter 若改成 two-parameter 的話,之前有使用到的 function 都要改成 two-parameter。於是最好是在建立一個 two-parameter 的 function 並保留 one-parameter 的 function。
5. [5] Suppose the following is part of your program: int foo(int x) { x = x + x; return x; } void main() { int x = 5, y=10; y=foo(x); cout << “x = “ << x << “y = “ << y; } What are the values of x and y printed out by the cout statement? x=5, y=10;
6. [10] Suppose the following is part of your program: int foo(int & x) { x = x + x; return x; } void main() { int x = 5, y=10; y=foo(x); cout << “x = “ << x << “y = “ << y; } What are the values of x and y printed out by the cout statement? X=10, y=10;
[10] We would like to make a dynamic array that can store 10 Check objects, and we would declare arr to be the “name” of the array. (a) Which of the following lines would be used for the declaration: A. Check *arr = new Check arr[ 10 ]; B. Check arr = new Check [10]; C. Check *arr = new Check [10]; D. Check new *arr[10]; C
7. (b) What is the difference between “delete arr” and “ delete [] arr”? delete arr arr 500 500 delete [] arr arr 500 500
8. [5] We can always pass parameters into a function by call by reference, but we cannot always return by reference from a function. Why? 因為 function 執行完後, function 裡所宣告的資料會被釋放,傳 return by reference 所指向的資料已被釋放,所以抓不到值。
9. [5] Suppose a client makes a function foo in the main program which declares an Array<float> object locally. The return type of foo is Array<float>, and at the end of the function, the client returns the local Array object (without using return by reference) back to the calling function in the main program. The Array object is destroyed at the end of the function (being a local object), so why is there no problem in returning it? call by value
10. [5] Compare a class that uses an Array as a data member to a class that uses a dynamic array (pointer) in the private section. Why would we need to write a destructor, overloaded assignment operator, and copy constructor for the latter, but not the former? Destructor:若是有去定義動態記憶體 必須在執行後去釋放動態記憶體。 overloaded assignment operator, and copy constructor: 必須去定義一個 deep copy 去做動態記憶體裡的資料搬移(=)。 Ex a=b shallow copy: b=abc; a=b; b=xyz; 則a=xyz Deep copy: b=abc; a=b; b=xyz; 則a=abc;
11. [10] In the class, we showed the = operator for the Array class as follows: template <class DataType> ……………….. ::operator =( const Array<DataType> & right ) { . delete [ ] elements; deepCopy( right ); } Why don’t we just use the following code? template <class DataType> ……………….. ::operator =( const Array<DataType> & right ) { . deepCopy( right ); . } 沒加 delete [ ] elements; 造成 memory leak.
12. [10] In the class, we showed the = operator for the Array class as follows: template <class DataType> Array<DataType> & Array<DataType>::operator =( const Array<DataType> & right ) { if ( this == &right ) return *this; delete [ ] elements; deepCopy( right ); return *this; } Why don’t we just use the following code: template <class DataType> Array<DataType> & Array<DataType>::operator =( const Array<DataType> & right ) { delete [ ] elements; deepCopy( right ); return *this; } 當沒加 if 判斷式時,當 x=x 時,會錯誤,原因是因為執行 delete [ ] elements 時,左邊 x 會被釋放,當 x 被釋放時,右邊 x 也會被釋放,所以要加if判斷式。
13. [10] In the class, we showed that class MinBalCheckBook is derived from class CheckBook and we have the following code: template <class DataType> MinBalCheckBook<DataType>::MinBalCheckBook(float initBalance, float minBal, float sFee, float cFee ) : CheckBook<DataType>( initBalance ), minBalance( minBal ), serviceFee( sFee ), checkFee( cFee ) { } (a) What is the above code for? (b) Can the above code be rewritten as below? template <class DataType> MinBalCheckBook<DataType>::MinBalCheckBook(float initBalance, float minBal, float sFee, float cFee ) { CheckBook<DataType>( initBalance ); minBalance = minBal; serviceFee = sFee; checkFee = cFee; } (a) constructor (b) 不能直接呼叫constructor CheckBook.