本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>0 H& R/ h: d9 N9 z) o9 \
#include <stdlib.h> #define PI 3.14
3 @1 n. ?; F. `) w. iusing namespace std; class Shape
; ^& K y1 z6 m( T/ e3 K9 c{0 j. F$ O7 [" F
private:2 M+ i, j: S6 @. n
double m_width;
6 Z2 w6 x& S8 ]& [ double m_height;
) a: E2 p, a& H0 E0 Z& Z, `2 hpublic:
+ C# I4 ~9 ]6 S$ d; f: D$ U Shape(){};" i+ W' \! v$ D) m
~Shape(){};4 y; z9 g5 V. Y" F' B# l
void SetWidth(double w = 0){m_width = w;};4 q" _- W* D$ a3 E7 E! {
void SetHeight(double h = 0){m_height = h;};# h/ G6 |; L; [) c; w% p( W3 l
double GetWidth(void){return m_width;};
H9 V* G N/ r5 A6 ^9 a8 M double GetHeight(void){return m_height;};9 q. [2 L8 l1 e; U$ y% Z* z: I
virtual double GetArea(void) = 0;" ^: t; e. Y, N+ B6 _1 t/ U0 W" y" o
}; class Rectangle : public Shape! `3 W2 o3 F) F+ J0 ?. x
{
2 }; R+ r! o; Q# C- z4 F. {public:5 t" y% X% y& ]( V X
Rectangle(){};
0 S8 e2 Z* i+ M5 v ~Rectangle(){};
# h4 U& w2 U# a% Z2 V* S! | virtual double GetArea(void){return GetWidth()*GetHeight();};
' d7 b" r9 Y: _! w N}; class Circle : public Shape; k* V7 t( }4 U$ J! B# |
{1 r; k# |& Q0 Q" X+ I
public:# T4 L) c7 R1 m- e0 A
Circle(){}; l" E: I' u# e0 u+ I. j$ X
~Circle(){};5 M; D+ n$ s( X( M7 o7 z
virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
+ N' r1 j. K$ n' Y" K void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
X$ z4 \8 o$ d) ?0 H! w// 主函数过程0 ^. L% b M& s$ ~$ @# n: f
// ------------------------------------------------
4 Z9 W) H+ g0 \% d' c2 d, zint main(int argc, char *argv[])! {/ l, J+ B: F4 I8 I
{
" V, z- I' }' u Rectangle re; Circle ci;
; S, d8 |( p& v6 R8 r6 J+ J0 r Shape *p1 = &re,*p2 = &ci;
6 ` R8 J9 Y# t+ c1 K4 q re.SetHeight(20);* c( ]( v! r3 H7 U9 ?# p0 w1 J) O0 G
re.SetWidth(10);, \ f) }7 }" F1 D% A
ci.SetRadius(12);* s+ z7 g8 I- I) j4 [0 i6 b8 Q
cout << "矩形面积:" << p1->GetArea() << endl; o0 B* ~4 P! C3 l0 _4 `& D# N
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); 2 ?8 R7 \6 l: s$ M9 y
return 0;
, l9 S9 X0 }' a- H' `} |