The Anatomy of a COM Server(Chapter 2 of COM and .NET Interoperability) part1

ExpandedBlockStart.gif Interface
  1  //  Interfaces.cpp : Defines the entry point for the console application.
  2 
  3  #include  " stdafx.h "
  4  #include  < iostream.h >
  5  #include  " windows.h "
  6 
  7  //  The interface.
  8  interface  ICar
  9  {
 10       virtual   void  SpeedUp( long  delta)  =   0 ;        
 11       virtual   void  CurrentSpeed( long   * currSp)  =   0 ;
 12  };
 13 
 14  //  The basic Car.
 15  class  Car :  public  ICar
 16  {
 17  private :
 18       long  m_currSpeed;
 19  public :
 20      Car() { m_currSpeed  =   0 ; }    
 21       virtual   ~ Car(){}
 22 
 23       //  ICar implementation. 
 24       void  SpeedUp( long  delta)
 25      {
 26          cout  <<   " I am a basic car "   <<  endl;
 27          m_currSpeed  +=  delta;    
 28      }         
 29       void  CurrentSpeed( long   * currSp)
 30      {
 31           * currSp  =  m_currSpeed;    
 32      }         
 33  };
 34 
 35  //  The wicked cool car.
 36  class  HotRod:  public  ICar
 37  {
 38  private :
 39       long  m_currSpeed;
 40  public :
 41      HotRod() { m_currSpeed  =   0 ; }    
 42       virtual   ~ HotRod(){}
 43       //  ICar implementation (massive turbo booster!). 
 44       void  SpeedUp( long  delta)
 45      {
 46          cout  <<   " I am a hot rod! "   <<  endl;
 47          m_currSpeed  +=  (delta  *   20 );    
 48      }         
 49       void  CurrentSpeed( long   * currSp)
 50      {
 51           * currSp  =  m_currSpeed;    
 52      }         
 53  };
 54 
 55  //  Not a Car.
 56  class  CellPhone
 57  {};
 58 
 59  //  Prototypes.
 60  void  RevEngine(ICar *  pCar);
 61 
 62  //  Take the cars out for a test drive.
 63  int  main( int  argc,  char *  argv[])
 64  {
 65       //  An array of ICar interfaces. 
 66      ICar *  theCars[ 2 ];
 67      theCars[ 0 =   new  Car();
 68      theCars[ 1 =   new  HotRod(); 
 69 
 70       //  Speed up each car 5 times. 
 71       for  ( int  j  =   0 ; j  <   5 ; j ++ )
 72      {
 73           for ( int  i  =   0 ; i  <   2 ; i ++ )
 74          {
 75              theCars[i] -> SpeedUp( 10 );
 76               long  currSp  =    0 ;
 77              theCars[i] -> CurrentSpeed( & currSp);
 78              cout  <<   "   ->Speed:  "   <<  currSp  <<  endl;
 79          }
 80      }
 81 
 82       //  Clean up memory.
 83      delete[]  * theCars;
 84 
 85       //  Uncomment to generate error.
 86       /*
 87      ICar car;
 88      car.CurrentSpeed(10);
 89       */
 90 
 91       //  Better!
 92      ICar *  pAnotherCar  =   new  Car();
 93      ICar *  pAnotherHotRod  =   new  HotRod();
 94 
 95       //  Rev the car.
 96      RevEngine(pAnotherCar);
 97      RevEngine(pAnotherHotRod);
 98      delete pAnotherCar;
 99      delete pAnotherHotRod;
100 
101       //  Uncomment for error.
102       //  CellPhone cp;
103       //  RevEngine(cp);
104       return   0 ;
105  }
106 
107  void  RevEngine(ICar *  pCar)
108  {
109      cout  <<   " Reving a car... "   <<  endl;
110       long  currSp  =   0 ;
111 
112       for ( int  i  =   0 ; i  <   5 ; i ++ )
113      {
114          pCar -> SpeedUp( 10 );
115          pCar -> CurrentSpeed( & currSp);
116          cout  <<   " Speed:  "   <<  currSp  <<  endl;
117      }
118       for (i  =   5 ; i  >   0 ; i -- )
119      {
120          pCar -> SpeedUp( - 10 );
121          pCar -> CurrentSpeed( & currSp);
122          cout  <<   " Speed:  "   <<  currSp  <<  endl;
123      }
124  }

 

转载于:https://www.cnblogs.com/zhtf2014/archive/2010/03/16/1687703.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值