本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
+ I+ F% I& w3 \+ b8 z4 f#include <stdlib.h> #define PI 3.146 K% J' ^) w" t" h+ q, s( h( d6 a0 Q% m
using namespace std; class Shape) H S4 ?! k& S
{4 |5 z( e4 O: W1 u+ T- ~4 w) u
private:) p( u1 O) X% a7 O
double m_width;" ^# I2 v/ s2 x
double m_height;
: i$ d- V5 F- F2 \7 @public:* [8 G* b$ ]& v
Shape(){};
& ?% A. b/ v: [& i7 `0 l ~Shape(){};; j, Q/ l6 y4 V
void SetWidth(double w = 0){m_width = w;};
7 ? R. Q* u* P, f void SetHeight(double h = 0){m_height = h;};" P' t; d7 @+ U2 F
double GetWidth(void){return m_width;};1 W/ L! ~& _, S: g Q
double GetHeight(void){return m_height;};
- Y8 A) ^& F' b Q0 W) e/ x virtual double GetArea(void) = 0;
) d6 v! e2 P3 c8 h}; class Rectangle : public Shape8 W" l" J% [9 C" w
{% L# j' d4 h. X. E4 H! a$ W; O
public:& n/ Y r, G: H, d
Rectangle(){};9 K* m$ [3 P( \$ d/ K* w, m2 `; T
~Rectangle(){};
! f) q5 K: | d, \& ~) R virtual double GetArea(void){return GetWidth()*GetHeight();};& Y+ z' u% T" U4 p
}; class Circle : public Shape( r0 L- i' L! t5 B! w# V W9 @
{
R* O3 D: x+ rpublic:. k$ R& P. Z5 q# `" k- G# }+ C
Circle(){};
1 M" u+ Y: u! h& ?; U2 ` ~Circle(){};
N' ]8 ~( n7 u% U* c# A& p/ { virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
0 L3 y6 Q; @+ F4 K* h+ K void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
( Z/ @+ x' @1 q// 主函数过程% {5 J! y- X+ L: E
// ------------------------------------------------5 Z, b2 c& o0 [, g; c- v1 @. S. v
int main(int argc, char *argv[])
9 }* h% J* s- b! k{
6 O: y1 C5 \/ Q. n4 p4 f Rectangle re; Circle ci;- m0 e, O; m" C% Q7 @" S
Shape *p1 = &re,*p2 = &ci;
" v( x* g# L( C% B re.SetHeight(20);
! q3 z- b( t1 b2 y: C re.SetWidth(10);
2 ?7 |# H" k( `6 u, [2 {+ b$ a ci.SetRadius(12);
# q/ w) T) {- P. D& m: q8 k) K cout << "矩形面积:" << p1->GetArea() << endl;
2 B2 F& n- f3 R4 n% G) E cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); 2 K! p$ S8 x0 V, }9 F1 b/ t( m
return 0;
' X! d. K7 J' b8 ]} |