Chapter 15 OOP

15.1 What about OOP?
The key ideas in object-oriented programming are:
  • data abstraction
    • using data abstraction, we can define classes that separate interface from implementation. 
  • inheritance
    • through inheritance, we can define classes that model the relationships among similar types.
    • In C++, a base class distinguishes functions that are type dependent from those that it expects its derived classes to inherit without change. The base class defines as vitual those functions it expects its derived classes to define for themself. 
  • dynamic binding
    • thought dynamic binding, we can use objects of these types while ignoring the details of how the differ.
    • because the decision as to which version to run depends on the type of the argument, that decision can't be made until run time.
  • Defined Terms
  1. overide Virtual function defined in a derived class that has the same parameter list as a virtual in a base class override the base-class definition.
  2. overloaded Function that has the same name as at least one other fonction. Overloaded functions must differ in the number or type of their parameters.
  3. dynamic binding Delaying until run time the selection of which function to run(which function to run are based on the underlying type of the object to which a reference or pointer is bound).
 
15.2 The Base and The Derived Class
sometimes we define a class that we won't others to inherit from, we can prevent a class from being used as a base by follwing the class neme with final.
  • a base class
    • base classes ordinarily should define a virtual destructor. virtual destructors are needed even if they do no work.
  • a derived class
    • when we call a virtual function through a pointer or reference, the call will be dynamically bound.
    • a derived class may access the public members of its base class but may not access the private menbers.
    • the base and derived parts of an object are not guaranteed to be stored contiguously.
    • like any other code that creates an object of the base-class type, a derived class must use a base-class constructor to initialize its base-class part.
    • the scope of a derived class is nested inside the scope of its base class.
    • if a base class defines a static member, there is only one such member defined for the entire hierarchy.
    • the declaration does not include its derivation list, but the define needed(to know the different between a declaration and a define).
  • Conversions and Inheritance
    • we can bind a pointer or reference to a base-class type to an object of a type derived from that base class.
    • no implicit conversion from base to derived.
    • no conversion between object. look the define of sliced down.
    • if the base class has one or more virtual functions, we can use a dynamic_cast to request a conversion that is checked at run time. Alternatively,in those cases when we know that the conversion from base to derived is safe, we can use a static_cast to override the compiler.
  • Defined Terms
  1. static type Type with which a variable is defined or that an expression yields.Static type is known at compile time.
  2. dynamic type Delaying uyntil run time the selection of which function to run.
  3. sliced down what happens when an object of derived type is used to initialize or assign an object of the base type.The derived portion fo the object is"sliced down," leaving only the base portion, which is assigned to the base.
posted @ 2013-01-05 19:48  hydekm  阅读(86)  评论(0编辑  收藏  举报