Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- /**
- * A point in 2D space.
- */
- class Point2D {
- public:
- int x, y;
- Point2D::Point2D (int, int);
- /**
- * Calculates and returns the distance between two points (the current Point2D
- * object and a given parameter p).
- */
- double dist2D(Point2D p) {
- return sqrt( pow((p.x - this->x), 2) + pow((p.y - this->y), 2) );
- }
- /**
- * Prints the 2D distance between two points as '2D distance = k' where k is
- * distance d as a ceiling-rounded int on a new line.
- */
- void printDistance(double d) {
- cout << "2D distance = \n" << ceil(d);
- }
- };
- /**
- * A point in 3D space.
- */
- class Point3D: public Point2D {
- public:
- int z;
- Point3D(int, int, int);
- /**
- * Calculates and returns the distance between two points (the current Point3D
- * object and a given parameter p).
- */
- double dist3D(Point3D p) {
- return sqrt( pow((p.x - this->x), 2) + pow((p.y - this->y), 2) + pow((p.z - this->z), 2) );
- }
- /**
- * Prints the 3D distance between two points as '2D distance = k' where k is
- * distance d as a ceiling-rounded int on a new line.
- */
- void printDistance(double d) {
- cout << "3D distance = \n" << ceil(d);
- }
- };
- int main() {
- int x1;
- int y1;
- int z1;
- int x2;
- int y2;
- int z2;
- cin >> x1 >> y1 >> z1;
- cin >> x2 >> y2 >> z2;
- Point3D p1(x1, y1, z1);
- Point3D p2(x2, y2, z2);
- double d2 = p1.dist2D(p2);
- double d3 = p1.dist3D(p2);
- //The code below uses runtime polymorphism to call the overridden printDistance method:
- Point2D p(0, 0);
- p.printDistance(d2);
- p = p1;
- p1.printDistance(d3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement