C ++ - polymorphism, pure virtual function, an abstract class, the factory mode, virtual destructor (a fastMacedonia Day10)

  A multi-state (more common Day9)

1, a multi-state condition

1) in addition to the polymorphic properties declared in the base class virtual function, and the formation of effective coverage in the subclass must also virtual function is called by a pointer or reference, it can be manifested, not directly invoked by polymorphic objects.

2) call the virtual function pointer may be a pointer this, as long as it is a base-class pointer subclass object, the same may exhibit polymorphism characteristic.

class Base{

public:

  virtual int cal(int x,int y){

    retunr x+y;
  }

  //d.foo()-->foo(&d)

  //void foo(Base* this)

  //Base* this =&d;

  void foo(void){

    cout<<cal(100,200)<<endl; 
  }

};



class Derived:public Base{

public:

  int cal(int a,int b){

    return* A B; 
  } 

} 



int main ( void ) { 

  the Derived D; 

  Base B = D; 

  COUT << b.cal ( 100 , 200 is ) << endl; // call is the base class version, without forming a multi- call state 

  d.foo (); // child class object is not foo function, but since the pass in this while is Base *, but points to a type Derived * & d, so invoking corresponds this- > cal (100,200), so the formation of polymorphic call 

  return  0 ; 

}

 


 

Two, pure virtual function, an abstract class, an abstract class pure

1, a pure virtual function

 

Virtual  void Draw ( void ) = 0 ; // pure virtual function

 

The general form of a pure virtual function is as follows:

virtual return type of the function name (parameter list) [ const ] = 0 ;

 

2, abstract class

  1) If class contains a pure virtual function, then it is an abstract class. For example, the Shape class is an abstract class, the class does not contain a particular behavior. The compiler does not allow abstract class instance object. If instantiated, the following error occurs:

  2) Further, if the base class has inherited pure virtual function, and the subclass is not covered, then the subclass will be transformed into an abstract class.

  3) If all member functions of a class (not including constructors, destructors) are pure virtual function, then this class is called pure abstract class.

3) Examples factory pattern

 

Team1 responsible for parsing the 

 class PDFParse { 

public : 

  void Prase ( const  char * pdfFile) // parse graphics, text, graphics function 

  { 

    OnCircle (); 
    OnRect (); 
    ontext (); 
    OnImage (); 
    // . . . 
  } 

Private : 

  Virtual  void Oncircle ( void ) = 0 ; 

  Virtual  void OnRect ( void ) = 0 ; 

  Virtual  void ontext ( void ) = 0 ; 

  Virtual  void OnImage (void)=0;

};

Team2负责绘图实现

class PDFRender:public PDFParse{

private:

  void OnCircle(void){

    ...
  }

  void OnRect(void){

    ...
  }

     void OnText(void){

    ...
  }

  void OnImage(void){

    ...
  };

};



int main(void){

  PDFRender render;

  render.parse("something.pdf"); // polymorphism is implemented by this pointer 

  return  0 ; 

}

 

4) multi-state realization of the principle (understanding)

  1: The virtual keyword stated function called virtual function, virtual function must have the class member functions.  

  2: the presence of a virtual function of the class has a dimensional virtual function table called the virtual table, a pointer to the object class has virtual virtual table pointer begins. And the virtual table is corresponding to a class, and is a virtual table pointer corresponding to the object.  

  3: an interface polymorphism multiple implementations, object-oriented core, polymorphism and polymorphism of the functions are divided into classes.  

  4: Polymorphism be implemented with virtual functions, combined with dynamic binding.  

  5: pure virtual function is a virtual function together with = 0;  

  6: abstract class means a class including at least one pure virtual function.

 

 

 

 The principle:  

(1) for the compiler, the non-virtual function call address in at compile time was bound, such binding is called early binding. If the non-virtual, or even subclass pointer references the calling function only when the shape of the base class pointer up to the base class or reference, calls the same function, as it has been bound at compile good, the execution Can not change.

(2) In (1) To use a function pointer to the base class is called a subclass, it is necessary to use with virtual functions

(3) If there is a virtual function, it will add a virtual function table pointer to a class of any class (three pointers, function pointer is a pointer typically, the second half of the virtual table is a virtual function pointer array, it is seen that two pointer), points to virtual function table (abbreviated virtual table), as shown above, foo (), four bytes representing the virtual table pointer size in a subject, does not belong to the object virtual table, but by the virtual table pointer takes contents. FIG base class foo () virtual function also, and there is a virtual function table after the virtual function, subclass inherits, and covers class foo A () starting address of the function to add the address of its class . The bar function is not rewritten, the intact inherit the function, the start address does not change.

(4) belonging to the virtual table pointer vptr late binding, it will be called according to the type of actual or actual pointer to the referenced type. Call a virtual function of each object is to be indexed by virtual functions, like the array has a starting address and the same index. So virtual table pointer initialized correctly is very important. Then the virtual table pointer is when to create and initialize it? Virtual table pointer is actually created and initialized in the constructor, in (3) also said, part of the virtual object table pointer, 4 bytes of memory size, it would first appear to create and initialize in the constructor granted.

(5) When constructing, the base class subobject first to the configuration of the base class, the compiler sees the parent class has virtual functions, to create and initialize the base class virtual table, when the class object constructor, the discovered subclass virtual functions, it is required to cover the base class virtual table cover, as foo () function is covered, but the bar () function is not covered. And have their virtual table pointer (as distinguished from the base class virtual table pointer) in a subclass, which is polymorphic reason why can be realized by a pointer or reference calls. While object calls directly, without virtual table pointer index, direct transfer member function ((This function implements the virtual functions of the parent class calls the base class through an object, in fact, is inside this pointer played a role) excluded).

 


 

 

 Third, virtual destructor

1, introduction

  When a base class objects to sub-class destructor pointer can only call the destructor of the base class and can not call the destructor subclass, the methods previously learned is a pointer to the base class do downwardly modeling for destruction. In practice this is not done, but the use of virtual destructors to solve.

 

 

class Base{

public:

  Base(void){

    cout<<"Base::Base()"<<endl;

  }

   virtual ~Base(void){

    cout<<"Base::~Base()"<<endl;

  }

};

class Derived:public Base{

public:

  Derived(void){

    cout<<"Derived::Derived()"<<endl;

  }

  ~Derived(void){

    cout<<"Derived::~Derived()"<<endl;

  }

};

 

int main(void){

  Base * pb = new Derived; // if not declared virtual destructor, the memory will be released in this way have a memory leak risk

  delete pb;

  return 0;

}

 

  If the destructor for the base class virtual function, then the destructor subclass are virtual functions, may be effective coverage of the base class destructor. This time to delete base-class pointer subclass, the actual call is destructor subclass, the subclass destructor will call the destructor of the base class, to avoid the above problems.

 


 

 

Fourth, practice - payroll calculation

 

Employees attributes: name, job number, job level, pay for performance, attendance

Manager: performance bonuses (yuan / month)

Technician: R & D allowance (Yuan / hour)

Seller: commission rate (percentage)

 

Salary = base salary + performance pay

Basic wage = Career Level attendance credits *

 

 Pay for performance: Due to different jobs and different

 

The general staff: half basic salary

Manager: Performance Bonus * Performance Factor (manual input)

Technician: R & D allowances * * Progress number of hours worked factor (manual input)

Seller: Sales degrees (manual input) * Percent

Technical director :( technician salary + performance manager's pay for performance) / 2

Sales director :( salesperson performance manager performance pay salary +) / 2

 

Results: print employee data, you must enter the input data, calculate payroll

 

Guess you like

Origin www.cnblogs.com/ptfe/p/11299869.html