本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
& } {* N0 d# s#include <stdlib.h> #define PI 3.14
. `' Y* s3 x" U1 L0 _using namespace std; class Shape" M' Y8 c- T% s0 g. S
{ i3 j9 Q* j5 A0 f! S% j
private:3 G* e( C. U& @- p, E p; A
double m_width;7 F% E! S4 y" ]9 i L9 H
double m_height;
; W% W$ S2 n6 t$ Npublic:: W7 R9 \* g8 S, x- J# b9 e
Shape(){};
: B0 q0 c' l. A2 |; _7 [( y ~Shape(){};
3 M7 m/ Q! D D# p void SetWidth(double w = 0){m_width = w;};
! z+ S& [5 y0 v) B- j3 m void SetHeight(double h = 0){m_height = h;};
7 b7 p7 \+ j# H6 \; e double GetWidth(void){return m_width;};& c7 U! s6 Z, L. \
double GetHeight(void){return m_height;};
. X* {2 ]" |& v( ] virtual double GetArea(void) = 0;& u+ v* U0 Y" J2 ^9 u0 E: a M
}; class Rectangle : public Shape
7 ]# t( v; B2 V{+ @$ y8 |7 F4 G- U/ a
public:" \% \$ t$ E3 Z0 O
Rectangle(){};
5 |! @, l" d' c2 S( V: N3 U ~Rectangle(){};6 G$ R* c1 d, a! d
virtual double GetArea(void){return GetWidth()*GetHeight();}; Y% s: Y' H E& B/ B# }7 J
}; class Circle : public Shape
) |8 f2 ]1 T( l1 A( O- a{
0 l% k* B- v! Y8 i2 F2 ppublic:: I6 O1 Y$ h. t4 W; _( x
Circle(){};+ ` `! Q+ }. f) m
~Circle(){};
8 q7 U0 e& Q6 V3 U- I virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};+ y/ x' ~6 c' o* x; d
void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
' Z; T7 ]4 Z7 D$ b// 主函数过程" g6 |! H) z1 b. b6 c
// ------------------------------------------------
, n1 n0 z8 E- Y cint main(int argc, char *argv[])8 Y( _! }9 t8 ^
{
, h3 [( N* X+ T( q+ P9 x/ t# ^ Rectangle re; Circle ci;
8 M# l9 M3 h$ _. P3 B/ i1 J0 y6 L Shape *p1 = &re,*p2 = &ci;$ c6 k2 J1 t+ O5 m; _5 S
re.SetHeight(20);
$ S1 K) g& ~* V) U* `0 ~3 t5 ^% Z re.SetWidth(10);
$ r, ^9 k/ k# S- @ ci.SetRadius(12);4 U7 b, F8 E: f
cout << "矩形面积:" << p1->GetArea() << endl;" a3 }4 H) s9 }7 K# }% ]- I
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE");
! k- [4 B; }$ s. A+ Y: O& @. M( B( {. J return 0;
6 n, b2 { R- f% S4 y2 @: J4 i} |