10_88.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
源码类别:

文章/文档

开发平台:

C/C++

  1. #include<iostream.h>
  2. const double PI=3.1416;
  3. class shape
  4. {
  5. protected:
  6. double r;
  7. public:
  8. shape(double x){r=x;}
  9. virtual double area()
  10. {
  11. cout<<"No area of this class."<<endl;
  12. return 0.0;
  13. }
  14. virtual double perimeter()
  15. {
  16. cout<<"No perimeter of this class."<<endl;
  17. return 0.0;
  18. }
  19. };
  20. class circle:public shape
  21. {
  22. public:
  23. circle(double x):shape(x){}
  24. double area()
  25. {
  26. return PI*r*r;
  27. }
  28. double perimeter()
  29. {
  30. return 2*PI*r;
  31. }
  32. };
  33. class in_s:public shape
  34. {
  35. public:
  36. in_s(double x):shape(x){}
  37. double area()
  38. {
  39. return 2*r*r;
  40. }
  41. double perimeter()
  42. {
  43. return 4*1.414*r;
  44. }
  45. };
  46. class ex_s:public shape
  47. {
  48. public:
  49. ex_s(double x):shape(x){}
  50. double area()
  51. {
  52. return 4*r*r;
  53. }
  54. double perimeter()
  55. {
  56. return 8*r;
  57. }
  58. friend double operator+(double d,ex_s &e);
  59. };
  60. void main()
  61. {
  62. shape *ptr;
  63. circle c1(11);
  64. in_s c2(12);
  65. ex_s c3(13);
  66. ptr=&c1;
  67. cout<<"The circle's area is:";
  68. cout<<ptr->area();  cout<<endl;
  69. cout<<"The circle's perimeter is:";
  70. cout<<ptr->perimeter(); cout<<endl;
  71. ptr=&c2;
  72. cout<<"The internal square' area is:";
  73. cout<<ptr->area(); cout<<endl;
  74. cout<<"The internal square'perimeter is:";
  75. cout<<ptr->perimeter();cout<<endl; 
  76. ptr=&c3;
  77. cout<<"The external square' area is:";
  78. cout<<ptr->area();cout<<endl;
  79. cout<<"The external square'perimeter is:";
  80. cout<<ptr->perimeter();cout<<endl;
  81. }