C ++ object-oriented inheritance and polymorphism

Chapter XIII polymorphism and inheritance

 

13.1 Inheritance and derivation

1. inheritance and derived syntax:

Class derived class name: class name of the base class inherited methods

{

// add coverage properties and behavior of the base class members or overloaded

}

2. inherited methods are disclosed succession, inheritance and protection of private inheritance. Were used three kinds of inheritance public, protected, and private. Either way, the derived class inherits all of the (base class constructor, the copy constructor and destructor excluded) all members of the base class

3. derived type of access to property: private members of the base class in the derived class which exist, but are isolated and can not be accessed directly. If they need to be accessed by members of the base class.

4. The derived class objects can be initialized (copy constructor) Object base class, the base class initialization of reference, assigned to the object base class. Derived class object address pointer can be initialized variables of the base class can be assigned to the base class pointer variable.

The virtual function: virtual member function using the reserved word can be declared as virtual functions.

6. The friend functions can not be virtual!

7. As may be defined in the base class destructor as his virtual function (better)

8. A pure virtual function (function after the header, write symbol "= 0" after a pure virtual function is a virtual function)

9. As long as a class containing pure virtual function, then this class is an abstract class. The only effect of an abstract class is to be inherited. Derived class which defines a pure virtual function can override base class. Abstract class derived from the abstract class may also be, as long as it is a pure virtual function which is not also covered by the definition. When a class is not a pure virtual function, this class will become concrete classes.

10. The abstract class can not create object; pointer variable, but may be defined by abstract classes in an object specific point derived class; abstract class declarations can be cited, but the object must be initialized with a specific type of derived classes when declarative referential .

11. Description about the virtual functions:

(1) the class member functions can only be declared virtual.

(2) a pure virtual function has no body. An abstract class is inherited. You can not create objects of abstract classes, but you can define an abstract class pointer variable, citing a statement of an abstract class.

(3) constructor, copy constructor can not be virtual. Because the virtual function is to recognize specific object, the object constructor is executed after the process has not been completed configuration, the object has not been formed.

(4) static function can not be virtual. Because the default static function does not exercise an object.

 

Guess you like

Origin www.cnblogs.com/joelovescoding/p/11829664.html