Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please change point 2 d class and point 3 d class ( point 3 d inherits from point 2 d ) so that we can

Please change point 2d class and point 3d class (point 3d inherits from point 2d) so that we can use templates. Please implement a test program. using C++
this is the following code for point 2d class
#include iostream
#include cmath // 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
};
heres code for point 3d
#include
using namespace std;
/*base class*/
class point2D
{
/*data members*/
protected:
double x,y;
public:
/*constructor*/
point2d(double x, double y)
{
this->x=x;
this->y=y;
}
/*setters*/
void setx(double X){x=X;}
void setY(double Y){y=Y}
};
/*inherit point2D*/
class point3D:public point2D
{
/*data members*/
protected:
double z;
public:
/*constructor call base class constructor*/
point3d(double x,double y,double z):point2d(x,y)
{
this->z=Z;
}
/*setters and getters*/
void setZ(double Z){z=Z;}
double getZ(){return z;}
};

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

What is Aufbau's rule explain with example?

Answered: 1 week ago

Question

Design a health and safety policy.

Answered: 1 week ago