Question

Must be C++ programming    The MyPoint class was created to model a point in a...

Must be C++ programming
   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.
A constant get function that returns the z value.
A constant distance(const ThreeDPoint&) function to return the distance between this point and the other point in the three-dimensional space.
    Implement the classes. Write a test program that creates two points (0,0,0) and (10,30,25.5) and displays the distance between them.

Homework Answers

Answer #1

Here is the answer for your question in C++ Programming Language.

CODE :

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;
//MyPoint Class
class MyPoint{
   public:
       //Class member variables
       double x;
       double y;
       //Default constructor that sets point(0,0)
       MyPoint(){
           x = 0;
           y = 0;
       }
       //Parameterised constructor that creates a point with specified co-ordinates
       MyPoint(double X,double Y){
           x = X;
           y = Y;
       }
       //Returns x
       double getX(){   return x; }
       //Returns y
       double getY(){ return y;   }
       //Returns ditance between two points
       double distance(const MyPoint& p1) const{
           return sqrt(((p1.x - x)*(p1.x - x)) + ((p1.y - y)*(p1.y - y)));
       }
};
class ThreeDPoint : public MyPoint{
   //Class variable
   double z;
   public:
       //Default constructor that sets point(0,0,0)
       ThreeDPoint(){
       z = 0;          
       }
       //Parameterised constructor that creates a point with specified co-ordinates
       ThreeDPoint(double X, double Y, double Z) : MyPoint(X,Y){
           z = Z;
       }
       //Returns z
       double getZ() const{ return z; }
       //Returns ditance between two points
       double distance(const ThreeDPoint& p1) const{          
           return sqrt(((p1.x - x)*(p1.x - x)) + ((p1.y - y)*(p1.y - y)) + ((p1.z - z)*(p1.z - z)));
       }
      
};
int main(){
  
   //Creates two points p1 and p2
   ThreeDPoint p1;
   ThreeDPoint p2(10,30,25.5);
  
   //Display output
   cout << "First point (" << p1.getX() << ", " << p1.getY() << ", " << p1.getZ() << ") is created." << endl;
   cout << "Second point (" << p2.getX() << ", " << p2.getY() << ", " << p2.getZ() << ") is created." << endl;  
    cout << "Distance between the two points : " << fixed << setprecision(2) << p1.distance(p2) << endl;
   return 0;
}

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

OUTPUT :

Any doubts regarding this can be explained with pleasure :)

Know the answer?
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
Similar Questions
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
To gain coding XP, complete this coding challenge by adding constructors to the Point class you...
To gain coding XP, complete this coding challenge by adding constructors to the Point class you completed for the previous challenge.Point should have three constructors:One that takes no arguments and does nothing (the default constructor).One that takes the x- and y-coordinates for the point to create as arguments. The x- and y-coordinates of a point should be non-negative. If the arguments provided are negative values, use 0 instead as the construction value.A copy constructor
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
The Galactic Federation of Planets (GFP) maintains records on all planets that are applying to become...
The Galactic Federation of Planets (GFP) maintains records on all planets that are applying to become members of the GFP. A planet may make an application to become a member of the Galactic Federation of Planets when their technology has led them to develop Star Gate technology enabling them to travel to other planets in the galaxy outside their own solar system. Your assignment is to create a class that can be used to hold the basic information about a...
K 2. Point Source Interference Pattern Consider a two-dimensional x-y space in which there are two...
K 2. Point Source Interference Pattern Consider a two-dimensional x-y space in which there are two identical and synchronized point sources located at y=±D/2 emitting waves of wavelength λ. The sources are said to constructively interfere at any location if the phase difference between waves received from the two sources at that location differ by an integral multiple of 2π. Equivalently, the distances from the location to the two sources differ by an integral number of wavelengths. Derive the complete...