Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

l Diagram the sequence of a typical selection process.

Answered: 1 week ago

Question

Introduction in the ethical cost allocation

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago