📄 10章 虚函数与多态性.txt
字号:
38 // Member function definitions for class Point
39 #include "point1.h"
40
41 Point::Point( int a, int b ) { setPoint( a, b ); }
42
43 void Point::sefPoint( int a, int b }
44 {
45 x = a;
46 y = b;
47 }
48
49 void Point::print() const
5O { cout << '[' << x << ", "<< y << '] '; }
51 // Fig. 10.2: circle1.h
52 // Definition of class Circle
53 #ifndef CIRCLE1_H
54 #define CIRCLE1_H
55 #include "point1.h"
56
57 class Circle : public Point {
58 public:
59 // default constructor
60 Circle( double r = 0.0, int x = 0, int y = 0 );
61
62 void setRadius( double );
63 double getRadius() const;
64 virtual double area() const;
65 virtual void printShapeName() const { cout << "Circle: "; }
66 virtual void print() const;
67 private:
68 double radius; // radius of Circle
69 };
7O
71 #endif
72 // Fig. 10.2: circlel.cpp
73 // Member function definitions for class Circle
74 #include "circie1.h"
75
76 Circle::Circle( double r, int a, int b )
77 : Point( a, b ) // call base-class constructor
78 { setRadius( r ); }
79
80 void Circle::setRadius( double r ) { radius = r > 0 ? r : 0; }
81
82 double Circle::getRadius() const { return radius; }
83
84 double Circle::area() const
85 { return 3.14159 * radius * radius; }
86
87 void Circle::print() const
88 {
89 Point::print();
90 cout << "; Radius =" << radius;
91 }
92 // Fig. 10.2: cylindrl.h
93 // Definition of class Cylinder
94 #ifndef CYLINDR1_H
95 #define CYLINDR1_H
96 #include "circle1.h"
97
98 class Cylinder : public Circle {
99 public:
100 // default constructor
101 Cylinder( double h = 0.0, double r = 0.0,
102 int x = 0, int y = 0 );
103
104 void setHeight( double );
105 double getHeight() const;
106 virtual double area() const;
107 virtual double volume() const;
108 virtual void printShapeName() const {cout << "Cylinder: ";}
109 virtual void print() const;
110 private:
111 double height; // height of Cylinder
112 };
113
114 #endif
115 // Fig. 10.2: cylindr1.cpp
116 // Member and friend function definitions for class Cylinder
117 #include "cylindr1.h"
118
119 Cylinder::Cylinder( double h, double r, int x, int y )
120 : Circle( r, x, y ) // call base-class constructor
121 { setHeight( h ); }
122
123 void Cylinder::setHeight( double h )
124 { height = h > 0 ? h : 0; }
125
126 double Cylinder::getHeight() const { return height; }
127
128 double Cylinder::area() const
129 {
130 // surface area of Cylinder
131 return 2 * Circle::area() +
132 2 * 3.14159 * getRadiusO * height;
133 }
134
135 double Cylinder::volume() const
136 { return Circle::area() * height; }
137
138 void Cylinder::print() const
139 {
140 Circle::print();
141 cout << "; Height =" << height;
142 }
143 // Fig. 10.2: figl0_02.cpp
144 // Driver for shape, point, circle, cylinder hierarchy
145 #include <iostream.h>
146 #include <iomanip.h>
147 #include "shape.h"
148 #include "point1.h"
149 #include "circle1.h"
150 #include "cylindr1.h"
151
152 void virtualViaPointer( const Shape * );
153 void virtualViaReference( const Shape & );
154
155 int main()
156 {
157 cout << setiosflags( ios::fixed | ios::showpoint )
158 << setprecision( 2 );
159
160 Point point( 7, 11 ); // create a Point
161 Circle circle( 3.5, 22, 8 ); // create a Circle
162 Cylinder cylinder( 10, 3.3, 10, 10 ); // create a Cylinder
163
164 point.printShapeName(); // static binding
165 point.print(); // static binding
166 cout << '\n';
167
168 circle.printShapeName(); // static binding
169 circle.print(); // static binding
170 cout << '\n';
171
172 cylinder.printShapeName(); // static binding
173 cylinder.print(); // static binding
174 cout << "\n\n";
175
176 Shape *arrayOfShapes[ 3 ]; // array of base-class pointers
177
178 // aim arrayOfShapes[ 0 ] at derived-class Point object
179 arrayOfShapes[ 0 ] = &point;
180
181 // aim arrayOfShapes[ 1 ] at derived-class Circle object
182 arrayOfShapes[ 1 ] = &circle;
183
184 // aim arrayOfShapes[ 2 ] at derived-class Cylinder object
185 arrayOfShapes[ 2 ] = &cylinder;
186
187 // Loop through arrayOfShapes and call virtualViaPointer
188 // to print the shape name, attributes, area, and volume
189 // of each object using dynamic binding.
190 cout << "Virtual function calls made off"
191 << "base-class pointers\n";
192
193 for ( int i = 0; i < 3; i++ )
194 virtualViaPointer( arrayOfShape[ i ] );
195
196 // Loop through arrayOfShapes and call virtualViaReference
197 // to print the shape name, attributes, area, and volume
198 // of each object using dynamic binding.
199 cout << "Virtual function calls made off"
200 << "base-class references\n";
201
202 for (int j = 0; j < 3; j++ )
203 virtualViaReference( *arrayOfShapes[ j ] );
204
205 return 0;
206 }
207
208 // Make virtual function calls off a base-class pointer
209 // using dynamic binding.
210 void virtualViaPointer( const Shape *baseClassPtr )
211 {
212 baseClassPtr->printShapeName();
213 baseClassPtr->print();
214 cout << "\nArea = "<< baseClassPtr->area()
215 << "\nVolume =" << baseClassPtr->volume() << "\n\n";
216 }
217
218 // Make virtual function calls off a base-class reference
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -