(COMP2012)[2016](f)midterm_=vngkoee^_10579.pdf

Decide whether the statement is syntactically

Info icon This preview shows pages 3–6. Sign up to view the full content.

View Full Document Right Arrow Icon
decide whether the statement is syntactically INCORRECT – that is, it will produce compilation error(s). Circle ”Yes” if it will give compilation error and ”No” otherwise. 1 point for each correct answer of ”Yes / No” question total 6 points (b) In line #7, the const keyword appears three times. Briefly explain the meaning of each const. (from left to right) The first const means p is a pointer variable that points to a const char. It does not allow modification of the data through the pointer. First const: The second const means that p is a constant pointer, which the address stored cannot be changed. Second const: The third const means print is a constant member function that guarantees it will not change any data members or call any non-const member functions. Third const: 1 point for each correct explanation of const total 3 points 3 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Problem 3 [10 points] Member Initialization (a) [3 points] Suppose we have a user-defined type A with default constructor. Is there any difference between the following two statements? Explain why. A obj; A obj(); They are different. The first statement: A obj; defines an object named obj of type A and the constructor invoked is the default one. The second statement: A obj() is the prototype of function named obj with no argument and the returned value is an object of type A. 1 point for stating they are different. 2 points for the explanation. Answer: (b) [3 points] Is the default constructor for class A always A::A()? If not, explain why. No, default constructor for class A does not have to be always A::A(). Constructor with default values could also be default constructor as well, for instance: A::A(int a = 10). 1 point for stating the default constructor for class A is NOT always A::A(). 2 points for the explanation. Answer: (c) [2 points] Will a constructor of class A be called when we create an array of that type? If so, which constructor get invoked? Yes, a constructor of class A will be called. The one gets called is default constructor. 1 point for stating constructor of A is called. 1 point for stating default constructor will be invoked. Answer: (d) [2 points] Suppose someone wants to initialize a const variable in primitive type within the class definition. Does it make sense? If not, in such case what else should the pro- grammer do? It doesn’t make sense to initialize a const variable in primitive type within the class definition. In fact, it should be initialized using member initialization list. 1 point for stating it is illegal to initialize a const variable within class definition. 1 point for stating the use of member initialization list. Answer: 4 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 4
Problem 4 [10 points] Order of Construction & Destruction #include < iostream > using namespace std; class ExhaustFan { public : ExhaustFan() { cout "E" endl; } ExhaustFan() { cout " E" endl; } } ; class
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern