本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>3 Q2 s" j' H D8 r9 A) W! @
#include <stdlib.h> #define PI 3.145 u4 X/ N& \) p, ^
using namespace std; class Shape, Q( s# s' Z @
{
" A+ P+ [- n* J8 u0 x' iprivate:
! i/ K, c/ N, a! ^ double m_width;2 ~/ ]4 I$ V- s6 n) V, t1 E# F& M
double m_height;# ]0 m. K- ~' x5 z3 Y
public:$ i5 j$ V" R; {" b) f* P9 Z$ l
Shape(){};8 K2 V! h* _! m( _, @$ k1 v, p2 _) p
~Shape(){};/ b6 g1 r. G5 v( ?! e
void SetWidth(double w = 0){m_width = w;};
4 Q) } Z$ x, z" U4 Z void SetHeight(double h = 0){m_height = h;};
, F: U1 G. R6 U3 ]& X double GetWidth(void){return m_width;};* ]4 a9 r; U7 x8 {; {
double GetHeight(void){return m_height;};
- S3 n ]% n2 E. }: m8 S. B6 m virtual double GetArea(void) = 0;6 H" H9 M' u0 `3 B
}; class Rectangle : public Shape# i9 e! t6 j3 N% S8 y. ?
{
4 n& _! s% R1 V% B/ q+ ?+ ~6 Fpublic:9 W" f9 s/ l( x# p- k
Rectangle(){};
) l: C& @8 x8 ` ~Rectangle(){};
& y+ r! U. C+ ]3 o; ^5 C0 W% J9 A virtual double GetArea(void){return GetWidth()*GetHeight();};) W% W6 q4 e& {: u9 C% t! _
}; class Circle : public Shape4 d7 c) ]% y/ u- J" M4 ]
{7 ^# q0 N T& u& u9 F5 m2 T
public:
6 T4 N: [( h+ Z' ]9 ? Circle(){};% y( J' V0 Y- R8 c/ r3 v
~Circle(){};
, C+ N; w2 R2 d* \) L1 L0 N6 M/ k. S virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};, {& q4 F, J% M8 Y
void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------6 B6 k3 K& b- D% K2 t
// 主函数过程' Q9 `' R9 m% e% F. Y
// ------------------------------------------------4 V3 l5 f8 ^/ X' O: V
int main(int argc, char *argv[])
s& v. k7 @+ D8 L# r{
+ r# M7 K7 X _& K* M9 i Rectangle re; Circle ci;% |* ?$ I; \* T" Y( j% q
Shape *p1 = &re,*p2 = &ci;
6 ]: F; n, Q! W4 y re.SetHeight(20);0 O+ Z" J" E, N! Y8 S/ N1 T6 J
re.SetWidth(10);4 W% s E! T* w- F
ci.SetRadius(12);
' U" q4 ?( P* g6 f$ ` cout << "矩形面积:" << p1->GetArea() << endl;
" A- |/ Q9 k8 ?1 P- ^ cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); 7 ]+ E& i# U" U$ P- s2 I9 _2 D
return 0;
# ?: G/ K2 S# s+ s& l' j C} |