本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>- `/ x# g( p# @. P, k. p" w
#include <stdlib.h> #define PI 3.14- C1 j4 O0 X5 F2 W, K
using namespace std; class Shape* [- u. ]/ n( B; q2 X* R
{8 D& N% f, X* f4 c
private:( e! e1 m. o) Y
double m_width;
1 ?3 Z" I: R% d9 H2 v% ] double m_height;+ v; |$ n8 z% m4 ?. Q" b0 k
public:
2 R* b2 c z+ e. V6 Q, M Shape(){};1 c6 ?! k" f7 u! o8 t
~Shape(){};9 t2 O( ^1 O0 K( Y( A1 n8 w6 d- _
void SetWidth(double w = 0){m_width = w;};2 V* D, b/ b8 f% h
void SetHeight(double h = 0){m_height = h;};8 J* e+ K. x( H
double GetWidth(void){return m_width;};1 X! x- m7 P: C/ v m
double GetHeight(void){return m_height;};
+ K& {+ W4 y6 _* p3 b3 ~ virtual double GetArea(void) = 0;
' w' V# ~; N' c8 | p}; class Rectangle : public Shape
* ^. G- k* @* { |{% ^& C, t! ]9 k9 w- z6 J) J( g( h0 S
public:
1 l X/ m. w @4 g4 y( ] Rectangle(){};2 o, o6 h2 |$ J2 f. f
~Rectangle(){};
; {1 N8 @3 ~8 Y, L virtual double GetArea(void){return GetWidth()*GetHeight();};
- U6 [) I2 }6 J}; class Circle : public Shape
' H. i3 [: ]3 N" \{
( C7 }* @, T) f# w, {6 r6 Vpublic:6 u, |8 s* U2 z
Circle(){};) s' S4 }# H) ]& T& A e
~Circle(){};
* z- ?; k6 z- I% ?$ d O: n virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};6 s$ {7 i. S' C X2 _: ]3 b
void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------ }& O6 y. J3 [' y) ~
// 主函数过程" I- C( g& b F H
// ------------------------------------------------) x: w3 H0 m0 W0 q
int main(int argc, char *argv[])
) a' U- f; _9 @' [1 I! Z0 L{
1 O1 Z4 D8 [$ e# ? Rectangle re; Circle ci;
" q% [8 [ ?2 b( f Shape *p1 = &re,*p2 = &ci;: t/ I: V7 ~ Y8 D
re.SetHeight(20);
3 t, z+ l1 H; [ re.SetWidth(10);
* P* r: }1 \& W6 h$ ?6 r8 j+ L ci.SetRadius(12);9 ]& V; C7 \$ R; L. U6 @
cout << "矩形面积:" << p1->GetArea() << endl;2 ]( k" o, ]+ w8 N8 G1 w
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE"); : v0 [8 V$ R9 {4 U
return 0;& ^% z: I' _% w$ H8 y) x/ B
} |