Question: Define the class Point 3 D that is derived from Point 2 D as defined in the following UML Point 3 D - z: double

Define the class Point 3D that is derived from Point2D as defined in the following UML
Point 3D
- z: double
+Point3D()
+ Point3D(x1: double, y1: double, z: double)
+ distanceToOrigin(): double
+ distanceToPoint(p2: const Point3D&) : double
+ friend operator +(P1: const Point3D&, P2: const Point3D&): Point 3D
+ friend operator <<(os: ostream&, P1: const Point3D&) : ostream&
+ friend operator >>(is: istream&, P1: const Point3D&) : istream&
Write a testing program to use both classes
here is the point 2 class
#include
#include // For mathematical operations
class Point2D {
public:
// Default constructor: Initializes x and y to 0
Point2D() : x(0), y(0){}
// Constructor with parameters: Initializes x and y with provided values
Point2D(double x1, double y1) : x(x1), y(y1){}
// Copy constructor: Creates a deep copy of another Point2D object
Point2D(const Point2D& P1) : x(P1.x), y(P1.y){}
// Setters: Allow modification of x and y coordinates
void setX(double x1){ x = x1; }
void setY(double y1){ y = y1; }
// Getters: Provide access to x and y coordinates (const ensures no modification)
double getX() const { return x; }
double getY() const { return y; }
// Calculates the distance from the origin (0,0)
double getDistanceToOrigin() const {
return sqrt(std::pow(x,2)+ std::pow(y,2));
}
// Calculates the angle (in radians) from the positive x-axis
double getAngle() const {
return atan2(y, x); // Use atan2 for correct quadrant handling
}
// Shifts the point by a specified amount (dx and dy)
void shift(double dx, double dy){
x += dx;
y += dy;
}
// Calculates the distance between two Point2D objects
double distanceToPoint(const Point2D& P2) const {
double dx = x - P2.x;
double dy = y - P2.y;
return sqrt(std::pow(dx,2)+ std::pow(dy,2));
}
// Overloaded addition operator (+): Adds two Point2D objects and returns a new Point2D
friend Point2D operator+(const Point2D& P1, const Point2D& P2){
return Point2D(P1.x + P2.x, P1.y + P2.y);
}
// Overloaded insertion operator (<<): Prints the Point2D coordinates to an ostream (like cout)
friend ostream& operator<<(ostream& os, const Point2D& P1){
os <<"("<< P1.x <<","<< P1.y <<")";
return os;
}
// Overloaded extraction operator (>>): Reads Point2D coordinates from an istream (like cin)
friend istream& operator>>(istream& is, Point2D& P1){
is >> P1.x >> P1.y;
return is;
}
private:
double x; // x-coordinate
double y; // y-coordinate
};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!