本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>. W. F4 ]: j/ r! l0 G6 B& P9 [
#include <stdlib.h> #define PI 3.14" \0 b3 ?5 \8 `0 N( D" y. M
using namespace std; class Shape
* Z4 z: h: z2 g& A{
" k4 B$ [! _. C0 C# G7 uprivate:
# I( L1 @3 f( g double m_width;
1 [, g$ @2 d8 Z& h' C' U1 P double m_height;
. i2 C; d1 e6 Z0 q8 ^, rpublic:
4 p" \! z' w3 R" `& Z Shape(){};
S0 H2 H$ f. m/ b7 P* t, s ~Shape(){};6 b8 F$ H( Z% B+ |* }3 ]
void SetWidth(double w = 0){m_width = w;};
. F! @! l2 O/ p6 j' X i4 [ void SetHeight(double h = 0){m_height = h;};! \, J% J/ q9 z3 f$ {: y5 N' K
double GetWidth(void){return m_width;};/ K7 }4 e3 l1 P% H0 K. `
double GetHeight(void){return m_height;};$ e# J/ B# c( T3 N/ G$ |. ]
virtual double GetArea(void) = 0;
; E+ D$ t' d0 Q; c$ R- S: x# @}; class Rectangle : public Shape/ f$ |' U( U$ `
{" j* Q. a& e' n3 O2 s: U @( H
public:2 m4 x% y! S, ~" n/ @2 @
Rectangle(){};! b2 d5 F. Z! h R
~Rectangle(){};
* `( p! c3 Z3 G5 {- |# r- V* T virtual double GetArea(void){return GetWidth()*GetHeight();};
& A" p- R. |. p5 E, D% u4 {# N. q}; class Circle : public Shape* J3 V1 H, g3 i& o5 A
{: M8 {$ k! l9 H$ T5 b, K
public:
3 ~' c6 N+ J$ C' W- M5 D, @& H Circle(){};: L) v% p7 x' } |
~Circle(){};
$ N; L/ O1 @) ?0 n: z( `1 { virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
, E# `2 r( X; c void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
_* c0 f! g, ~0 ~7 a* V: ~( m/ L% u// 主函数过程
2 H. y# w, o+ B6 \8 h// ------------------------------------------------6 ?4 e% _4 X" z( P/ a8 W
int main(int argc, char *argv[])4 m' o- @8 B9 d' Z& E5 M/ }
{1 @3 d3 [( M/ I$ l9 t f
Rectangle re; Circle ci;6 _4 f& @7 K* T- P) W9 S
Shape *p1 = &re,*p2 = &ci;
6 x0 y/ d6 b5 B, ^( F) W8 { re.SetHeight(20);
, {6 ]6 Y' ]& `0 f, s! H# ^9 \ re.SetWidth(10);6 \/ e9 V$ ^* V) P( ]+ O
ci.SetRadius(12);
, C s, A1 I6 o A h8 O% g5 r cout << "矩形面积:" << p1->GetArea() << endl;
2 @( w" O, h' P: N; E4 t* ^- _- U cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); ; q3 Z# @8 H- G" j5 k
return 0; `" \- R l9 A
} |