Quiz — Class design
This quiz tests you on a variety of aspects of class design. Examples of questions you may be asked include the following:
- complete the definition of a class or class template;
- distinguish the various kinds of inheritance;
- justify the need for a friend function or class, an overloaded operator, or a reference-valued function; or
- predict the behavior of code that uses inheritance.
Readings
A Computer Science Tapestry, sections 9.4 (operator overloading), 11.2, 11.3, and 12.3 (templates), chapter 13 (inheritance), and appendix E (operator overloading).
C++ Program Design, sections 9.3 through 9.5 (operator overloading), 10.6 (iterators), 12.9 (operator overloading), and chapters 13 (inheritance) and 14 (templates and polymorphism) except for material on multiple inheritance.
Computing Fundamentals with C++, section 11.2 (iterators) and chapters 16 (inheritance), 17 (templates), and 18 (operator overloading). Chapter 16 does not provide all the information about inheritance that you'll need to know in this quiz.
Sample exercises
Exercises in section 9.4 of A Computer Science Tapestry, exercises 9.2, 9.7, 9.8, and 9.9 at the end of chapter 9, exercises in sections 11.2, 11.3, and 12.3, and all exercises in chapter 13 will be useful.
Exercises 9.26, 9.27, 9.29, 9.32, 9.33, 10.44, 10.46 through 10.53, 12.45, 12.49, 12.50, and all exercises in chapters 13 and 14 of C++ Program Design will be useful.
All exercises in chapters 17 and 18 of Computing Fundamentals with C++, including programming projects, will be useful.
Sample quiz
- Explain why the >> stream extraction operator must be overloaded as a friend function rather than a member function of the class whose value is extracted.
- The assignment operator in the vector class is overloaded as follows.
const vector<itemType> &operator= (const vector<itemType> &);This is typically done to enable concatenated vector assignments such as x = y = z. Consider the use of the following declarations to overload the assignment operator:const vector<itemType>operator= (const vector<itemType>); const vector<itemType>&operator= (const vector<itemType>); const vector<itemType>operator= (const vector<itemType>&);Do they work? If so, briefly explain; if not, describe the error messages produced by the other two for the codevector<itemType> a, b, c; ... a = b = c;void Sort (vector<int> &values) { for (int k=1; k<values.length(); k++) { // insert element k into the appropriate position // in values[0] ... values[k-1]. int j, temp = values[k]; for (j=k-1; j>=0 && temp<values[j]; j--) { values[j+1] = values[j]; } values[j+1] = temp; } }Rewrite it as a member function of a Collection class template that represents a collection of objects of the Collection parameter type. Assume that the Collection values are stored in a vector object named values.
- Suppose that the Collection parameter type of exercise 3 is itself a class. Which member functions must it provide for your Sort function to compile without error? Which additional member functions ought to be provided as well?
- A program has been written to manipulate shapes. It declares a base Shape class as follows, along with numerous classes, such as Point, Circle, and Cylinder, derived from Shape.
shape.h:
class Shape { public: virtual float Area ( ); virtual float Volume ( ); virtual void PrintShapeName ( ) const = 0; };Shape::Area ( ) { return 0.0; } Shape::Volume ( ) { return 0.0; }
- Add an integer member myFill to class Shape that is accessible to each object of a class derived from Shape, but is not publicly accessible.
- Explain which of the functions Area, Volume, and PrintShapeName must be defined as member functions of a class derived from Shape in order for the class to compile without errors, and which functions need not be so defined.
- The program also includes a vector of shapes, along with code that prints the areas of the shapes as follows:
vector<Shape *> vectorOfShapes(N); for (int k = 0; k < N; k++) { vectorOfShapes[k]->PrintShapeName(); cout << ": area = " << vectorOfShapes[k]->Area (); cout << endl; }Suppose that all the derived classes include declarations of Area and PrintShapeName functions. What would be the effect on this code of removing the declaration of PrintShapeName or Area from the class Shape declaration? What would be the effect of removing the keyword virtual from the declaration of PrintShapeName or Area in the class Shape declaration?Solutions to sample quiz questions
- The left hand side of any infix operator overloaded as a member function of some class C must be an object of type C. Thus, for >> to be overloaded as a member function so that the expression cin >> x is legal, it has to be overloaded as a member function of cin's class, namely istream. (The expression
object-of-class-C >> operandis translated toobject-of-class-C . operator>> (operand)as a result.)a.operator= (b.operator= (c))Consider each of the possibilities in turn. Withconst vector<itemType>operator= (const vector<itemType>);the argument to operator= and its return value both involve copying the vector, which is slow but legal. Withconst vector<itemType>&operator= (const vector<itemType>);b = c returns a reference to an object, and this object is passed by value (i.e. copied) for the assignment to a. Again, this is slower than necessary, but legal. Finally, considerconst vector<itemType> operator= (const vector<itemType>&);The value of the assignment b = c is a temporary copy of something, which is then passed by reference in the assignment to a. This is suspicious, since one usually can't create a reference to a temporary object. However, it is allowed if the argument is a reference to a const object; without the const, a type mismatch error is generated by the compiler.template <class T> void Collection<T>::Sort ( ) { for (int k=1; k<values.length(); k++) { int j; T temp = values[k]; for (j=k-1; j>=0 && temp<values[j]; j--) { values[j+1] = values[j]; } values[j+1] = temp; } }
- The operations performed on class T values are initialization (in the statement T temp = values[k]), assignment, and comparison with <. The initialization calls the copy constructor for T. When not overloaded, assignment is implemented by memberwise copying, as is the copy constructor. There is no default behavior for <, so a definition for operator< must be provided.
If one comparison operator is overloaded, the others should be also.
a.
protected: int myFill;b. Area and Volume have definitions that derived classes inherit and thus need not provide. PrintShapeName is a pure virtual function, so derived classes must supply their own PrintShapeName definition.
c. Even though the derived classes supply their own Area and PrintShapeName functions, those functions must still be declared in class Shape since the code in the loop thinks it's dealing only with Shape objects. Removing the keyword virtual from the Area declaration causes the default Area function—the one that always returns 0—to be used for all the derived classes, again since the loop code thinks it's dealing only with Shape objects. Removing virtual from the PrintShapeName declaration produces a syntax error.
![]()
![]()
![]()