Inheritance and Polymorphism

  1. OOP Design principles
    • Consistent abstraction in same class
    • Encapsulate implementation details
    • USe inheritance to simplify design and reuse code
    • Information hiding
    • Keep coupling loose
  2. Examples of coupling (Classes A and B)
    • Calling a member of A in B
    • Creating an instance of B in A
    • Using a A as a parameter in B's methods
  3. Bad coupling practice(s)
    Semantic/behavioral coupling
  4. Examples of good coupling
    • Simple data parameters
    • Simple object - instantiates an object
  5. What is semantic coupling?
    Assuming the behavior of a coupled class or it's methods
  6. Examples of semantic coupling (Classes A and B)
    • If class A has foo and bar methods, assuming foo will call bar internally in B (i.e not calling it explicitly in B)
    • B assuming methods exist for all instances of A
    • Class A setting a control flag of B
    • Some method of B uses global data after a method of A modifies it
    • Class A passes class C to B, but assumes only some fields will be used and fails to instantiate those that it doesn't expect B to use
  7. Why is semantic coupling bad?
    • Assumptions about class implementations
    • Likely implementations will change in future
    • Harder to track down problems
  8. What are abstract types?
    A collection of data and operations that work on some data
  9. Properties of abstract data types
    • Hidden implementation details (work with abstraction instead)
    • Easy to change implementation
  10. How to use "has a" relationships
    Use containment (data members)
  11. Types of object relationships
    • Containment "has a"
    • Inheritance "is a"
  12. How to override an interface?
    Use containment or inheritance
  13. If a base and derived class have a common method, which is called by default?
    The derived class implementation
  14. How to access base class implementation of a shared method?
    • Use scoping
    • baseName::methodName()
  15. What is not inherited by a derived class?
    • Overridden functions
    • Constructors
    • Destructors
    • Assignment operator
  16. How do friend functions handle inheritance?
    Friends of the base class are not automatically friends of the derived class
  17. If A is derived from B,
    Can A be converted to B?
    Can be be converted to A?
    • A can be converted to B
    • but B cannot be converted to A
    • A similar relationship is true for pointers
  18. How are constructors called in deeply inherited systems?
    The constructor from the deepest base class is called first and they are called in depth order
  19. Is it necessary to call the base class constructor manually?
    No, it will be called automatically unless otherwise stated
  20. How to set parameters for a base class constructor
    • Use initialization list
    • Derived::Derived(...) : Base{ <params> } { ...
  21. What order are destructors called in?
    Base class first and so on...
  22. What can access protected members
    • Current class
    • Derived classes
    • Friend functions
  23. Forms of inheritance
    • Specialization - New attributes and behaviors (public inheritance)
    • Specification - Base classes specifies some behaviour, but does not implement (public inheritance)
    • Restriction - Restrict base class methods (protected or private inheritance)
  24. Types of binding
    • Static (Compile time - Function overloading)
    • Dynamic
  25. How is dynamic binding achieved in C++
    • Using virtual functions
    • Matching functions in base classes are automatically declared virtual
  26. How to specify which version of a virtual function to call?
    Use a pointer of the type
  27. Properties of virtual destructors
    Base class destructors should always be virtual (making it virtual allows delete to call correct destructor)
  28. What is an abstract class
    A class that contains at least one pure virtual funciton
  29. What is a pure virtual function
    A function that is not implemented in a class
  30. How to determine runtime type of an object?
    • Include <typeinfo>
    • typeID(*<object>) //returns runtime type (must dereference param)
  31. How to specify multiple inheritance
    Class A : B, C, ... {
Author
Ant
ID
353607
Card Set
Inheritance and Polymorphism
Description
inherit
Updated