CS246 Midterm: CS 246 fall09ans

91 views4 pages
Final Exam Answers – CS 246 Fall 2009
Instructor: Peter Buhr
December 17, 2009
These are not the only answers that are acceptable, but these answers come from the notes or class
discussion.
1. (a) 3 marks A constructor is a special member used to perform initialization to ensure an object
is valid before use. It is called by the compiler immediately after object allocation.
(b) 2 marks Routine operators allow implicit conversions on both operands of an infix call.
(c) 4 marks Initialization involves a newly allocated object with undefined values. Assignment
involves an existing object that may contain previously computed values. The mechanism
used to implement each in C++ is the copy constructor and the assignment operator.
(d) 2 marks Ashallow-copy copies pointer values. A deep-copy copies the values referenced by
pointers (possibly recursively).
(e) 4 marks
1struct T2; // forward
struct T1 {
1 T2 *t2; // pointer, break cycle
1 T1(); // forward declaration
};
struct T2 {
T1 t1;
};
1 T1::T1() { t2 = new T2; } // can now see T2
T1 t1;
1struct T2; // forward
struct T1 {
1 T2 *t2; // pointer, break cycle
1 T1( T2 *t2 ) : t2( t2 ) {};
};
struct T2 {
T1 t1;
};
.5 T2 t2;
.5 T1 t1( t2 );
2. (a) 2 marks Type inheritance relaxes name equivalence by aliasing the derived name with its
base-type names.
(b) 1 mark routine pointers
(c) 5 marks
i. bp.f(); // Base::f
ii. bp.g(); // Base::g
iii. ((Derived &)bp).g(); // Derived::g
iv. bp.Base::h(); // Base::h
v. bp.h(); // Derived::h
(d) 3 marks Right picture
m2
m1
y
x
copy
y
x
direct routine pointer
y
x
VRT
m1
m2
m1
m2
indirect routine pointer
(e) 3 marks Adown cast is a dynamic check to determine the actual type an object pointed to by
apolymorphic pointer/reference.
1
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Final exam answers cs 246 fall 2009. These are not the only answers that are acceptable, but these answers come from the notes or class discussion. 1. (a) 3 marks a constructor is a special member used to perform initialization to ensure an object is valid before use. It is called by the compiler immediately after object allocation. (b) 2 marks routine operators allow implicit conversions on both operands of an in x call. (c) 4 marks initialization involves a newly allocated object with unde ned values. Assignment involves an existing object that may contain previously computed values. The mechanism used to implement each in c++ is the copy constructor and the assignment operator. (d) 2 marks a shallow-copy copies pointer values. A deep-copy copies the values referenced by pointers (possibly recursively). (e) 4 marks. T1( t2 *t2 ) : t2( t2 ) {}; T1::t1() { t2 = new t2; } // can now see t2.