Starting from:

$25

COEN79 Homework 3 Solved

1.      The sequence's insert member function normally puts a new item before the current item. What does insert do if there is no current item? 
 

2.      (4 pts) Modify the following code to generate the given output. Do not modify the main function. 


Output: 

Number of box objects created so far: 1 

Number of box objects created so far: 2 
 

3.      (6 pts) In the following code, indicate if the selected lines are legal or illegal: 

 

#include <iostream> 

  

class small { public:     small( ) {size = 0;};     void k() const;     void h(int i);     friend void f(small z); 

     private: 

    int size; 

}; 

  

void small::k() const 

{     small x, y;     x = y; // LEGAL/ILLEGAL? 

    x.size = y.size; // LEGAL/ILLEGAL? 

    x.size = 3; // LEGAL/ILLEGAL? 

};  void small::h(int i) 



     

};  void f(small z) 

{     small x, y;     x = y; // LEGAL/ILLEGAL? 

    x.size = y.size; // LEGAL/ILLEGAL? 

    x.size = 3; // LEGAL/ILLEGAL? 

    x.h(42); // LEGAL/ILLEGAL? 

}; 


int main() {          small x, y;     x = y; // LEGAL/ILLEGAL?     

    x.size = y.size; // LEGAL/ILLEGAL?     

    x.size = 3; // LEGAL/ILLEGAL?     

    x.h(42); // LEGAL/ILLEGAL?      

    return 0; 




4.    (4 pts) We create an array of fruit in the main function. How can we make sure that for all the items in array fruit_ptr the values of weight and color are equal to 1 and 2, respectively? Please show your solution. Do not modify the main function. 
 

5.    Explain why heap variables are essentially global in scope. Please present an example as well. 


6.    Is it possible to use the keyword “this” inside a friend function? Please explain your answer. 
 

7.    (4 pts) Does the following code compile? Does it run? Is there any problem with the code? If yes, how do you fix it?

 

19.         cout << "Employee::foo()";       
20.         c -> process();     
21.     }   
22. };   23.  
24. int main() {        
25.     Employee ob;     
26.     ob.foo();        
27.     return 0;   
28. }   
 
 
 

 

More products