![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }
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 }