本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
4 j! b9 Y- @# |/ B' f. J2 S# j#include <stdlib.h> #define PI 3.14, v- [, H4 Q8 D E2 W+ b8 D% _
using namespace std; class Shape4 r# i( A7 z( `' S3 p0 H
{
! G: R0 P; I' P1 q0 S9 tprivate:7 M' m+ R* L4 M0 ~
double m_width;9 c6 A5 z2 Q( I8 a6 x N
double m_height;8 P. \6 |/ `' K" Q) ^% t1 d7 d
public:
) Q2 p4 `: N, Z8 |! ^! ? Shape(){};
Z+ L9 l0 t5 O9 T+ ?4 p) _ ~Shape(){};% w4 C: }' i3 X% D7 g& r; q
void SetWidth(double w = 0){m_width = w;};
# N- x W! I& M; E% y void SetHeight(double h = 0){m_height = h;};' a8 ^. `: I8 |8 j3 w
double GetWidth(void){return m_width;};2 _' J* k4 T; @: A+ @4 E
double GetHeight(void){return m_height;};
7 K3 A2 `, M& _( {6 ^% `0 j0 k A% F% y virtual double GetArea(void) = 0;7 R9 t) S. }0 A1 B0 M7 z
}; class Rectangle : public Shape
) M: H1 d% j y/ a5 w9 e{
2 d( v$ X, [. Z: f7 Xpublic:
/ i' F$ R7 V6 N/ e' m Rectangle(){};& u; w# z/ c- \
~Rectangle(){};
- M ~- z" q& X3 `5 h virtual double GetArea(void){return GetWidth()*GetHeight();};
9 i q# ?( {( s. Y0 Z}; class Circle : public Shape. I; n- G1 o1 a: I% T
{
1 J3 X# Y/ |, a& rpublic:; l7 P& ~, O, q( r4 a! X
Circle(){};' V* @4 X. v6 n/ I! z0 I+ y
~Circle(){};2 B( a5 `2 e/ ]9 t' v
virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
9 ~3 W" n8 l' \; q) o# f1 N Q/ U void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------6 I5 _1 {) c% ^; R
// 主函数过程
3 R2 E/ s/ s% Q8 ^4 u; |// ------------------------------------------------
4 k. A4 L/ s% ]6 Cint main(int argc, char *argv[]). e8 k" i( F/ a9 ^9 L( u8 i, x v/ t
{3 P& }( o% w. {2 f& S: T( a, z
Rectangle re; Circle ci;
7 q6 A1 }5 p% |1 _ Shape *p1 = &re,*p2 = &ci;
4 ]$ O" u' h2 K$ m! j- ` re.SetHeight(20); v$ g& n6 F: }/ X( `7 W1 R8 a
re.SetWidth(10);; ~3 U O1 H0 E
ci.SetRadius(12);$ F/ G. U6 l3 H
cout << "矩形面积:" << p1->GetArea() << endl;% L1 w2 S# s1 a' C1 ]" V0 b
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); . `* P& `1 v4 ~4 y, s9 L
return 0;( N6 b2 v& u9 B7 ]" @ Q
} |