C ++ Prime reading notes

1.OOP core idea:

Data abstraction, inheritance, dynamic binding

2. Inheritance:

 2.1) inherit parent class subclasses inherit all the members except the constructors and destructors

If you want to inherit a constructor, you need to define the display

E.g

Novel::Book(std::string book_name):EmotionEngine(book_name) {
      std::cout << "build sucess" << std::endl;
    }

The constructor inherits the parent class

2.2)public/private/protect

Members of the public can access external functions and classes 

This function can only be accessed private members of the class

protected function can only be accessed members of the class and the derived class member functions

public inheritance: the most commonly used, the parent class public, but also for public subclass of parent class protected subclass protected

protected inheritance: inherited are all protected, private inherited are all private

2.3) class in question is stored among the memory

Static member variables and member functions are defined at the time of the class have been allocated a good memory, member functions in the code area, static member variables in the static area, but

Non-static member functions can not be directly invoked by the class, because it contains this pointer parameters, needs to correspond Examples

2.4) derived bring issues related to: virtual functions

c ++ virtual functions are implemented:

1. Each create a class with virtual functions, create a vtable, vtabel corresponding class share the same virtual table with all objects of a class, at the time of inheritance, virtual function tables inherit the parent class, and add your own virtual functions, and to replace the cover for their

2. The virtual function table is actually an array of function pointers, when the virtual function call, not a direct call, find the corresponding first vptr The virtual function table to find the corresponding function pointer in the virtual function table, then call. Multiple Inheritance

3. pointers accessibility issues

Accessibility is determined by a defined pointer by the pointer, for example by BaseClass defined pointer can range accessible memory area is BaseClass

BaseClass pointer to an object of a derived class, the derived class can access only inherited from the parent class members can not assign addresses to the parent pointer of the derived class, the derived class because of the greater access range, cross-border

and final override: final class for indicating the class can not be inherited, a virtual function, then the function can not be overridden in a subclass, the override for the derived class, indicates that the function is to cover the base class

Pure virtual function:

virtual int fun_a(int number) =0;

In the virtual function table, on a pure virtual function value is 0, there is not a pure virtual function instance of the class object

Guess you like

Origin blog.csdn.net/loubiao9212/article/details/85709289