Chap 7 Constructors and Other Tools

Total Page:16

File Type:pdf, Size:1020Kb

Chap 7 Constructors and Other Tools

Homework #4 Name______Due February 20, 2006

TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false. 1) A static variable of a class cannot be changed.

2) It is legal to call a constructor as a member function of an object of a class, as in the code below. class A { public: A(){} A(int x, int y):xx(x), yy(y) {} // other members private: int xx; int yy; }; int main(){ A w; w.A(2,3); }

3) A class may not have another class type object as a member.

4) A constructor is a special kind of member function. It is automatically called when an object of that class is declared.

MULTIPLE CHOICE. Choose the alternative(s) that best complete the statement or answers the question. 5) Which of the following are legal access to the class or struct members? (Assume each is outside of the class member definitions.) struct S class C class D { { { int x; int x; public: int y; int y; int x; } private: int y; S s; int z; private: }; int z; C c; }; D d; A) d.z B) c.z C) c.x D) s.x E) d.x

6) Which of the following are accurate comparisons between call-by-value and const call-by- reference? A) Call-by-value copies the argument whereas const call-by-reference does not. B) Both protect against changing the caller's argument. C) Call-by-value uses more memory than const call-by-reference in making the copy. D) Both are very fast for all sizes of objects.

1 Homework #4

7) Which of the following are correct? A) A class can have only one constructor. B) A class can have only one default constructor. C) A constructor must be declared with a return type. D) A default constructor can only be provided by the compiler. E) A constructor must be declared for each class.

SHORT ANSWER. Write the word or phrase that best completes each statement or answers the question. 8) What is a default constructor? When does a class not have a default constructor?

9) Describe the differences between a call to an inline function member and a function that is not declared inline. What advantages are there to inline? What disadvantages?

10) Examine the definitions below. Rewrite the definition of this class so that functions f()const and g(const A& x) are inline. const int x = 17; class A { public: A( ); A(int n); int f( ) const; int g(const A& x); private: int i; };

2

Recommended publications