本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>$ c6 w$ w) d3 H I8 X
#include <stdlib.h> #define PI 3.145 h4 B: ^: A/ @8 |- _
using namespace std; class Shape
5 h: l0 K, n8 G{. h: i1 t7 x+ u. e* ?9 B
private:
/ s3 m$ O- t. `# {1 P/ t double m_width;
2 R& ]7 H( A$ s/ L1 b double m_height;
# M0 u0 }4 ~& e8 n% ?& ] ~8 opublic:0 C ?) j5 N+ |) f7 c
Shape(){};
, d7 Z: F2 l4 h9 c) c ~Shape(){};
. j" q% @1 j6 o2 Z5 D void SetWidth(double w = 0){m_width = w;};: t. J$ S* g; n3 k* z0 F
void SetHeight(double h = 0){m_height = h;};
3 |8 \4 E" n6 u double GetWidth(void){return m_width;};
|6 E! V( H- x3 S double GetHeight(void){return m_height;};0 p4 b. T; V$ H2 g! {
virtual double GetArea(void) = 0;
+ ?8 [( x8 i" B( ~}; class Rectangle : public Shape6 J" t3 Y5 j# ^# y, n2 w6 ?; o, i2 Q. c
{
; Q( V1 c! X n& \7 }2 k/ y: `public:
* F( K) ?7 k6 c# k/ ~ Rectangle(){};, e- F0 S% O& g, s' c( P8 \0 C B
~Rectangle(){};1 ]* f5 V. V7 B2 t
virtual double GetArea(void){return GetWidth()*GetHeight();};; U" p$ F8 H3 c; V& ]6 t
}; class Circle : public Shape
- B6 `! J5 L, a6 |% C- H, i{7 b5 a! Q7 x+ K3 K
public:
, A# M$ _9 M9 p% A, D9 F Circle(){};# R( O" R ~, {7 s1 m
~Circle(){};
$ X) ^" u6 v6 o1 W: s7 |& I- w virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};, \. R( V7 I+ ~6 [# b0 |: a y
void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
. d! j' J9 A/ R: ^! \// 主函数过程9 a4 n6 V( I2 U6 X) _- S8 q
// ------------------------------------------------9 C! e( g7 |3 W$ e
int main(int argc, char *argv[])3 f. v0 t$ I4 B( o3 x- a4 Q0 W
{8 y8 ?4 v: g8 h0 J
Rectangle re; Circle ci;6 R. o# H. S* H4 [
Shape *p1 = &re,*p2 = &ci;
- X3 b- i% e- f* f5 m' e re.SetHeight(20);( ?2 c) f2 Y& n
re.SetWidth(10);
+ D, k2 } T( [' S2 C ci.SetRadius(12);8 ?2 _2 Y+ R! r: w4 O
cout << "矩形面积:" << p1->GetArea() << endl; o- L9 n7 ?# T: R3 g( m! m
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE");
5 _ ^$ a( M: {4 Z! R return 0;( r5 D4 O* u3 ^8 N, o
} |