本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>; d j5 J) g$ i' _7 j4 C8 S
#include <stdlib.h> #define PI 3.14+ E* c8 o( s5 d4 R! H
using namespace std; class Shape; x. `+ N& a% G7 W" I
{; S4 H7 S; O8 D& Z6 M0 F2 h
private:5 T" u9 h c: M9 Q& B, j0 Z
double m_width;% b& B6 [6 L9 L
double m_height;
* J$ Y( W, B. q7 ppublic:
& K: S. f& o5 d: J, {& z: s% G& q Shape(){};
3 g& r( n' d, k+ w ~Shape(){};
* F$ w5 c) A5 |' N }- v& m void SetWidth(double w = 0){m_width = w;};; @1 ?4 n0 d7 ]
void SetHeight(double h = 0){m_height = h;};7 r5 d& d. R- ^4 l7 j5 h# o/ N
double GetWidth(void){return m_width;};$ S' `+ @( h7 T; _: O- i" D2 F
double GetHeight(void){return m_height;};/ F7 r6 M: J3 x) ^9 D
virtual double GetArea(void) = 0;
+ W# A% U6 ]4 g0 m( L( o}; class Rectangle : public Shape6 }2 V) a! \% [" q' Y9 ^4 q3 i
{3 l# P) R5 I$ D# W
public:
6 s6 L8 p8 [% f9 E7 d Rectangle(){};
; T" R$ ?. D" x ~Rectangle(){};
* D% l8 j% x8 x- n% n virtual double GetArea(void){return GetWidth()*GetHeight();};& ]: S( Y1 N% b- ?( k
}; class Circle : public Shape
8 \) k" R5 ^$ F9 r8 T8 S9 T$ Y+ w{3 z7 [7 Q* h! y. H" d
public:
w; @3 O8 E/ _6 n9 M7 p Circle(){};3 s' n. h7 w: i
~Circle(){};
) ^2 F: q m( K# v' D7 G& l) t virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};* i( v2 @0 a/ O7 i- f, B
void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------2 x6 H7 h8 ]- g8 v. }1 r
// 主函数过程1 b2 x( f8 u& d0 ?) B
// ------------------------------------------------. x% M1 o3 A0 g* ?- b0 D7 O
int main(int argc, char *argv[]). h s. }' n5 f' m
{
2 d4 [. { i. G. d! [ Rectangle re; Circle ci;
: ?- ]1 E; o! g. Q Shape *p1 = &re,*p2 = &ci;
/ x" `" [! f% z7 ~2 s4 n7 A re.SetHeight(20);
0 z6 y2 D8 v" x, I re.SetWidth(10);+ U N+ s8 }% |+ L1 B- ^
ci.SetRadius(12);
5 @7 {& q! `$ ], r3 Y: b2 X4 k& R" a cout << "矩形面积:" << p1->GetArea() << endl;0 n; v3 h4 h. L- L
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE");
; h6 E! x* }9 W8 t5 M return 0;
r A" K/ i! }7 N4 V2 J} |