Question

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 type. This method takes in as a parameter another MyPoint.
Implement the class. Write a test program that creates two points (0, 0) and (10, 30.5) and displays the distance between them.

(Extend MyPoint) The MyPoint class was created to model a point in a two-dimensional space. The MyPoint class has the properties x and y that represent x- and y-coordinates, two get functions for x and y, and the function for returning the distance between two points. Create a class named ThreeDPoint 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 that are passed in as parameters.
A constant get function that returns the z value.
A constant distance(const MyPoint&) function to return the distance between this point and the other point in the three-dimensional space.
Turn in a MyPoint.h, MyPoint.cpp, ThreeDPoint.h, ThreeDPoint.cpp and a pointtest.cpp that has your main.Create 2 MyPoints of differing values and demonstrate all of the methods.Create 2 ThreeDPoints of differing values and demonstrate all of the methods.

Page about distance between two points (2D)

Page about distance between two points (3D)

In the case of a 2D point you can assume a z value of 0.

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

//MyPoint.h

#pragma once
#include<math.h>
using namespace std;
class MyPoint
{
private:
   double m_dX;
   double m_dY;
public:
   MyPoint():m_dX(0),m_dY(0)
   {
   }
   MyPoint(double x, double y):m_dX(x),m_dY(y)
   {
   }
   const double getX()
   {
       return m_dX;
   }
   const double getY()
   {
       return m_dY;
   }
   double GetDistance(const MyPoint& other)
   {
       return sqrt(pow((m_dX - other.m_dX), 2) + pow((m_dY - other.m_dY), 2));
   }
};

//ThreeDPoint.h

#pragma once
#include "MyPoint.h"
class ThreeDPoint :
   public MyPoint
{
private:
   double m_dZ;
public:
   ThreeDPoint():m_dZ(0),MyPoint(0,0)
   {
          
   }
   ThreeDPoint(double x, double y,double z):m_dZ(z),MyPoint(x,y)
   {

   }
   const double getZ()
   {
       return m_dZ;
   }
   double GetDistance(ThreeDPoint& other)
   {
       return sqrt(pow((getX() - other.getX()), 2) + pow((getY() - other.getY()), 2) + +pow((m_dZ - other.m_dZ), 2));
   }
};

//Main program

#include <iostream>
using namespace std;
#include "MyPoint.h"
#include "ThreeDPoint.h"
int main()
{

   MyPoint p1(0, 0);
   MyPoint p2(10, 30.5);

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

   ThreeDPoint p3(0, 0, 0);
   ThreeDPoint p4(10, 20, 30);
   cout << p4.GetDistance(p3)<<endl;

}

//Output

Add a comment
Know the answer?
Add Answer to:
Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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...

  • 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...

  • 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...

  • Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in thr...

    Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...

  • 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...

  • 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...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1) Two double data members named width and height which specifies the width and height of the rectangle . (2) A no-arg constructor that creates a rectangle with width 1 and height 1. (3) A constructor that creates a rectangle with the specified width and height . (4) A function named getArea() that returns the area of this rectangle . (5) A function named getPerimeter()...

  • C++: vectors. Euclidean vectors are sets of values which represent values in a dimensional field. A...

    C++: vectors. Euclidean vectors are sets of values which represent values in a dimensional field. A 2d vector would represent values in x,y space (an ordered pair of coordinates) and a 3d vector would represent values in x,y,z space (an ordered triplet of coordinates). We define the basic definition of the 2d vector as follows: class Vector2D { public: Vector2D (); Vector2D (double ,double ); double dotProduct(Vector2D& ); friend Vector2D& operator +(Vector2D&, Vector2D&); friend std::ostream& operator <<(std::ostream& o, Vector2D& a);...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

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