Question: This is in C++ program. I'm getting 3.31662, but the right answer is 3.74166 #include #include // for sqrt() using namespace std; class TwoD {
This is in C++ program. I'm getting 3.31662, but the right answer is 3.74166
#include
class TwoD { protected: double x, y; // x and y coordinates public: // inline implementation of constructor TwoD(){x = y = 0.0; cout << "TwoD default constructor" << endl;} // default constructor TwoD(double i, double j):x(i), y(j){cout << "TwoD consctructor with two arguments" << endl;} // inline implementation of member functions void setX(double NewX){x = NewX;} void setY(double NewY){y = NewY;} double getX() const {return x;} double getY() const {return y;} // get distance of 2D points double getDistance (const TwoD& point) const; };
// calculate the distance of two 2D points double TwoD::getDistance(const TwoD& point) const { double point1[2]; double point2[2]; double dx, dy; double distance;
point1[0] = x; point1[1] = y;
point2[0] = point.getX(); point2[1] = point.getY();
dx = point2[0] - point1[0]; dy = point2[1] - point1[1];
distance = sqrt(dx * dx + dy * dy); return distance; } class ThreeD:public TwoD { private: double z;
public: // --->ADD CODE HERE<---: // Create a default inline constructor that reuses the constructor of // the TwoD class. // YOUR CODE GOES HERE
ThreeD()/*:TwoD()*/{z=0;} // --->ADD CODE HERE<---: // Create an inline constructor that initializes the 3D point // and reuses the TwoD class. // YOUR CODE GOES HERE ThreeD(double i, double j, double k)/*:TwoD(i,j)*/{z = k;} void setZ(double NewZ){z = NewZ;} double getZ() const {return z;} // get distance for two 3D points double getDistance(const ThreeD& point) const; };
// --->ADD CODE HERE<---: // Overload the definition of getDistance() of TwoD class so that it // can calculate the distance between two 3D points double ThreeD::getDistance(const ThreeD& point) const { // YOUR CODE GOES HERE: double point1[3]; double point2[3]; double dx,dy,dz; double distance;
point1[0] = x; point1[1] = y; point1[2] = z;
point2[0] = point.getX(); point2[1] = point.getY(); point2[2] = point.getZ();
dx = point2[0] - point1[0]; dy = point2[1] - point1[1]; dz = point2[2] - point1[2];
distance = sqrt(dx * dx + dy * dy + dz * dz); return distance; }
// --->ADD CODE HERE<---: // Implement a main() function. // You should ask the user for the xyz coordinates of two 3D points, // and then calculate and print out the distance between these two points. int main() { // YOUR CODE GOES HERE int x,y,z; ThreeD obj1; cout << "Enter the xyz coordinates of the first point:"; cin >> x >> y >> z; obj1.setX(x); obj1.setY(y); obj1.setZ(z);
cout << "Enter the xyz coordinates of the second point:"; cin >> x >> y >> z; ThreeD obj2(x,y,z);
cout << "The distance between the two points is:" << obj1.getDistance(obj2) << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
