10_88.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
- #include<iostream.h>
- const double PI=3.1416;
- class shape
- {
- protected:
- double r;
- public:
- shape(double x){r=x;}
- virtual double area()
- {
- cout<<"No area of this class."<<endl;
- return 0.0;
- }
- virtual double perimeter()
- {
- cout<<"No perimeter of this class."<<endl;
- return 0.0;
- }
- };
- class circle:public shape
- {
- public:
- circle(double x):shape(x){}
- double area()
- {
- return PI*r*r;
- }
- double perimeter()
- {
- return 2*PI*r;
- }
- };
- class in_s:public shape
- {
- public:
- in_s(double x):shape(x){}
- double area()
- {
- return 2*r*r;
- }
- double perimeter()
- {
- return 4*1.414*r;
- }
- };
- class ex_s:public shape
- {
- public:
- ex_s(double x):shape(x){}
- double area()
- {
- return 4*r*r;
- }
- double perimeter()
- {
- return 8*r;
- }
- friend double operator+(double d,ex_s &e);
- };
- void main()
- {
- shape *ptr;
- circle c1(11);
- in_s c2(12);
- ex_s c3(13);
- ptr=&c1;
- cout<<"The circle's area is:";
- cout<<ptr->area(); cout<<endl;
- cout<<"The circle's perimeter is:";
- cout<<ptr->perimeter(); cout<<endl;
- ptr=&c2;
- cout<<"The internal square' area is:";
- cout<<ptr->area(); cout<<endl;
- cout<<"The internal square'perimeter is:";
- cout<<ptr->perimeter();cout<<endl;
- ptr=&c3;
- cout<<"The external square' area is:";
- cout<<ptr->area();cout<<endl;
- cout<<"The external square'perimeter is:";
- cout<<ptr->perimeter();cout<<endl;
- }