Question

Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as a parameter to the Point that we called the method of. You will need to use sqrt(). For example at the end of the following, dist should be equal to 5.0:

Point p1(-1.5, 0.0);
Point p2(1.5, 4.0);
double dist = p1.distanceTo(p2);

Next, write a class called LineSegment that contains two Point-pointers, where each holds the address of a Point object that represents an endpoint of the LineSegment. It should have get and set methods for both fields and a constructor that takes the addresses of two Point objects and passes them to the set methods to initialize the data members. It should also contain a method called length that returns the length of the LineSegment – by using the distanceTo method on its endpoints – and a method called slope that returns the slope of the LineSegment. You don't need to do anything special for vertical lines - division by zero will result in the special value inf, which stands for "infinity". Your program will not be tested with line segments where both endpoints have the same coordinates. The LineSegement class might be used as follows:

Point p1(4.3, 7.52);
Point p2(-17.0, 1.5);
LineSegment ls1(&p1, &p2);
double length = ls1.length();
double slope = ls1.slope();

The functions for the Point class should have the following names:

setXCoord, getXCoord

setYCoord, getYCoord

distanceTo

The functions for the LineSegment class should have the following names:

setEnd1, getEnd1

setEnd2, getEnd2

length

slope

The files must be named: Point.hpp, Point.cpp, LineSegment.hpp and LineSegment.cpp

Point.cpp and LineSegment.hpp should both #include Point.hpp. LineSegment.cpp should #include LineSegment.hpp. The main method you write for testing will also need to include LineSegment.hpp. If you named the file with your main method "geomMain.cpp", then you can compile your program with "g++ Point.cpp LineSegment.cpp geomMain.cpp -o geom".

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The cpp and hpp files are given Below.

//Point.hpp

#ifndef POINT_INCLUDE

#define POINT_INCLUDE

class Point

{

public:

//Member Variables

double XCoord;

double YCoord;

//Constructors

Point();

Point(double,double);

//Function Declarations

void setXCoord(double);

void setYCoord(double);

double getXCoord();

double getYCoord();

double distanceTo(Point);

};

#endif

//Point.cpp

#include "Point.hpp"

#include<iostream>

#include<math.h>

using namespace std;

//Member Function Defintions

Point::Point()

{

this->XCoord=0.0;

this->YCoord=0.0;

}

Point::Point(double x,double y)

{

this->XCoord=x;

this->YCoord=y;

}

void Point::setXCoord(double x)

{

this->XCoord=x;

}

void Point::setYCoord(double y)

{

this->YCoord=y;

}

double Point::getXCoord()

{

return this->XCoord;

}

double Point::getYCoord()

{

return this->YCoord;

}

double Point::distanceTo(Point p2)

{

double ans=sqrt(pow((this->XCoord)-(p2.XCoord),2)+pow((this->YCoord-p2.YCoord),2));

return ans;

}

//LineSegment.hpp

#ifndef LINE_SEGMENT_INCLUDE

#define LINE_SEGMENT_INCLUDE

#include "Point.hpp"

class LineSegment

{

public:

//Members Variables

Point *End1;

Point *End2;

//Constructor

LineSegment(Point*,Point*);

//Member Functions Declaration

void setEnd1(Point *p1);

void setEnd2(Point *p2);

Point* getEnd1();

Point* getEnd2();

double length();

double slope();

};

#endif

//LineSegment.cpp

#include<iostream>

#include "LineSegment.hpp"

using namespace std;

//Member Function Definitions

LineSegment::LineSegment(Point *p1,Point *p2)

{

setEnd1(p1);

setEnd2(p2);

}

void LineSegment::setEnd1(Point *p1)

{

End1=p1;

}

void LineSegment::setEnd2(Point *p2)

{

End2=p2;

}

Point* LineSegment::getEnd1()

{

return End1;

}

Point* LineSegment::getEnd2()

{

return End2;

}

double LineSegment::length()

{

return End1->distanceTo(*End2);

}

double LineSegment::slope()

{

return ((End2->YCoord-End1->YCoord)/(End2->XCoord-End1->XCoord));

}

//geomMain.cpp

#include<iostream>

#include "LineSegment.hpp"

using namespace std;

int main()

{

Point p1(2.0, 3.0);

Point p2(1.0, 8.0);

LineSegment ls1(&p1, &p2);

double length = ls1.length();

double slope = ls1.slope();

cout<<"Length : "<<length<<endl;

cout<<"Slope : "<<slope;

}

Add a comment
Know the answer?
Add Answer to:
Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...

    Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains: • Two data fields x and y that represent the coordinates. • A no-arg constructor that creates a point (0,0). • A constructor that constructs a point with specified coordinates. • Two get functions for data fields x and y, respectively. • Two set functions for data fields x and y, respectively. • A function named distance that returns the distance from this point...

  • . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should...

    . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should have the following features: • A constructor that accepts two double values (x,y) to create a point object. A default constructor. • A void print() method that prints out the points coordinates. • A void set(Point2D p) method that allows you to set the point using the given object of Point2D. • A Point2D midPoint(Point2D p1) method that returns a Point2D object that is...

  • Write a class called Taxicab that has three int fields (data members) to store its current...

    Write a class called Taxicab that has three int fields (data members) to store its current x- and y-coordinates and the total distance it has driven so far (the actual distance driven by the Taxicab, not the Euclidean distance from it's starting point). The class should have a constructor that takes two parameters and uses them to initialize the coordinates, and also initializes the distance traveled to zero. The class should have a default constructor that sets all three fields...

  • You are to write a class called Point – this will represent a geometric point in...

    You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints). Point should have the following: Data:  that hold the x-value and the y-value. They should be ints and must be private. Constructors:  A default constructor that will set the values to (3, -5)  A parameterized constructor that will receive 2 ints (x then y) and set the data to what is...

  • Design a class named MyPoint to represent a point with x and y-coordinates. The class contains:...

    Design a class named MyPoint to represent a point with x and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. . A no-arg constructor that creates a point (0, 0) .A constructor that constructs a point with specified coordinates. Two get functions for data fields x and y, respectively. A function named distance that returns the distance from this point to another point of the MyPoint type Write a test program that creates two...

  • C++, entry level no pointers or vectors. Write a class called Person that has two data...

    C++, entry level no pointers or vectors. Write a class called Person that has two data members - a string variable called name and a double variable called age. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods. Write a separate function (not part of the Person class) called stdDev that takes two parameters...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT