Question
I have previously posted a question in chegg which is as follows Create an overloaded function that computes the distance between 2, 3, 4 or
I have previously posted a question in chegg which is as follows
Create an overloaded function that computes the distance between 2, 3, 4 or 5 points
class XYPoint {
public:
int x;
int y;
XYPoint(int xc, int yc) : x(xc), y(yc) {}
};
float distance(XYPoint p1, XYPoint p2);
...
float distance(XYPoint p1, ...XYPoint p5);
distance is computes as two-d distance between points and added : distance{p1-p2} + distance{p2-p3} + distance{p3-p4}
This program is to be written in c++ using different a file for header, a file for implementation and other file for main.
Should be compiled in g++ in terminal in an ubuntu system
The expert answer given to this was:
#ifndef XYPOINT_H #define XYPOINT_H
class XYPoint { public: XYPoint(int xc, int yc); float distance(XYPoint p1, XYPoint p2); float distance(XYPoint p1,XYPoint p2,XYPoint p3,XYPoint p4, XYPoint p5); private: int x; int y; };
#endif
==========================================================
#include "XYPoint.h" #include
XYPoint::XYPoint(int xc, int yc) :x(xc), y(yc){} float XYPoint::distance(XYPoint p1, XYPoint p2){ double squareSum = pow(p1.x - p2.x,2) + pow(p1.y - p2.y, 2); double d = sqrt(squareSum); return d; } float XYPoint::distance(XYPoint p1,XYPoint p2,XYPoint p3,XYPoint p4, XYPoint p5){ return distance(p1,p2) + distance(p2,p3) + distance(p3,p4) + distance(p4,p5); }
==========================================================
#include
using namespace std;
int main(){ XYPoint p(0,0); XYPoint p1 (1,1); XYPoint p2 (2,2); XYPoint p3 (3,3); XYPoint p4 (4,4); XYPoint p5 (5,5); double distance = p.distance(p1,p2,p3,p4,p5); cout<<"p1 p2 distance: " <
I need the g++ commands using which this was compiled. Because when i am trying to compile the same, it is throwing a lot of errors
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started