1. Protected base class members cannot be accessed by (a) global functions. (b) friends of the base class. (c) derived classes. (d) friends of derived classes. 2. Which data members and functions can a derived class access from its base class? (a) only public (b) public and private (c) only protected (d) public and protected 3. Assuming the definition, class Circle : public Point which of the following is false? (a) the colon( : ) in the header of the class definition indicates inheritance (b) the keyword public indicates the type of inheritance (c) All the public and protected members of class Circle are inherited as public and protected members, respectively, into class Point (d) Point is the base class and Circle is the derived class 4. A derived class cannot _________ access __________ members of its base class. (a) directly, protected (b) ever, private (c) indirectly, public (d) directly, private 5. Suppose class Wagon is a derived class from base class Car and that the member functions void Wagon::print( ) { cout << "I am a wagon\n"; } void Car::print( ) { cout << " I am a car\n"; } have been defined and that the following variables have been declared Car aCar; Wagon aWagon; What will be printed by the statements: aCar.print(); aWagon.print(); (a) I am a wagon, I am a wagon (b) I am a car, I am a car (c) I am a car, I am a wagon (d) I am a wagon, I am a car 6. _________ relationships correspond to ___________. (a) is a, composition (b) has a, composition (c) with a, inheritance (d) as a, inheritance 7. If objects of classes all derived from the same base class all need to draw themselves, the draw() function would most likely be (a) private (b) virtual (c) protected (d) global 8. The main difference between a pure virtual function and a regular virtual function is (a) the return type (b) the inheritance properties (c) a pure virtual function cannot have an implementation. (d) the location in the class. 9. Polymorphism is implemented via (a) member functions (b) virtual functions (c) inline functions (d) non-virtual functions 10. Which of the following are not allowed? (a) Objects of abstract classes. (b) Multiple pure virtual functions in abstract classes. (c) References to abstract classes. (d) Arrays of pointers to abstract classes.
class classA { public: virtual void print() const { cout << "ClassA x: " << x << endl;} void doubleNum(); classA(int a=0) {x = a;} private: int x; }; void classA::doubleNum() { x=2*x; } class classB: public classA { public: void print() const; void doubleNum(); classB(int a=0, int b=0):classA(a) { y=b;} private: int y; }; void classB::print() const { classA::print(); cout << "ClassB y: " << y << endl; } void classB::doubleNum() { classA::doubleNum(); y=2*y; }
int main() { classA *ptrA; classA objectA(2); classB objectB(3,5); ptrA = &objectA; ptrA->doubleNum(); ptrA->print(); cout << endl; ptrA = &objectB; ptrA->doubleNum(); ptrA->print(); cout << endl; return 0; }
void doubleNum();is replaced by
virtual void doubleNum();Now what is the output of the program?
B b; D d; B *pb; D *pd;Which of the following assignments are legal?
b = d; d = b; pd = pb; pb = pd; d = pd; b = *pd; *pd = *pb; pb = static_cast <B *> pd;