The C ++ object-oriented programming of

------ core idea of ​​object-oriented programming are: data abstraction, inheritance, and dynamic binding.


 

A, defines the base class and derived classes

  • Defines the base class

  You should always have a base class virtual destructor, even if the function performs no actual operation is true.

  • Member function and inheritance

  In C ++, it must be the base class member function to separate two: one is the base class for derived classes covered desired function; one is the base class inherits directly derived classes desired function. For the former we want to define it as a virtual function (Virtual function) , when we use a pointer or reference when you call a virtual function, the call will be dynamic binding . The different objects bound reference or pointer , the call may perform a version of the base class version is also possible to perform the derived class.

  Before the base class member function by adding virtual key to cause the dynamic binding function is executed, any non-static function outside constructor can be virtual. Virtual keyword can only occur inside the class declaration statements before and not external function definition for the class. If the base class virtual function is a function declaration, then the derived class is also an implicit function of the virtual function .

  If a member function is not declared virtual, it occurs in the resolution process rather than run compile time.


 

  • Definition of the derived class

  Derived classes must use the class derivation list (class derived list) clearly indicate that it is inherited from the class to which the. Class derivation list as:

class Bulk_quote : public Quote{
}

 

 Access specifier role is to: control derived class inherited members is visible to the user derived class from the base class. If the derivative is a public, public members of the base class is a derived class interface part. Public Object of derived type also can be bound to a reference or pointer to the base class.

  • The derived class virtual function

  C ++ allows derived classes to explicitly destined to cover one of its member functions inherited virtual function. It consists in the parameter list later , or in the back of the const keyword const member functions , or in reference to a member function of a reference qualifier later add a keyword override.

  May be a pointer or reference to the base class is bound to the base part on the derived class object, this conversion is called a derived class to the base class (derived-to-base) type conversion.

  • The derived class constructor

  Each member class controls its own initialization procedure, it must also use the derived class constructor for the base class to initialize its base part. The base part and a derived object of the derived class object data members are in their initialization phase constructor the initialization operation is performed. First, the initialization part of the base class, then the order of declaration of the members of the derived class initialization sequence.

  Each class is responsible for defining its own interface, in order to interact with the object class must use the interface of the class, even if the object is part of the base class derived class as well.

  • Inheritance of static members

  If the base class defines a static member, the member is uniquely defined only exists in the whole system. No matter derived from the base class in a number of categories, each unique instance there is only static members. Static members follow a common orientation control rules.

  • Class can be used as the base class

  Class can be used as the base class, it must be defined, rather than just the statement. That can not derive a class of its own.

  • To prevent the occurrence of a succession

  If a class does not want other classes inherit it, namely a keyword followed by the class name in the final.

 

1 class NoDerived final {/*...*/}

 

 

 

  


 

  • Casting between base and derived classes

  We pointer references the base class, and bound to a derived object , if (or when a pointer) using a reference the base class, we do not know the type of reference or pointer to the real object being bound, the base class may be objects, it may be a derived class object.

  • Static versus dynamic typing

   Static type is known at compile time, which is generated when the type or expression type of variable declarations. Dynamic type memory variable or expression is represented in the model object.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

Guess you like

Origin www.cnblogs.com/ccpang/p/11360467.html