fork download
  1. #include <iostream>
  2. #include<cmath>
  3. using namespace std;
  4. class Point{
  5. private:
  6. double x,y;
  7. public:
  8. Point(double x, double y){
  9. this->x=x;
  10. this->y=y;
  11. }
  12. void setVar(double x, double y){
  13. this->x=x;
  14. this->y=y;
  15. }
  16. double getX(){return x;}
  17. double getY(){return y;}
  18. void add(Point p){
  19. x+=p.x;
  20. y+=p.y;
  21. }
  22. // double calculateDistFromOrigin(){
  23. // return sqrt(x*x+y*y);
  24. // }
  25. };
  26.  
  27.  
  28. class Point3D : private Point{
  29. private:
  30. double z;
  31. public:
  32. Point3D(double x, double y, double z): Point(x, y){
  33. this->z=z;
  34. }
  35. // void setVar(double x, double y, double z){
  36. // Point::setVar(x,y); ////
  37. // this->z=z;
  38. // }
  39. void setZ(double z){
  40. this->z=z;
  41. }
  42.  
  43. void setVarDelegate(double x, double y){
  44. setVar(x, y);
  45. }
  46.  
  47. double getXDel(){return getX();}
  48. double getYDel(){return getY();}
  49. double getZ(){return z;}
  50.  
  51. void add(Point3D p){
  52. Point::add(Point(p.getX(), p.getY()));
  53. // Point::add(Point(p.x, p.x));
  54. z+=p.z;
  55. }
  56.  
  57. // double calculateDistFromOrigin(){
  58. // return sqrt(getX()*getX()+getY()*getY()+getZ()*getZ());
  59. // }
  60. };
  61.  
  62.  
  63. int main(){
  64. Point p1(2.5, 3.2);
  65. p1.add(Point(5.0, 5.0));
  66.  
  67. cout<<"(x, y, z)="<<"("<<p1.getX()<<", "<<p1.getY()<<")"<<endl;
  68.  
  69. Point3D p(1.2, 2.5, 3);
  70. p.add(Point3D(5.0, 5.0, 5.0));
  71.  
  72. //cout<<"(x,y,z)="<<"("<<p.getX()<<","<<p.getY()<<","<<p.getZ()<<")"<<endl;
  73. cout<<"(x, y, z)="<<"("<<p.getXDel()<<", "<<p.getYDel()<<", "<<p.getZ()<<")"<<endl;
  74.  
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
(x, y, z)=(7.5, 8.2)
(x, y, z)=(6.2, 7.5, 8)