1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Fig. 10.20: fig10_20.cpp
// Driver for shape, point, circle, cylinder hierarchy.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include <vector>
using std::vector;
#include "shape.h" // Shape class definition
#include "point.h" // Point class definition
#include "circle.h" // Circle class definition
#include "cylinder.h" // Cylinder class definition
void virtualViaPointer( const Shape * );
void virtualViaReference( const Shape & );
int main()
{
// set floating-point number format
cout << fixed << setprecision( 2 );
Point point( 7, 11 ); // create a Point
Circle circle( 22, 8, 3.5 ); // create a Circle
Cylinder cylinder( 10, 10, 3.3, 10 ); // create a Cylinder
cout << point.getName() << ": "; // static binding
point.print(); // static binding
cout << '\n';
cout << circle.getName() << ": "; // static binding
circle.print(); // static binding
cout << '\n';
cout << cylinder.getName() << ": "; // static binding
cylinder.print(); // static binding
cout << "\n\n";
// create vector of three base-class pointers
vector< Shape * > shapeVector( 3 );
// aim shapeVector[0] at derived-class Point object
shapeVector[ 0 ] = &point;
// aim shapeVector[1] at derived-class Circle object
shapeVector[ 1 ] = &circle;
// aim shapeVector[2] at derived-class Cylinder object
shapeVector[ 2 ] = &cylinder;
// loop through shapeVector and call virtualViaPointer
// to print the shape name, attributes, area and volume
// of each object using dynamic binding
cout << "\nVirtual function calls made off "
<< "base-class pointers:\n\n";
for ( int i = 0; i < shapeVector.size(); i++ )
virtualViaPointer( shapeVector[ i ] );
// loop through shapeVector and call virtualViaReference
// to print the shape name, attributes, area and volume
// of each object using dynamic binding
cout << "\nVirtual function calls made off "
<< "base-class references:\n\n";
for ( int j = 0; j < shapeVector.size(); j++ )
virtualViaReference( *shapeVector[ j ] );
return 0;
} // end main
// make virtual function calls off a base-class pointer
// using dynamic binding
void virtualViaPointer( const Shape *baseClassPtr )
{
cout << baseClassPtr->getName() << ": ";
baseClassPtr->print();
cout << "\narea is " << baseClassPtr->getArea()
<< "\nvolume is " << baseClassPtr->getVolume()
<< "\n\n";
} // end function virtualViaPointer
// make virtual function calls off a base-class reference
// using dynamic binding
void virtualViaReference( const Shape &baseClassRef )
{
cout << baseClassRef.getName() << ": ";
baseClassRef.print();
cout << "\narea is " << baseClassRef.getArea()
<< "\nvolume is " << baseClassRef.getVolume() << "\n\n";
} // end function virtualViaReference
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
|