本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
, w! p c' s C#include <stdlib.h> #define PI 3.14
1 h/ x6 j4 @1 g5 C* K% vusing namespace std; class Shape
$ t& z+ K/ C" b1 L& f! v{
0 M: O$ m, M- b$ K3 j* ]5 Eprivate:/ n; w) v2 _, [1 s
double m_width;1 Y @+ Y3 J& H' @" s! j
double m_height;
& v% m" w5 A) }6 q2 ?public:
" K( _" R- S5 J' e. |& s, o0 m Shape(){};
6 k( G5 p" S6 }5 z/ e& C ~Shape(){};' c* ^1 X. b; n- V' e) F
void SetWidth(double w = 0){m_width = w;};# z X1 i& g2 E3 q3 @
void SetHeight(double h = 0){m_height = h;};5 S( a+ }+ v1 a6 ]
double GetWidth(void){return m_width;};7 K3 c1 z" ]# O0 Y: G8 ?5 ?
double GetHeight(void){return m_height;};6 x6 K" @# }4 p1 r
virtual double GetArea(void) = 0;4 n1 O) r8 L9 `
}; class Rectangle : public Shape/ E( s! Z; I# @& z7 A/ z) m1 Y
{
% D% k) N, I$ p* }public:6 L- l* U! e. y9 A! ^7 f3 z
Rectangle(){};
6 m w# I' r# {% k# F$ v! ? ~Rectangle(){};
& Q, T, j7 Q1 n( m Y0 J3 |) Q+ b) J virtual double GetArea(void){return GetWidth()*GetHeight();};# E4 u- x" r$ L: s; J0 E$ J
}; class Circle : public Shape
" s: p- G9 F9 P{
3 ~2 k2 ^5 l( qpublic:: O# B1 Y; O* R3 d* z4 ~
Circle(){};
% }& m+ J+ P# h( d8 U! s: V# d& {# | ~Circle(){};
0 F) d! i3 \4 C4 T8 S virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
( n) g. N- N r# o5 o: [ I! t8 T void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------( w1 W) T, l+ `( V
// 主函数过程
' ]" v; i+ J7 a% x2 }" |+ t// ------------------------------------------------
, K/ [( }/ M" K' U) Fint main(int argc, char *argv[])
# W$ D( |3 U1 J' ]{' d, ]8 L: K* W$ F
Rectangle re; Circle ci;+ t G$ }5 R5 M1 b/ T; C6 |+ t; ?
Shape *p1 = &re,*p2 = &ci;
' {) l# b- `5 A( t$ U- U re.SetHeight(20);1 |7 i& k. G( k! q" Q$ S4 p1 y5 V' N
re.SetWidth(10);
; m) u. Y7 v$ d- Q9 K# ] ci.SetRadius(12);8 n" P+ Y3 g9 @) i0 `6 i4 y/ a& H
cout << "矩形面积:" << p1->GetArea() << endl;
2 i2 W5 e& z; k4 l: X, B% U( k cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); 2 n* W; V2 u! B8 {1 i4 y% Y
return 0;
: l6 C7 ]) K5 s* `' T. A+ E} |