Thinking in C++ Notes (14)

Chapter 14: Inheritance & Composition

  • Y is inherited from X then all private elements of X are still private in Y
  • all of the public members in the base class will be private in the derived class if the base class were not preceded by public
  • constructors of base-class objects and member objects must be called in the constructor initializer list
  • order of constructor & destructor calls
    • construction starts at the very root of the class hierarchy, and that at each level the base class constructor is called first, followed by the member object constructors
    • the destructors are called in exactly the reverse order of the constructors
    • the order of constructor calls for member objects is determined by the order they declared in the class

  • providing the exact signature and return type in the derived class definition as in the base class definition is called redefining(for ordinary member function) or overriding(virtual function)
  • anytime you redefine an overloaded function name from the base class, all the other versions are automatically hidden in the new class

  • constructors and destructors don’t automatically inherit and must be created specially for each derived class
  • operator= doesn’t inherit because it performs a constructor-like activity, but other operators are automatically inherited.
  • in the copy-constructor definition of the class, you must explicitly call the copy-constructor of the member object(or base-class object) or the default constructor will be automatically called instead
  • in the operator= definition of the class, you must explicitly call the operator= of the member object(or base-class object) or no assignment at all happens for the member object

  • Composition is used when you want the features of an existing class inside your new class, but not its interface
  • The is-a relationship is expressed with inheritance and the has-a relationship is expressed with composition

  • automatic type conversion happens only in function calls, not during member selection.(P607)
  • when you inherit privately, the object cannot be treated as a instance of the base class
  • private inheritance is included in C++ for completeness, but if for no other reason than to reduce confusion, you will usually want to use composition rather than private inheritance
  • private inheritance is useful when you want to hide part of the functionality of the base class
  • you can public privately inherited members by declare “using base::member” in the public section
  • “protected” means “ this is private as far as the class user is concerned, but available to anyone who inherits from this class”

  • upcasting is always safe because you’re going from a more specific type to a more general type
  • you must remember to call the base-class copy-constructor whenever you write your own copy-constructor
  • upcasting occurs: during the function call or during a simple assignment to a pointer or reference

Comments