本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>8 ?; ]( U! m7 v1 O$ {3 |
#include <stdlib.h> #define PI 3.14
: r4 b# p! M% j" x) a/ iusing namespace std; class Shape
) Y7 \$ K: `/ z" s0 y; {{
7 u& { M- u$ f3 S- M5 W1 Vprivate:
* X. W' ~+ u1 r$ n/ K* p double m_width;: G$ g$ d2 p, j* A" K9 q; N
double m_height;) W* {) t6 D6 V
public:4 W4 f" I2 Z" V/ t/ Z7 C8 n
Shape(){};) `* S6 c$ C; q" \2 o6 y
~Shape(){};
6 C" Q& Y/ o z7 V void SetWidth(double w = 0){m_width = w;};
4 @: ?' V& u* }; _, B void SetHeight(double h = 0){m_height = h;};: o6 a3 M. H& \# T
double GetWidth(void){return m_width;};% D4 h7 Y0 x, T( h6 [5 o1 t& h G
double GetHeight(void){return m_height;};" T, X+ B; ?1 ~7 o
virtual double GetArea(void) = 0;5 d7 y9 [0 x0 e: o% [
}; class Rectangle : public Shape w" f& h" T+ [4 x/ P
{# O' n* z5 B& W1 v
public:$ \* a. ]& |. \ y$ f' y1 ^
Rectangle(){};) W4 z! }+ F1 e2 V o' y
~Rectangle(){};# o$ U% w2 w8 L* x
virtual double GetArea(void){return GetWidth()*GetHeight();};
; z- A8 F2 J6 x8 B, M! N" K}; class Circle : public Shape
8 r% M* a* m2 g8 T$ C: K; s{
! z+ [; A8 U, @9 n6 a1 _% Zpublic:
9 C8 l. t! ?! U+ g' V: ^ Circle(){};
3 r3 K% c6 g. Q' N* _* Y+ a ~Circle(){};2 v, `# |1 k3 |2 n+ S
virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};2 i: f3 M0 H! j* ?3 X: J- O/ m" L
void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------, q; { J" }1 l( k1 f; u+ \3 `
// 主函数过程4 W, ?2 J: s8 T8 ^' V0 J' Z
// ------------------------------------------------
3 q8 u1 ^/ i9 ] o* L7 ~int main(int argc, char *argv[])
# P4 g: r _2 u7 P6 h4 j/ o3 \{
+ N \$ Z4 L6 I4 R, g2 ~+ n; Q9 P Rectangle re; Circle ci;, w$ N1 {& f. T1 [' E$ U \
Shape *p1 = &re,*p2 = &ci;
! Q% k6 ?. p1 P7 X, c ? re.SetHeight(20);
- V5 X# W, D) K) S) f5 | re.SetWidth(10);
) a; @! `) R* t3 f) n ci.SetRadius(12);
& m$ {, o7 O5 }8 z2 l0 J, I cout << "矩形面积:" << p1->GetArea() << endl;0 u6 z$ B6 h7 C1 I( f
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE");
( F O4 S r- ~. ^& Q; o return 0;
9 k1 _6 k% W/ P3 Z) l$ p} |