本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
. L& Y8 c7 f/ Q#include <stdlib.h> #define PI 3.14
+ J8 V4 Z- u) Q$ v: Q6 k/ D5 Eusing namespace std; class Shape, [6 q' P# F+ e5 A9 v' I
{. c. t, s% L& @
private:4 L" M8 s3 o4 Y( i. p
double m_width;
u$ w; L5 \" t7 f2 E3 l3 N* V double m_height;
4 q+ j$ @$ t* s9 zpublic:
/ z4 G; X T6 }" A4 M& M' } Shape(){};
3 @9 Z# H# ~ {4 z( k6 J ~Shape(){};
, j6 f! p" F9 d r8 q void SetWidth(double w = 0){m_width = w;};! b/ e+ ^- Z4 `& `
void SetHeight(double h = 0){m_height = h;};
& Y- D/ i6 F4 {( O double GetWidth(void){return m_width;};
+ h4 u! ]' ^0 B$ s9 ]* m4 f) P double GetHeight(void){return m_height;}; D u% b, T9 G
virtual double GetArea(void) = 0;; i! \/ Z' p( n
}; class Rectangle : public Shape
9 L7 Z3 _( w9 J/ A e b{
& m" I* v$ N dpublic:
6 X" F4 S ?8 w Rectangle(){};
: ~) H3 g1 g: e' K0 U ~Rectangle(){};, H6 t/ V& |/ P# f' V( X
virtual double GetArea(void){return GetWidth()*GetHeight();};
7 K% }: |5 \, x- t8 s/ x" f C}; class Circle : public Shape7 O3 R+ t4 S/ o
{
T/ C7 W! R6 Z2 s( O. o! z+ zpublic:
" W8 f( A. p! ]7 h X Circle(){};
2 m m& A3 a) a/ S$ z$ p ~Circle(){};
* N! ?' v: G4 O" I: N6 X* { virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};5 ~4 k2 s# ?3 [5 c3 `
void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
0 K# {. w" k/ ^// 主函数过程
7 ? s. U6 C- W// ------------------------------------------------
$ t2 t- ]0 {, `7 { pint main(int argc, char *argv[])( U+ L' [4 j, [+ U- i1 e+ K
{
9 G1 A/ x% L$ G Rectangle re; Circle ci;: ]$ K9 M: f1 ]+ W' f" v7 q, \
Shape *p1 = &re,*p2 = &ci;
- i8 _0 J* a* g) b! ^# W; {& e& ? re.SetHeight(20);
% Z5 a+ W) \+ d# L re.SetWidth(10);. k8 A) }5 m+ _, M0 p& ?
ci.SetRadius(12);
3 z: G+ ] `+ m cout << "矩形面积:" << p1->GetArea() << endl;
# Q8 H6 l3 S, l, g1 b4 w cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); + C! | T* \7 r
return 0;
% O$ O" A1 o9 Q} |