Chapter 15) Inheritance, Polymorhphism, and Virtual Functions

Ace your homework & exams now with Quizwiz!

3. How does base class access specification differ from class member access specification?

3. Base class access specification specifies how members of the base class are inherited by the derived class. Member access specification specifies how class members may be accessed by code outside the class.

15.2 Protected Members and Class Access

CONCEPT: Protected members of a base class are like private members, but they may be accessed by derived classes. The base class access specification determines how private, public, and protected base class members are accessed when they are inherited by the derived classes.

15.3 Constructors and Destructors in Base and Derived Classes

CONCEPT: The base class's constructor is called before the derived class's constructor. The destructors are called in reverse order, with the derived class's destructor being called first.

15.8 Multiple Inheritance

CONCEPT: Multiple inheritance is when a derived class has two or more base classes.

36. Suppose a class named Tiger is derived from both the Felis class and the Carnivore class. Here is the first line of the Tiger class declaration: class Tiger : public Felis, public Carnivore Here is the function header for the Tiger constructor: Tiger(int x, int y) : Carnivore(x), Felis(y) Which base class constructor is called first, Carnivore or Felis ?

Carnivore

31. A(n) __________ of inheritance is where one class is derived from a second class, which in turn is derived from a third class.

Chain

42. T F Protected members of a private base class become public members of the derived class.

False, Protected members of a private base class become PRIVATE members of the derived class.

43. T F Public members of a protected base class become private members of the derived class.

False, Public members of a protected base class become protected members of the derived class.

38. T F The base class's access specification affects the way base class member functions may access base class member variables.

False, The base class's access specification affects the way the derived class member functions may access base class member functions.

52. T F A base class may not be derived from another class.

False, think Inheritance Hierarchy

50. T F A member function of a derived class may not have the same name as a member function of the base class.

False; When a derived class

20. When both a base class and a derived class have constructors, the base class's constructor is called __________ (first/last).

First

21. When both a base class and a derived class have destructors, the base class's constructor is called __________ (first/last).

Last

19. A derived class inherits the __________ of its base class.

Member variables and member functions

40. T F Private members of a private base class become inaccessible to the derived class.

True

41. T F Public members of a private base class become private members of the derived class.

True

44. T F Private members of a protected base class become inaccessible to the derived class.

True

47. T F The base class destructor is called after the derived class destructor.

True

51. T F Pointers to a base class may be assigned the address of a derived class object.

True

49. T F Arguments are passed to the base class constructor by the derived class constructor.

True The general format of this type of constructor declaration is: ClassName::ClassName(ParameterList) : BaseClassName(Arguementlist)

34. Write the first line of the declaration for a Poodle class. The class should be derived from the Dog class with public base class access.

class Poodle : public Dog

35. Write the first line of the declaration for a SoundSystem class. Use multiple inheritance to base the class on the CDplayer class, the Tuner class, and the CassettePlayer class. Use public base class access in all cases.

class SoundSystem : public CDplayer, public Tuner, public CassettePlayer

8. When does static binding take place? When does dynamic binding take place?

Static binding - The process of matching a function call with a function at compile time. Dynamic binding - C++ determines which function to call at runtime, depending on the type of the object responsible for the call.

33. Multiple inheritance opens the opportunity for a derived class to have ambiguous members. That is, two base classes may have member functions of the same name. In situations like these, the derived class should always __________ .

redefine or override the member functions.

25. _________ binding is when the compiler binds member function calls at compile time.

static

24. A(n) __________ member function in a base class expects to be overridden in a derived class.

virtual

29. A(n) __________ class cannot be instantiated.

Abstract Base Class

48. T F It isn't possible for a base class to have more than one constructor.

False

45. T F Protected members of a public base class become public members of the derived class.

False, Protected members of a public base class become stay protected.

46. T F The base class constructor is called after the derived class constructor.

False, The base class constructor is called before the derived class constructor.

23. When a derived class redefines a function in a base class, which version of the function do objects that are defined of the base class call? __________

Base Class version

39. T F The base class's access specification affects the way the derived class inherits members of the base class.

True

9. What is an abstract base class?

An abstract base class is not instantiated itself, but serves as a base class for other classes. The abstract base class represents the generic, or abstract form of all the classes that are derived from it. A class is abstract when it has one or more virtual functions.

6. Which constructor is called first, that of the derived class or the base class?

Base class constructor

15.7 Abstract Base Classes and Pure Virtual Functions

CONCEPT: An abstract base class cannot be instantiated, but other classes are derived from it. A pure virtual function is a virtual member function of a base class that must be overridden. When a class contains a pure virtual function as a member, that class becomes an abstract base class.

15.4 Redefining Base Class Functions

CONCEPT: A base class member function may be redefined in a derived class.

15.5 Class Hierarchies

CONCEPT: A base class member function may be redefined in a derived class.

15.6 Polymorphism and Virtual Member Functions

CONCEPT: A base class member function may be redefined in a derived class.

15.1 What Is Inheritance?

CONCEPT: Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions (except the constructors and destructor) of the class it is based on.

11. What base class is named in the line below? class Pet : public Dog

Dog

2. A program uses two classes: Dog and Poodle . Which class is the base class and which is the derived class?

Dog - Base Class Poodle - Derived Class

26. _________ binding is when a function call is bound at runtime.

Dynamically

32. _________ is where a derived class has two or more base classes.

Multiple Inheritance General format: class DerivedClassName : AccessSpecification BaseClassName, AccessSpecification BaseClassName [, ...]

5. Can a derived class ever directly access the private members of its base class?

No

10. A program has a class Potato , which is derived from the class Vegetable , which is derived from the class Food . Is this an example of multiple inheritance? Why or why not?

No, Multiple inheritance is when a derived class has two or more base classes. This is an example of Class Hierarchies.

28. When a pointer to a base class is made to point to a derived class, the pointer ignores any __________ the derived class performs, unless the function is __________.

Overrides; virtual

12. What derived class is named in the line below? class Pet : public Dog

Pet

27. _________ is when member functions in a class hierarchy behave differently, depending upon which object performs the call.

Polymorphism

14. What is the class access specification of the base class named below? class Pet : Fish

Private

15. Protected members of a base class are like __________ members, except they may be accessed by derived classes.

Private

4. What is the difference between a protected class member and a private class member?

Protected members of a base class are like private members, except they may be accessed by functions in a derived class. To the rest of the program, however, protected members are inaccessible.

13. What is the class access specification of the base class named below? class Pet : public Dog

Public

30. A(n) __________ function has no body, or definition, in the class in which it is declared.

Pure Virtual Function

22. An overridden base class function may be called by a function in a derived class by using the __________ operator.

Score Resolution Operator (::) Takes the form of: BaseClassName::functionName(Arguements)

7. What is the difference between redefining a base class function and overriding a base class function?

When a derived class has a function with the same name as a base class's function, and the base class function is not virtual, it is said that the function is redefined in the derived class. If the base class's function is virtual, however, it is said that the function is overridden. There is a distinction between redefining a function and overloading a function. An overloaded function is one with the same name as one or more other functions, but with a different parameter list. The compiler uses the arguments passed to the function to tell which version to call. Overloading can take place with regular functions that are not members of a class. Overloading can also take place inside a class when two or more member functions of the same class have the same name. These member functions must have different parameter lists for the compiler to tell them apart in function calls. Redefining happens when a derived class has a function with the same name as a base class function. The parameter lists of the two functions can be the same because the derived class function is always called by objects of the derived class type.

1. What is an "is a" relationship?

When one object is a specialized version of another object, there is an "is a" relationship between them. This indicates that one class "is a" specialized version of the other class.


Related study sets

Foundations of Information Systems chap 1 sammary

View Set

Chapter 1: Introducing Health Psychology

View Set

A&P II Exam 4 Chapter 26: Fluid, Electrolyte, and Acid-Base Balance

View Set

(Med-Surg I) Final Exam Multiple Choice Questions

View Set

Final Exam US history comprehensive chapter 1 - 15

View Set