point class in c 2b 2b

Solutions on MaxInterview for point class in c 2b 2b by the best coders in the world

showing results for - "point class in c 2b 2b"
Emily
23 Aug 2017
1/*
2  Examples of classes, inheritance, overloading, overriding,   
3  static binding, and dynamic binding
4  with classes Point2D and Point3D.
5
6  Not an example of polymorphism.
7*/
8
9#include <iostream>
10#include <cmath>
11
12using namespace std;
13
14/*
15  Base class Point2D represents a point in two dimensional space
16*/
17class Point2D {
18  
19public:
20  
21  /*
22    Static data members have to be initialized when declared.
23    Static data memebers are usually declared const (constant), 
24    because the values should not be changed (Principle of Least Privilege)
25  */
26  static const double ZERO = 0.0;
27  
28  /*
29    Overloaded constructor with zero parameters
30  */
31  Point2D(){
32    //initialize the data members
33    x = ZERO;
34    y = ZERO;
35    //cout<<*this<<endl;
36  }
37  
38  /*
39    Overloaded constructor with two parameters
40  */
41  Point2D(double x2, double y2){
42    //initialize the data members
43    x = x2;
44    y = y2;
45    //cout<<*this<<endl;
46  }
47   
48  //set (mutator) functions
49  void setX(double x2){
50    x = x2;
51  }
52
53  void setY(double y2){
54    y = y2;
55  }
56
57  //get (accessor) functions   
58  double getX(){
59    return x;
60
61  }
62
63  double getY(){
64    return y;
65  }
66
67  /*
68    Returns the distance from point (0,0).
69    Can be the magnitude of a vector or length of a line.
70  */
71  double distance(){
72    return sqrt(x*x + y*y);
73  }
74
75  //friend function for overloading output operator<<()
76  friend ostream & operator<<(ostream & output, const Point2D & point){
77    output<<"("<<point.x<<", "<<point.y<<")";
78    return output;
79  }                    
80
81  //data members represent two points in space
82protected:
83  double x; 
84  double y;      
85};
86
87/*
88  Derived class Point3D represents a podouble in three dimensional space
89*/
90class Point3D:public Point2D {
91  
92public:
93  /*
94    Overloaded constructor with zero parameters
95  */
96  Point3D():Point2D(){
97    //initialize data member
98    z = ZERO;
99    //cout<<*this<<endl;
100  }
101  
102  /*
103    Overloaded constructor with three parameters
104  */
105  Point3D(double x2, double y2, double z2):Point2D(x2,y2){
106    //initialize data member
107    z = z2;
108    //cout<<*this<<endl;
109  } 
110  
111  //set (mutator) functions
112  void setZ(double z2){
113    z = z2;
114  }
115
116  //get (accessor) functions       
117  double getZ(){
118    return z;
119  }
120
121  //overridden function, returns the distance from point (0,0,0)
122  double distance(){
123    return sqrt(x*x + y*y + z*z);
124  }
125
126  //friend function for overloading output operator<<()
127  friend ostream & operator<<(ostream & output, const Point3D & point){
128    output<<"("<<point.x<<", "<<point.y<<", "<<point.z<<")";
129    return output;
130  }          
131
132  /*
133    Data members represent three points in space.
134    Points x and y are inherited from Point2D.
135  */
136protected:
137  double z;                   
138};
139
140//driver function to test classes
141int main(){
142  Point3D point(3, 4, 5);
143  cout<<"point = "<<point<<endl;
144  //function distance() returns 7.07107
145  cout<<point.distance()<<endl;
146  
147  //pointer2D points to point
148  Point2D *pointer2D = &point; 
149  cout<<"*pointer2D = "<<*pointer2D<<endl;
150  //function distance() incorrectly returns 5.0
151  cout<<pointer2D->distance()<<endl;
152  
153  //pointer3D points to point
154  Point3D *pointer3D = &point; 
155  cout<<"*pointer3D = "<<*pointer3D<<endl;
156  //function distance() returns 7.07107
157  cout<<pointer3D->distance()<<endl;
158   
159  return 0;
160}
161
162/*
163point = (3, 4, 5)
1647.07107
165*pointer2D = (3, 4)
1665
167*pointer3D = (3, 4, 5)
1687.07107
169
170*/
171
172
173
174