6 Consider the following main function: int main() { Time t(2, 50); t.setHour(5)
ID: 3868621 • Letter: 6
Question
6 Consider the following main function: int main() { Time t(2, 50); t.setHour(5); t.setMinute(20); cout << "The time is " << t << endl; } What is necessary in the Time class to make this main function compile? None of the above. A friend function called t is needed. The << operator must be overloaded for the Time class The Time class must have the data member t defined in the public area of the class. 7 In which of the following ways can you write an operator overload? (Select all that apply) in a class member function in a friend function in a nonmember function in the main function 8 Suppose cursor is a pointer that points to a penguin node in a linked list using a penguin class that includes a member function get_link(). You wish to move cursor to point to the next node (to the next penguin) in the list. Which statement do you use? cursor++; cursor = cursor->get_link( ); cursor.get_link(); cursor = get_link(); 9 Suppose cursor points to a penguin node in a linked list using a penguin class, which includes functions called get_data() and get_link(). What Boolean expression should be true when that penguin node is the ONLY node in the list? cursor->get_data( ) == 0.0 cursor->get_link( ) == NULL cursor == NULL cursor->get_data( ) == NULL 10. The following steps have been performed for deleting a node from a linked list. The nodes have the member accessor function get_link(). The head_ptr points to the head node of a list. Step 1: Set up a local variable called remove_ptr and set it equal to head_ptr Step 2: Set head_ptr equal to the head_ptr->get_link() Step 3: Delete remove_ptr Which of the following situations should use these steps? To delete ALL nodes from a list To delete a node from the middle (neither front nor end) of a list. To delete a node from the front of a list To delete a node from the end of a list 11. Which operation is better for storing items in an array than storing items on a linked list? deleting an item at the beginning (position 0). inserting an item at the beginning (position 0). Growing the size of the container. retrieve a copy of the item at location i. 12. You are working with a node class named node. Suppose that f is a non-member function with a prototype like this with a missing data type in the parameter list: void f(________ head_ptr); // Precondition: head_ptr is a head pointer for a linked list. // Postcondition: The function f has done some computation with // the linked list, but the list itself is unchanged. What is the best data type for head_ptr in the parameter list of this function? node node* & node* node& 13. A virtual function provides you with the ability to have several different functions with the same name in a parent and children classes and have C++ figure out which one to call at run-time. True False 14. Suppose cursor points to a node in a linked list (using the node definition with member functions called get_data and get_link). What Boolean expression will be true when cursor points to the tail node (last node) of the list? cursor == NULL None of the above cursor->get_link( ) == NULL cursor->get_data( ) == NULL 15. You wish to convert a container class to a template container class. Which of the following should you consider? Make sure operations on container data are not type specific. For example, don't use string functions on container data. Make the class visible to the client. One way to do this is to include the main function in the header file. Make implementation visible. One way to do this is to include both interface and implementation in the header file. Make sure all return statements that return container data only return container data. If a return statement returns container data under certain conditions, but another return statement returns something that is not container data under other conditions, there could be a data type conflict when a different data type is used for this template.
Explanation / Answer
6 The operator must be overloaded. Since the cascading operator is being used the given object "t" it is needed to be overloaded.
7 Operator overloading is done normally using member functions, but it can be done using friend functions too. Friend functions come to the picture when the access to private members is also needed. But if the access is not necessary then it can be done using non-member functions altogether. So all the options except for the last one are correct.
8 cursor = cursor->get_link( );
Since the cursor is a pointer, we need to use the arrow to access the member function.
9 cursor->get_link( ) == NULL
Since cursor points to the only element, the next element it would get address of doesnt exist. Thats why the address would be NULL.
10. To delete a node from the front of a list. It is pretty self explanatory.
11. Retrieve a copy of the item at location i. Since array uses contiguous memory locations for storage, indexing takes O(1) complexity.
12. It should be node*, as we only need the address of the starting point.
13. True
14. cursor->get_link( ) == NULL
This is because it is the last element, if we try to find the next element we will find nothing, NULL address!
15. Make sure operations on container data are not type specific. As templates are only for the purpose of making a function or class that is generic for every data type.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.