Question

Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...

Create a MyPoint class to model a point in a two-dimensional space.

The MyPoint class contains:

• Two data fields x and y that represent the coordinates.

• A no-arg constructor that creates a point (0,0).

• A constructor that constructs a point with specified coordinates.

• Two get functions for data fields x and y, respectively.

• Two set functions for data fields x and y, respectively.

• A function named distance that returns the distance from this point to another point of the MyPoint type.

Create ThreeDPoint class to model a point in a three-dimensional space.

Let ThreeDPoint be derived from MyPoint with the following additional features:

• A data field named z that represents the z-coordinate.

• A no-arg constructor that constructs a point with coordinates (0,0,0).

• A constructor that constructs a point with three specified coordinates.

• A get function that returns the z value. • A set function that that sets the z value.

• Override the distance function to return the distance between two points in the threedimensional space.

Put both of your classes in a file called point.cpp.

the code will be tested with code similar to below.

#include "point.cpp"

int main()

{

MyPoint p1(1, 2);

MyPoint p2(4, 2.5);

cout << p1.distance(p2) << endl; p1.setX(1);

p1.setY(1); p2.setX(2); p2.setY(2);

cout << p1.distance(p2) << endl;

ThreeDPoint p3(2,3,1);

ThreeDPoint p4(8,-5,0);

cout << p3.distance(p4) << endl; return 0;

}  

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

#include <iostream>
#include <cmath>

using namespace std;


class MyPoint
{
private:
int x, y;
public:
MyPoint()
{
x=0;
y=0;
}
MyPoint(int xx, int yy)
{
x = xx;
y = yy;
}
void setX(int xx)
{
x = xx;
}
int getX()
{
return x;
}
void setY(int yy)
{
y = yy;
}
int getY()
{
return y;
}
double distance(MyPoint p)
{
return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
};

class ThreeDPoint : public MyPoint
{
private:
int z;
public:

ThreeDPoint()
{
}
ThreeDPoint(int xx, int yy, int zz)
{
setX(xx);
setY(yy);
z = zz;
}

void setZ(int zz)
{
z = zz;
}
int getZ()
{
return z;
}
double distance(ThreeDPoint p)
{
return sqrt((getX()-p.getX())*(getX()-p.getX())+(getY()-p.getY())*(getY()-p.getY())+(z-p.z)*(z-p.z));
}


};

int main()

{

MyPoint p1(1, 2);
MyPoint p2(4, 2.5);
cout << p1.distance(p2) << endl;
p1.setX(1);
p1.setY(1);
p2.setX(2);
p2.setY(2);
cout << p1.distance(p2) << endl;
ThreeDPoint p3(2,3,1);
ThreeDPoint p4(8,-5,0);
cout << p3.distance(p4) << endl;
return 0;

}

OUTPUT :

DACodingICODE BLOCKSPointbin,Debug\Point.exe 1.41421 10.0499 Process returned e (exe) execution time 0.042s Press any key t

Add a comment
Know the answer?
Add Answer to:
Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with...

    Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with x-and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point (0, 0). A constructor that constructs a point with specified coordinates using passed in arguments. Two get functions for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint...

  • Design a class named MyPoint to represent a point with x and y-coordinates. The class contains:...

    Design a class named MyPoint to represent a point with x and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. . A no-arg constructor that creates a point (0, 0) .A constructor that constructs a point with specified coordinates. Two get functions for data fields x and y, respectively. A function named distance that returns the distance from this point to another point of the MyPoint type Write a test program that creates two...

  • Calculates the distance between two points of N dimensional space. If the two points are in...

    Calculates the distance between two points of N dimensional space. If the two points are in different dimensions, print the distance as -1. Use Euclid Distance and Manhattan Distance. Thanks! **Use the code below and complete the rest.** public class Distance2 { public static void main(String[] args) { Point p1 = new Point(new double[] {1.0, 2.0, 3.0}); Point p2 = new Point(new double[] {4.0, 5.0, 6.0}); System.out.println("Euclidean Distance: " + EuclidDistance.getDist(p1, p2)); System.out.println("Manhattan Distance: " + ManhattanDistance.getDist(p1, p2)); Point p3...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...

    NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. The class contains: - Two private data fields x and y that represent the coordinates with get methods. - A constructor that constructs a point with specified coordinates, with default point (0, 0). - A method named distance that returns the distance from this point to another point of the Point type. - A method named isNearBy(p1) that returns...

  • In Python - Design a class named Time. The class contains: -Data fields hour, minute, and...

    In Python - Design a class named Time. The class contains: -Data fields hour, minute, and second that represent a time. -A no-arg constructor that creates a Time object for the current time.(the values of the data fields will respresent the current time.) -A constructor that constructs a Time object with a specified hour, minute, and second, respectively. -A constructor that constructs a Time object with a specified elapsed time since midnight, Jan 1, 1970, in milliseconds. (The values of...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of...

    Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of coordinates (1; 2). Print out the coordinates of the two points on the same line, by calling toString on the two points. Print the result of applying the method equals on point p1, using p2 as argument. Set the x coordinate of p2 equal to the x coordinate of p1, using the methods setX and getX. Set the y coordinate of p2 equal to...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • Implement the Point class (code for this up to the isHigher method was discussed in the...

    Implement the Point class (code for this up to the isHigher method was discussed in the lecture). The class has the following instance variables: • x coordinate (an int) • y coordinate (an int) and the following methods: • Constructor that sets the x and y coordinates • Get and set methods • Method equals if this point is equal to another point object (if the x and y coordinates are the same). • Method isHigher if this point is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT