本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream># _2 t) `. d# g+ l9 v7 x. X
#include <stdlib.h> #define PI 3.14. ~( R y) ?$ v3 r" ^5 C" S& u5 _. X1 d
using namespace std; class Shape
, t2 h \: O8 ]' A6 l; V, ?{
0 N0 A9 W& I# R1 f h- Zprivate:
6 l2 B; g0 e3 X! ~3 f double m_width;
/ V ~) A# {0 M7 ?) w) i7 F double m_height;
1 x9 O5 ^9 V7 T* C2 k4 Mpublic:" C& M: z; B! H6 O( Q1 Y4 r! r9 T
Shape(){};
4 S# `5 ]0 s# x ~Shape(){};
! J! d8 k) Q, M3 N void SetWidth(double w = 0){m_width = w;};
6 g, R3 X, p. U' x! [4 H* ^ ] void SetHeight(double h = 0){m_height = h;};
t, f) f! C4 `5 d5 m! y' B double GetWidth(void){return m_width;};' ^$ y- H, K$ a! Z& I& E3 @4 S
double GetHeight(void){return m_height;};3 C* X* s5 g9 L( v6 k8 x
virtual double GetArea(void) = 0;
% @( z2 K( C. H& ?8 U}; class Rectangle : public Shape) B* T' R! x! A) f5 Y) i
{
' `5 w0 S8 Y" Q: D4 v5 Gpublic:. ?! i. e S; x& r4 ]
Rectangle(){};" R9 G; T& Q# g2 k
~Rectangle(){};
( Q" }. n* P; _, M' m/ m6 } virtual double GetArea(void){return GetWidth()*GetHeight();};: i4 U% K* Q7 Y; c% a5 @5 Y
}; class Circle : public Shape5 @/ ^* d6 t, k9 |" T L) r, h! U' j
{+ j! M% X. j3 _9 T0 a4 a
public:
4 e& O2 D/ O) J- H9 d) ^4 z9 k Circle(){};3 i; x# i" E: V3 D/ P0 F
~Circle(){};; i) r0 w' Z7 z z& \2 Z
virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
v7 ]' \' p" d, l, E void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
) }6 |: v+ l) x. T, c// 主函数过程
% \- `: i; g1 R1 o; j+ I// ------------------------------------------------* d! @7 t4 ^' ~+ Y' j! A! x6 Q) l
int main(int argc, char *argv[])
; l- z' ]. F/ p$ M{
" e3 b# c0 ~2 |+ A7 Y$ l+ O3 ? Rectangle re; Circle ci;
/ J5 }0 N) }4 L! Y$ r& ? Shape *p1 = &re,*p2 = &ci;
: j( Z! e$ D% N; u re.SetHeight(20);
8 A5 }! l+ S! H0 |3 N re.SetWidth(10);) ]/ m4 [$ z0 c2 V
ci.SetRadius(12);. J) F$ f9 G9 x: @$ b5 u
cout << "矩形面积:" << p1->GetArea() << endl;5 C1 G" a' ?+ o6 \& K) g
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); ) i# x% k( H6 A. L
return 0;7 ?" K5 F5 n1 l6 l& I4 r- L
} |