1. 程式人生 > >C++ Object Oriented programming

C++ Object Oriented programming

   Object Oriented programming is based on three fundamental conpects:data abstraction,inheritance,and dynamic binding.

   the key idea behind OOP is polymorphism. in C++,ploymorphism applies only to references or

pointers to types related  by inheritance.

   Dynamic binding lets us write programs that use objects of any type in an inheritance hierarchy without caring about the object's specific types.Programs that use these classes need not distinguish

between functions defined in the base or in a derived class.

   Protected Members,an important property: Aderived object may access the protected members of its base class only through a derived object.The derived class has no spicial access to the protected membes of base type objects.

  example:

 

Derived to Base Conversions

Because every derived object contains a base part,we can bind a base_type reference to the base-class part of a derived object. example:

Public,Private,and Protected Inheritance

   In Public inheritance,the members of the base retain their access levels.

   In Protected inheritance,the public and protected members of the base class are protected members

in the derived class.

   In private inheritance,all the members of the base are private in the derived class.

   All classes that inherit from base have the same access to the members in base,regardless of the access label in their derivation lists.the derivation access label controls the access that users of the derived class have to the members inherited from base.

a simple  example:

   Friendship and Inheritance

Frineds may access the class'private and protected data.

Handle Classes and Inheritance

   The handle class stores and manages a pointer to the base class.the type of the object to which that pointer points will vary,it can point at either a base-or a derived-type object.

Defining the handle

   In addition to managing the use count,the Sales_item class will define the dereference and arrow operators:

to be continue...