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. double calculateDistFromOrigin(){
  19. return sqrt(x*x+y*y);
  20. }
  21. };
  22.  
  23.  
  24. class Point3D : private Point{
  25. private:
  26. double z;
  27. public:
  28. Point3D(double x, double y, double z): Point(x, y){
  29. this->z=z;
  30. }
  31. void setVar(double x, double y, double z){
  32. Point::setVar(x,y); ////
  33. this->z=z;
  34. }
  35.  
  36. double getXDel(){return getX();}
  37. double getYDel(){return getY();}
  38. double getZ(){return z;}
  39. double calculateDistFromOrigin(){
  40. return sqrt(getX()*getX()+getY()*getY()+getZ()*getZ());
  41. }
  42. };
  43.  
  44.  
  45. int main(){
  46. Point3D p(1.2, 2.5, 3);
  47. p.calculateDistFromOrigin();
  48.  
  49. //cout<<"(x,y,z)="<<"("<<p.getX()<<","<<p.getY()<<","<<p.getZ()<<")"<<endl;
  50. cout<<"(x,y,z)="<<"("<<p.getXDel()<<","<<p.getYDel()<<","<<p.getZ()<<")"<<endl;
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
(x,y,z)=(1.2,2.5,3)