순허함수와 추상류의 계승과 실현

15327 단어
코드를 직접 보면 주석이 매우 상세하다
먼저 헤더 파일 "virtual function.h"를 씁니다.
   1: #ifndef _VIRTUAL_FUNCTION_H_
   2: #define _VIRTUAL_FUNCTION_H_
   3:  
   4: #include <iostream>
   5: #define   Human
   6: #define   Female
   7: #define    Women
   8: #define    Girl
   9: class  
  10: {    
  11:     int age;    //    ,  
  12: public:
  13:      ()
  14:     {
  15:         std::cout << "      " << std::endl;
  16:         age = 18; 
  17:     }
  18:     ~ ()
  19:     {
  20:         std::cout << "       " << std::endl;
  21:     }
  22:     //       ,    ,    
  23:     virtual void showAge() = 0;
  24: };
  25: class  
  26: {
  27: public:
  28:     //             
  29:      ()
  30:     {    //               
  31:         std::cout << "      " << std::endl;
  32:     }
  33:     ~ ()
  34:     {    //               
  35:         std::cout << "      " << std::endl;
  36:     }
  37:     //    ,    ,    
  38:     virtual void showMaxim() = 0
  39:     {
  40:         std::cout << "     。。。" << std::endl;
  41:     }
  42: };
  43:  
  44: class    :public  , public  
  45: {
  46:     //    ,     ,    
  47:     int charm;    //        
  48: public:
  49:     //      
  50:       ()
  51:     {
  52:         std::cout << "       。。。" << std::endl;
  53:     }
  54:     ~  ()
  55:     {
  56:         std::cout << "       " << std::endl;
  57:     }
  58:     //             ,        
  59:     virtual void showAge()
  60:     {
  61:         std::cout << "    20  " << std::endl;
  62:     }
  63:     virtual void showMaxim()
  64:     {
  65:         std::cout << "        " << std::endl;
  66:     }
  67: };
  68:  
  69: class    :public   
  70: {
  71: public:
  72:     void showAge()
  73:     {
  74:         std::cout << "        ,    "<<std::endl;
  75:     }
  76:     void showMaxim()
  77:     {
  78:         std::cout << "        ,     "<<std::endl;
  79:     }
  80: };
  81:  
  82: #endif // !_VIRTUAL_FUNCTION_H_

메일 하나 더 주세요.cpp
   1: #include "virtual_function.h"
   2:  
   3: int main()
   4: {
   5:     {
   6:            *pgirl = new   ;
   7:           *phuman = pgirl;
   8:           *pfemale = pgirl;
   9:            *pwomen = pgirl;
  10:  
  11:         //                    
  12:         std::cout << "
====== ============"
<< std::endl;
  13:         pgirl->showAge();
  14:         pgirl->showMaxim();
  15:         //phuman    showAge
  16:         phuman->showAge();
  17:         //pfemale    showMaxin
  18:         pfemale->showMaxim();
  19:         //       
  20:         pwomen->showAge();
  21:         pwomen->showMaxim();
  22:  
  23:         //                    
  24:         std::cout << "
====== ============"
<< std::endl;
  25:         pgirl->  ::showAge();
  26:         pgirl->  ::showMaxim();
  27:         //phuman    ,           
  28:         //phuman->  ::showAge();
  29:         //pfemale    ,       
  30:         //pfemale->  ::showMaxim();
  31:         //       
  32:         pwomen->  ::showAge();
  33:         pwomen->  ::showMaxim();
  34:  
  35:         //                ,       showAge  ,    
  36:         //       ,           
  37:         std::cout << "
====== ============"
<< std::endl;
  38:     //    pgirl-> ::showAge();    //    ,    
  39:         pgirl-> ::showMaxim();
  40:         //phuman    showAge
  41:     //    phuman-> ::showAge();
  42:         //pfemale    showMaxin
  43:         pfemale-> ::showMaxim();
  44:         //       
  45:     //    pwomen-> ::showAge();
  46:         pwomen-> ::showMaxim();
  47:  
  48:  
  49:  
  50:         std::cout << "

====== sizeof , ============"
<< std::endl;
  51:         // 32    
  52:         std::cout << "sizeof (  )=" << sizeof(  ) << std::endl;    //16         showAge showMaxim   ,        
  53:         std::cout << "sizeof (  )=" << sizeof(  ) << std::endl;    //16              ,       4   
  54:         std::cout << "sizeof (   )=" << sizeof( ) << std::endl;    //8        age 4   ,      (vfptr)     
  55:         std::cout << "sizeof (   )=" << sizeof( ) << std::endl;    //4              , vfptr     
  56:  
  57:         std::cout << "pgirl=\t\t" << pgirl << std::endl;        //    01A92B8
  58:         std::cout << "pwomen=\t\t" << pwomen << std::endl;    //    01A92B8
  59:         std::cout << "phuman=\t\t" << phuman << std::endl;    //    01A92B8
  60:         std::cout << "pfemale=\t" << pfemale << std::endl;    //    01A92C0
  61:  
  62:  
  63:     }
  64:  
  65:     std::cin.get();
  66:     return 0;
  67: }

출력 결과는 다음과 같습니다.
      
      
       。。。
 
======                    ============
 
======                    ============
    20  
        
    20  
        
 
======                   ============
     。。。
     。。。
     。。。
 
 
======      sizeof  ,     ============
sizeof (  )=16
sizeof (  )=16
sizeof (   )=8
sizeof (   )=4
pgirl=        00586D38
pwomen=        00586D38
phuman=        00586D38
pfemale=    00586D40

좋은 웹페이지 즐겨찾기