本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
% ^- ~' k) S8 O9 R. K: u, ?#include <stdlib.h> #define PI 3.14
% }. ]( ~* W' ], [# Xusing namespace std; class Shape; ]3 P4 ]4 E7 j! T# C8 @: w7 j8 h4 ~: c
{$ J1 y5 S8 A3 J5 D+ I) a/ u" J
private:2 q, q& ?$ j( v
double m_width;
2 d& [# _; Y5 _8 v' m, o double m_height;" y: W% X- ]" l. X2 O6 c" j/ Y; w
public:: [9 }' H- \3 c
Shape(){};
7 S$ J. h' X+ [: _: k ~Shape(){};
; K. j$ B6 I3 r) p8 l void SetWidth(double w = 0){m_width = w;}; f' d* n$ S ?9 N. I' Z/ x% z9 M( B
void SetHeight(double h = 0){m_height = h;};
( C# M' W6 Q! V& r7 i double GetWidth(void){return m_width;};3 u, Y1 i6 p2 B' n
double GetHeight(void){return m_height;};: O7 k I- k7 J% z2 j$ O
virtual double GetArea(void) = 0;. z5 B$ r( V- M' A3 K; P" K/ o+ M
}; class Rectangle : public Shape
' L, Z3 t- p, @' [{
& j- u9 z4 F- C% N* k# tpublic:) a6 f. _% x" y+ z! \
Rectangle(){};; J# i, N. ^, @
~Rectangle(){};, K2 l, o' w$ }
virtual double GetArea(void){return GetWidth()*GetHeight();};3 v6 ^5 M6 K( }
}; class Circle : public Shape
# n+ B9 t1 F- q' N* L{
5 e$ ^9 e) K+ X; R$ i7 x* H5 e, spublic:7 ^1 o; T! O0 G* a+ P
Circle(){};
5 G2 n/ w( w- Y0 R8 l7 u5 l5 A- h ~Circle(){};1 K% e" b' `, O7 c1 v6 [. |" h! J2 ]
virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
% a( \: G; q% t4 B2 v+ B/ ` void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------) W1 @% F9 t8 `9 u
// 主函数过程
4 Q% Y; Z6 K% d7 r& o8 R// ------------------------------------------------
/ r# f8 J' T1 ]int main(int argc, char *argv[])# O" l1 u* R) x h ?) W
{
6 I& I, H% Q6 n Rectangle re; Circle ci;. ?7 I8 d/ |+ x. H2 v$ X8 T- L
Shape *p1 = &re,*p2 = &ci;
: a: d7 t' G8 b; P' F4 J re.SetHeight(20);
9 \- `3 W: ^+ R( ?2 { re.SetWidth(10);
+ {- I7 `; c. \# ~1 {0 |6 }1 S ci.SetRadius(12);2 [9 J& p7 o; w( r9 c& w' `3 n
cout << "矩形面积:" << p1->GetArea() << endl;2 L8 K8 b" _ _* i) E( B. S; ^
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); - M7 a! q/ [5 B- R% M8 x. A8 s' r
return 0;, t- ~( |, l: ^
} |