7. Given the class declaration class Point { public: Point (int=0, int=0); // in
ID: 3641038 • Letter: 7
Question
7. Given the class declarationclass Point {
public:
Point (int=0, int=0); // initializes xCoord and yCoord
void setPoint (int, int); // assigns values to xCoord and yCoord
int getX( ) const; // returns value in xCoord
int getY( ) const; // returns value in yCoord
private:
int xCoord; //X coordinate of the point
int yCoord; //Y coordinate of the point
};
Line -- constructor that accepts 4 integers in its parameter list representing two sets of X/Y coordinates
startingPoint -- returns the point object marking the beginning of the line.
endingPoint -- returns the point object marking the end of the line.
setStart -- accepts a point object that represents the new starting point of the Line object
setEnd -- accepts a point object that represents the new ending point of the Line object.
longest -- compares two Line objects and returns the Line object that is longest.
length -- returns the length of the line.
(b) Write complete function definitions for Line, longest (assume length has already been coded) , startingPoint and setStart.
Explanation / Answer
Please rate...
#include<iostream>
#include<cmath>
#include<valarray>
using namespace std;
class Point {
public:
Point ()
{
} // initializes xCoord and yCoord
Point (int x, int y)
{
xCoord=x;
yCoord=y;
} // assigns values to xCoord and yCoord
int getX( )
{
return xCoord;
} // returns value in xCoord
int getY( )
{
return yCoord;
} // returns value in yCoord
private:
int xCoord; //X coordinate of the point
int yCoord; //Y coordinate of the point
};
class Line {
private:
Point a,b;
public:
Line()
{
}
Line (Point c, Point d)
{
a=c;
b=d;
}
Point StartingPoint()
{
return a;
}
Point EndingPoint()
{
return b;
}
void setStart(Point na)
{
a=na;
}
void setEnd(Point nb)
{
b=nb;
}
float length(Line l)
{
float d;
int x,xx,y,yy;
x=(l.StartingPoint().getX()-l.EndingPoint().getX());
xx=x*x;
y=(l.StartingPoint().getY()-l.EndingPoint().getY());
yy=y*y;
d=sqrt(xx+yy);
return d;
}
Line longest(Line l1,Line l2)
{
if(length(l1)>length(l2))return l1;
else return l2;
}
};
int main()
{
Line l,lo;
Point s1(0,0);
Point e1(3,4);
Point s2(2,3);
Point e2(4,6);
Line l1(s1,e1);
Line l2(s2,e2);
cout<<"Length of line1 "<<l.length(l1);
cout<<" Length of line2 "<<l.length(l2);
lo=l.longest(l1,l2);
cout<<" The longest among these is "<<l.length(lo);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.