本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
! P4 F P; B D. E+ `#include <stdlib.h> #define PI 3.140 @1 M% }2 E0 y: R: o
using namespace std; class Shape
2 y6 N8 K+ Y" H! f{
- w- }6 x1 K6 H2 \8 o$ Oprivate:
* d$ j# }6 E3 g8 H( H) x( \' Y H double m_width;' L0 s6 i1 M) Q& v* A
double m_height;7 q0 S% X( E4 l5 r
public:) i. u, ?6 a5 I% b" I9 f2 {
Shape(){};9 |8 f# C' [5 @7 }, J
~Shape(){};
9 j. z7 G' N3 h: D5 J C) U void SetWidth(double w = 0){m_width = w;};
# @2 s- ]/ w2 N7 r void SetHeight(double h = 0){m_height = h;};) Q1 D0 E' v5 |
double GetWidth(void){return m_width;};
( w: f* i; P) d( L0 p double GetHeight(void){return m_height;};
" x" ]( X4 p' b! `4 l' q virtual double GetArea(void) = 0;
* @7 }( t3 h) }}; class Rectangle : public Shape9 }/ R6 N, C7 x; H" C& Y
{" {( x% i6 Z+ `9 q" t
public:
) ]$ t7 F& V* D4 T# r# H Rectangle(){};
+ m* \6 L9 P8 p$ L8 f) s3 j ~Rectangle(){};
# s0 H" p" R1 V9 E( L0 J virtual double GetArea(void){return GetWidth()*GetHeight();};
3 j/ [; [3 ^4 v}; class Circle : public Shape
) b0 L" e( K. s{
7 Q( v/ u3 H% O3 D7 m- Gpublic:! J/ T; e, y. ]2 ^5 D; k5 e
Circle(){};
( b j$ `$ n/ P2 U* K0 p ~Circle(){};
! a& E( R5 r3 Z4 [8 X9 F6 ^ virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
* o1 d& a" ]( ~' q% ^" B% r void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------3 o6 { b* j. }9 h0 b- T
// 主函数过程* j) p* r3 F" B, U p* l0 C
// ------------------------------------------------- h" k6 ?' p3 [5 [( p
int main(int argc, char *argv[])8 g) V& Z- H, m) J2 l1 \6 b! O4 k7 l
{9 F2 b. a: C R2 O
Rectangle re; Circle ci;
" j: b: ~! g2 P" w Shape *p1 = &re,*p2 = &ci;( {* d) G% C& c' r, }. m+ t
re.SetHeight(20);3 r% A% n- T5 T$ L
re.SetWidth(10);6 v- |1 v3 J8 e' S
ci.SetRadius(12);
6 @/ ~" Z( K! m: |; Q) b# Z0 t4 m$ v cout << "矩形面积:" << p1->GetArea() << endl;
: p! b+ d t; h6 G! X cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); 7 I8 Q( Q8 o1 u: T: n- {- g
return 0;7 W# I9 O# {* U1 r
} |