本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>2 p- |+ I# S) Z# P
#include <stdlib.h> #define PI 3.14
8 g: W- S1 ^, i& F* z' Uusing namespace std; class Shape
$ L5 E& n. X0 I; x6 }" K{
; y9 B# l" _$ K3 Z# }5 V0 P; Rprivate:; g2 D& N4 T( s- M
double m_width;5 A% F7 U* I& l4 _6 `' l
double m_height;% m. I3 X! k# m2 ?8 Z F$ ]
public:$ M! U- t1 \9 p% f
Shape(){};
& Z! N6 M+ @% O2 _/ T3 \6 B ~Shape(){};
2 c u( E- s: B3 Z) h void SetWidth(double w = 0){m_width = w;};
9 j. q: D0 F& l9 {( { d- ] void SetHeight(double h = 0){m_height = h;};) D5 @7 |- z8 p9 O/ ~& H
double GetWidth(void){return m_width;};
+ u( V+ f# o: b. M+ T! d: G% F double GetHeight(void){return m_height;};
: |( ?# R2 |4 G0 c: H virtual double GetArea(void) = 0;. H' ~6 @* [* ]9 F
}; class Rectangle : public Shape2 x- R9 f, D N. w( l% V/ x
{* L B# A" \5 W( x$ v- S" ^
public: r6 {& D. \( i2 H4 p" o
Rectangle(){};' t% k) Q& r6 U1 Q' _! z7 V
~Rectangle(){};( E+ y6 i: L( r6 e
virtual double GetArea(void){return GetWidth()*GetHeight();};$ o4 j; m2 n i
}; class Circle : public Shape
- Q. o3 ?3 W/ }2 X8 S' \{
; R, P# b& N, C$ C& dpublic:
+ X/ L1 B1 }0 R# I* }. } Circle(){};
" z: T e: o$ k% w' U ~Circle(){};* l9 n+ X+ \. H' n1 t& H, O
virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
8 V- r8 t: Z: d# v void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
3 C3 y7 j4 @+ w; x* {// 主函数过程
/ O# I z7 N$ q C! p- u! z// ------------------------------------------------- M. N$ N- i( c
int main(int argc, char *argv[])
- f7 Q: W7 f$ ]{
8 Z, Q* l( G# F8 q Rectangle re; Circle ci;! k/ j' b$ r* {9 q1 i0 \
Shape *p1 = &re,*p2 = &ci;
. w q0 I# f$ N2 m re.SetHeight(20);* K C# g1 R/ U% e
re.SetWidth(10);
! e q; _1 ~! {1 a4 _4 N, o ci.SetRadius(12);
6 f3 D7 A3 ^( J/ l$ ^6 i! q cout << "矩形面积:" << p1->GetArea() << endl;
& I: P! `& J5 j% Q cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); n: H, O% c0 A1 K) j
return 0;) r z$ E. M& t. P3 ^' m
} |