Complete the code where it says to add code #include <iostream> #include <math.h
ID: 3865466 • Letter: C
Question
Complete the code where it says to add code
#include <iostream>
#include <math.h> // for sqrt()
using namespace std;
class TwoD
{
protected:
double x, y; // x and y coordinates
public:
// inline implementation of constructor
TwoD(){x = y = 0.0;} // default constructor
TwoD(double i, double j):x(i), y(j){}
// 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;}
-=--:----F1 Exercise.cpp Top L1 (C++/l Abbrev)---------------------------
Exercise.cpp has auto save data; consider M-x recover-this-file
File Edit Options Buffers Tools C++ Help
// the TwoD class.
// YOUR CODE GOES HERE
// --->ADD CODE HERE<---:
// Create an inline constructor that initializes the 3D point
// and reuses the TwoD class.
// YOUR CODE GOES HERE
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:
}
// --->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
}
Explanation / Answer
#include <iostream>
#include <cmath> // for sqrt()
using namespace std;
class TwoD
{
protected:
double x, y; // x and y coordinates
public:
// inline implementation of constructor
TwoD(){x = y = 0.0;} // default constructor
TwoD(double i, double j):x(i), y(j){}
// 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;}
}; // End TwoD class.
class ThreeD : public TwoD {
// YOUR CODE GOES HERE
protected:
double z;
public:
// inline implementation of constructor
ThreeD(){z = 0.0;} // default constructor
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
{
double pointX = point.getX();
double pointY = point.getY();
double pointZ = point.getZ();
double distance = sqrt (pow(pointX - getX(),2) + pow(pointY - getY(),2) + pow(pointZ - getZ(),2));
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()
{
double p1x,p1y,p1z,p2x,p2y,p2z;
ThreeD p1, p2;
cout << "Enter x, y and z of point 1: ";
cin >>p1x >> p1y >> p1z;
p1.setX(p1x);
p1.setY(p1y);
p1.setZ(p1z);
cout << "Enter x, y and z of point 2: ";
cin >>p2x >> p2y >> p2z;
p2.setX(p2x);
p2.setY(p2y);
p2.setZ(p2z);
double distance = p1.getDistance(p2);
cout << "Distance = " << distance << endl;
}
Let me know if you like the code!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.