CSCI 241 Review Sheet 2.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

10/18/24, 7:30 PM CSCI 241 Review Sheet 2 CSCI 241 Midterm 2 Review Sheet Function overloading Know how to do it Know the criteria that are used/not used to distinguish between the functions...

10/18/24, 7:30 PM CSCI 241 Review Sheet 2 CSCI 241 Midterm 2 Review Sheet Function overloading Know how to do it Know the criteria that are used/not used to distinguish between the functions Operator Overloading When we define a class, we can extend the definitions of some of the standard operators to make the operators work with the class as well as with the predefined data types. Overloading different operators can involve different problems. Keep in mind some examples. For instance, in class, we looked at the Rational class, which represents rational numbers. For the Rational class, we can easily overload the operators ==, !=, >, =, , and ==? Which of these can be const methods? Which is normally a friend function? Which ones have to be written twice? 2. Suppose I have a variable K of type Pile *. In my program, I have a line K = new Pile; and later I have a line delete K; What is wrong with this? What would be a better way to do it? Suppose instead I simply K = 0; Is that any better? 3. I want to have a class called Pair which provides ordered pairs (A, B) of elements of type float. The data elements of class Pair are two items of type float. a. Can I use the default copy constructor, destructor and assignment operator for the Pair class? b. Write the code for the equality operator == for the Pair class, c. Suppose I want to have a method called MakeMirror which returns a Pair containing the same two elements in the opposite order. (If the current instance contains (A, B), then MakeMirror() gives us (B, A).) Write the prototype statement for MakeMirror. d. Suppose I want to have a bool-valued function called IsMirror which will return true if two Pair objects contain the same elements in the opposite order. Write the prototype for IsMirror as a friend function. 4. Which of the following is not always provided as a default for every class (even though the default version may not be what we want)? A. basic constructor B. assignment operator C. operator [] D. copy constructor E. destructor 5. Suppose we have the following class: class Bundle { private: float Height; https://faculty.cs.niu.edu/~hutchins/csci241/t2rev.htm 3/4 10/18/24, 7:30 PM CSCI 241 Review Sheet 2 int * List; // dynamically allocated array of int int Size; // size of list public: Bundle(); Bundle(Bundle & Other); // more stuff }; We would also like to have an assignment operator. Here is some code for it. Fill in the blanks. ____________ Bundle::operator =(const Bundle & Other) { if (_________________________) { List = __________________; delete __________________; for (int I = 0; I < ____________; I++) { ________________________; } Height = Other.Height; } return _____________; } 6. For the Bundle class as described above, write the destructor. 7. For the Bundle class as described above, write the copy constructor. 8. Suppose we are overloading the insertion operator (output)

Use Quizgecko on...
Browser
Browser