ep8_5.cpp
上传用户:wxcui2006
上传日期:2022-07-12
资源大小:1274k
文件大小:4k
源码类别:

书籍源码

开发平台:

Visual C++

  1. /*8.5 以点(point)类为基类,重新定义矩形类和圆类。点为直角坐标点,矩形水平放置,由左下方
  2. 的顶点和长宽定义。圆由圆心和半径定义。派生类操作判断任一坐标点是在图形内,还是在图形的边缘
  3. 上,还是在图形外。缺省初始化图形退化为点。要求包括拷贝构造函数。编程测试类设计是否正确。*/
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7. const double PI=3.1415926535;
  8. class Point{
  9. private:
  10. double x,y;
  11. public:
  12. Point(){x = 0; y = 0; }
  13. Point(double xv,double yv){x = xv;y = yv;}
  14. Point(Point& pt){ x = pt.x; y = pt.y; }
  15. double getx(){return x;}
  16. double gety(){return y;}
  17. double Area(){return 0;}
  18. void Show(){cout<<"x="<<x<<' '<<"y="<<y<<endl;}
  19. };
  20. class Circle :public Point{
  21. double radius;
  22. public:
  23. Circle(){radius = 0;}
  24. Circle(double xv,double yv,double vv):Point(xv,yv){radius = vv;}
  25. Circle(Circle& cc):Point(cc){radius = cc.radius;}  //拷贝构造函数
  26. double Area(){return PI*radius*radius;}
  27. void Show(){
  28. cout<<"x="<<getx()<<'t'<<"y="<<gety()<<'t'<<"radius="<<radius<<endl;//访问基类的数据成员
  29. }
  30. int position(Point &pt){
  31. double distance = sqrt((getx()-pt.getx())*(getx()-pt.getx())
  32.                     +(gety()-pt.gety())*(gety()-pt.gety()));
  33.     double s=distance-radius;
  34. if(s==0) return 0;    //在圆上
  35. else if(s<0) return -1; //在圆内
  36. else return 1; //在圆外
  37. }
  38. };
  39. class Rectangle:public Point{
  40. double width,length;
  41. public:
  42. Rectangle(){width=0; length=0; }
  43. Rectangle(double xv,double yv,double wv,double lv):Point(xv,xv) {
  44. width = wv;
  45. length= lv;
  46. }
  47. Rectangle(Rectangle& rr):Point(rr){
  48. width = rr.width;
  49. length = rr.length;
  50. }
  51. double Area(){return width*length;}
  52. void Show(){
  53. cout<<"x="<<getx()<<'t'<<"y="<<gety()<<'t';
  54. cout<<"width="<<width<<'t'<<"length="<<length<<endl;
  55. }
  56. int position(Point &pt);
  57. };
  58. int Rectangle::position(Point &pt){
  59. double s1,s2;
  60. s1 = (pt.getx()-getx()); s2=(pt.gety()-gety());
  61. if(((s1==0||s1==width)&&s2<=length)||((s2==0||s2==length)&&s1<=width)) return 0; //在矩形上
  62. else if(s1<width&&s2<length) return -1; //在矩形内
  63. else return 1; //在矩形外
  64. }
  65. int main(){
  66. Circle cc1(3,4,5),cc2,cc3(cc1);
  67. Rectangle rt1(0,0,6,8),rt2,rt3(rt1);
  68. Point p1(0,0),p2(6,8),p3(3,3),p4(8,4),p5(8,8);
  69. cc1.Show();
  70. cc2.Show();
  71. rt1.Show();
  72. rt2.Show();
  73. cout<<"点p1:";
  74. p1.Show();
  75. cout<<"矩形rt3:"<<'t';
  76. rt3.Show();
  77. switch(rt3.position(p1)){
  78. case 0:cout<<"在矩形上"<<endl;break;
  79. case -1:cout<<"在矩形内"<<endl;break;
  80. case 1:cout<<"在矩形外"<<endl;break;
  81. }
  82. cout<<"圆cc3:"<<'t';
  83. cc3.Show();
  84. switch(cc3.position(p1)){
  85. case 0:cout<<"在圆上"<<endl;break;
  86. case -1:cout<<"在圆内"<<endl;break;
  87. case 1:cout<<"在圆外"<<endl;break;
  88. }
  89. cout<<"点p2:";
  90. p2.Show();
  91. cout<<"矩形rt3:"<<'t';
  92. rt3.Show();
  93. switch(rt3.position(p2)){
  94. case 0:cout<<"在矩形上"<<endl;break;
  95. case -1:cout<<"在矩形内"<<endl;break;
  96. case 1:cout<<"在矩形外"<<endl;break;
  97. }
  98. cout<<"圆cc3:"<<'t';
  99. cc3.Show();
  100. switch(cc3.position(p2)){
  101. case 0:cout<<"在圆上"<<endl;break;
  102. case -1:cout<<"在圆内"<<endl;break;
  103. case 1:cout<<"在圆外"<<endl;break;
  104. }
  105. cout<<"点p3:";
  106. p3.Show();
  107. cout<<"矩形rt3:"<<'t';
  108. rt3.Show();
  109. switch(rt3.position(p3)){
  110. case 0:cout<<"在矩形上"<<endl;break;
  111. case -1:cout<<"在矩形内"<<endl;break;
  112. case 1:cout<<"在矩形外"<<endl;break;
  113. }
  114. cout<<"圆cc3:"<<'t';
  115. cc3.Show();
  116. switch(cc3.position(p3)){
  117. case 0:cout<<"在圆上"<<endl;break;
  118. case -1:cout<<"在圆内"<<endl;break;
  119. case 1:cout<<"在圆外"<<endl;break;
  120. }
  121. cout<<"点p4:";
  122. p4.Show();
  123. cout<<"矩形rt3:"<<'t';
  124. rt3.Show();
  125. switch(rt3.position(p4)){
  126. case 0:cout<<"在矩形上"<<endl;break;
  127. case -1:cout<<"在矩形内"<<endl;break;
  128. case 1:cout<<"在矩形外"<<endl;break;
  129. }
  130. cout<<"圆cc3:"<<'t';
  131. cc3.Show();
  132. switch(cc3.position(p4)){
  133. case 0:cout<<"在圆上"<<endl;break;
  134. case -1:cout<<"在圆内"<<endl;break;
  135. case 1:cout<<"在圆外"<<endl;break;
  136. }
  137. cout<<"点p5:";
  138. p5.Show();
  139. cout<<"矩形rt3:"<<'t';
  140. rt3.Show();
  141. switch(rt3.position(p5)){
  142. case 0:cout<<"在矩形上"<<endl;break;
  143. case -1:cout<<"在矩形内"<<endl;break;
  144. case 1:cout<<"在矩形外"<<endl;break;
  145. }
  146. cout<<"圆cc3:"<<'t';
  147. cc3.Show();
  148. switch(cc3.position(p5)){
  149. case 0:cout<<"在圆上"<<endl;break;
  150. case -1:cout<<"在圆内"<<endl;break;
  151. case 1:cout<<"在圆外"<<endl;break;
  152. }
  153. return 0;
  154. }