本来想写个试试,可为什么Dev-cpp会提示我Rectangle未定义?奇怪 VC7就很顺利? #include <iostream>( R5 h1 t6 p m7 L: _ r
#include <stdlib.h> #define PI 3.14# V. S J2 O; R- \7 r9 J0 b
using namespace std; class Shape$ M! `, C# k6 d
{
$ [8 o# B* r. l- g) u( `5 bprivate:
( @8 C2 A- u5 A7 q- c* x! q2 d double m_width;! `$ r% f& A' ~+ D! z' x
double m_height;5 R, H2 O2 `2 ~; Q1 R% r
public:7 D, v# T1 X4 \9 Q
Shape(){};( }' L; L( n$ V% }) H
~Shape(){};
8 _: B9 Z v9 p' G void SetWidth(double w = 0){m_width = w;};) w M7 u" m8 r) R
void SetHeight(double h = 0){m_height = h;};3 K$ M8 J$ J: [
double GetWidth(void){return m_width;};6 e8 O- ^3 _ n6 e( X" n& T* o3 f' i
double GetHeight(void){return m_height;};( O# i' C; I( L1 R
virtual double GetArea(void) = 0;; a4 y2 u3 ]9 l6 k1 f
}; class Rectangle : public Shape
7 b9 w2 R+ [! i{
$ w- I7 g8 j+ W8 \2 x+ Spublic:
2 ?2 F3 {$ B; M9 k6 P& B: Y Rectangle(){};
/ @* [, b+ J2 Z ~Rectangle(){};
9 e, }; }4 c$ X! w0 O+ { virtual double GetArea(void){return GetWidth()*GetHeight();};) n' N. a c" A1 `" Y: E" V
}; class Circle : public Shape
! C! [) Y! f/ C6 D0 c{
$ h! B9 B6 X+ h; ]public:+ D+ W4 T0 C s
Circle(){};% G H' s& Y: f7 {+ d
~Circle(){};
4 h+ v7 ~2 ]4 R3 U virtual double GetArea(void){return GetWidth()*GetWidth()*PI;};
( T6 {& q. p: ^3 G void SetRadius(double r = 0){SetWidth(r);}; }; // ------------------------------------------------
& w- [6 w5 k6 W) ^& H1 f// 主函数过程% d0 i3 c; ]3 H o
// ------------------------------------------------0 i- k* D/ s' I- O) X( Z0 m
int main(int argc, char *argv[])
5 k5 G7 F# L. K R$ Q1 P{3 S ]& y( N5 \' t: [' Q
Rectangle re; Circle ci;
! \% N5 ^5 v# p3 N7 S i9 @, L Shape *p1 = &re,*p2 = &ci;. y0 ?7 F9 ~9 F) @! `
re.SetHeight(20);0 C6 r5 m) }0 `2 g/ w9 c' L( _
re.SetWidth(10);
- R. N8 Y/ n2 n# V- H/ ^* t ci.SetRadius(12);9 ?/ u) P0 _2 a- D
cout << "矩形面积:" << p1->GetArea() << endl;0 e% D, K9 F; c
cout << "圆形面积:" << p2->GetArea() << endl; system("PAUSE");
' T( u9 }( {# A" [5 ` return 0;- V# D! @5 \- Q5 m, D `
} |