本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>
, i6 P4 t, \- s a' o s0 U#include <stdlib.h> #define PI 3.14$ e% f( {* i" m' u' J
using namespace std; class Shape
* K j0 }. e6 y{8 ^( n$ l" r6 a! J+ M, n
private:. s. ~) b, U% W8 t4 C4 j) L& c+ B/ ]
double m_width;, u& j2 H; b4 u6 V! T- E& W
double m_height;( A, P! @- Y* X8 Z# J, W5 m
public:$ z% Y9 B% ?0 w$ X- {
Shape(){};! A U& T# z3 \9 y2 U
~Shape(){};
- @" C0 O! }% A+ U/ J \* `7 _ void SetWidth(double w = 0){m_width = w;};
" T/ F- O6 @( R/ A: | void SetHeight(double h = 0){m_height = h;};* a9 l X. b! |1 W. M' k
double GetWidth(void){return m_width;};4 h6 i- E5 \% \1 [, Q, w S
double GetHeight(void){return m_height;};
( a u& a7 }3 \ virtual double GetArea(void) = 0;
9 Y, t2 V! R+ |. C}; class Rectangle : public Shape7 \4 r, v9 W( T- I i" n
{
. U3 C" b- F5 V4 Cpublic:
/ X( D, f% L# S$ [' m Rectangle(){};; V5 j7 }" g; }7 H2 [9 \: ?
~Rectangle(){};
/ W$ p3 y3 A& g virtual double GetArea(void){return GetWidth()*GetHeight();};
1 Y+ _3 D3 V) e& o}; class Circle : public Shape2 v& O3 [3 C2 z2 a
{
, R i$ r7 P8 zpublic:
5 X& W9 a& C* C& b Circle(){};. A6 U6 Z5 ~5 J# a2 d5 ?
~Circle(){};. T& N3 K3 F: ~. J% _$ `5 H* y
virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
+ |! }% v4 h$ L: O+ F void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
4 s$ ^" X& j* _// 主函数过程- G- ^: F! C: R# a/ Q9 m3 {) p
// ------------------------------------------------+ x% k0 ^) L" J0 W2 |; V; o
int main(int argc, char *argv[])
3 v% \2 T+ C7 g. ~9 V* N# F{
# U) X, F( m; e3 A. a' W; G Rectangle re; Circle ci;( ]5 F: i6 D' V$ m+ ?( M
Shape *p1 = &re,*p2 = &ci;
8 \3 K% N% D% B re.SetHeight(20);
; P9 A# K* @9 X* _ re.SetWidth(10);) u/ q) Y! q5 y, }- z
ci.SetRadius(12);
$ P5 b5 J5 M% }- Z1 P4 S cout << "矩形面积:" << p1->GetArea() << endl;. h. g( d5 _+ o b3 H6 m9 K n- u
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE");
/ O1 A- i; [6 D- k5 R return 0;
2 N5 |4 b! d1 R: X# ^} |