Answer to Question #168704 in C++ for Mazbabul Alam

Question #168704

CreateCreate a point class which can specify a point in a two dimensional plane. It should have a no-

arg constructor, which initialize the co-ordinates by 0 and also have a two-arg constructor, 

which initialize the co-ordinates by the appropriate two parameters. It should also have a show( 

) function, which will display the values of the two co-ordinates. Now find the distance between 

two objects of the point class and show its use in the main( ) function. 

[The distance between (x1,y1) to (x2,y2) points is d = √((x2-x1)²+(y2-y1)²) ]


1
Expert's answer
2021-03-03T18:34:54-0500
#include <iostream>
#include <cmath>

using namespace std;

class point {
private:
    double x, y;

public:
    point() {
        x = 0;
        y = 0;
    }

    point(double _x, double _y) {
        x = _x;
        y = _y;
    }

    void show() {
        cout << "Point: (" << x << ", " << y << ")\n";
    }

    double distance(point other) {
        double d = sqrt(pow(other.x - x, 2) + pow(other.y - y, 2));
        return d;
    }
};

int main()
{
    point a;
    point b(3, 4);
    a.show();
    b.show();
    cout << "Distance: " << a.distance(b) << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS