Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 7 Assume the following class definitions for two classes Point and Line

ID: 3573933 • Letter: Q

Question

Question 7

Assume the following class definitions for two classes Point and Line:

class Point{

public:

   Point();                 // default constructor

   Point (int x, int y);    // non-default constr, initializing the x and y coord

   void move_x (int dist); // move x coord by dist

   void move_y (int dist); // move y coord by dist

   void display() const;    // display x and y coordinates

   int getx() const;        // returns the value of the x coordinate

   int gety() const;        // returns the value of the x coordinate

private:

   int x_coord;

   int y_coord;

};

class Line{

public:

    Line(Point p1, Point p2); // construct a line with the two extreme points

    void move_horiz(int dist); // move line along the x-axis

    void move_vert(int dist); // move line along the y-axis

    void draw() const;         // print the extreme points of the line

    double length() const;     // calculates and returns the length of line

private:

    Point segment[2];          // the two extreme points of the line

};

A) - Write the details of all the member functions for both classes. Note that the length of a line from point (x1,y1) to (x2,y2) is given by:   length = (x2-x1)2 + (y2-y1)2

B) - Write a test main () as follows:

int main(){

   . . . // declare a Point p1 with coordinates (0,0)

   . . . // declare a Point p2 with coordinates (3,4)

   . . . // declare a line line1 with extreme points p1 and p2

   . . . // output the length of line1.              What is the output?

   . . . // move line1 by 10 along the x-axis

          // and by 5 along the y axis.

   . . . // print the extreme points of line 1.      What is the output?

}

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;


class Point{
public:
Point() // default constructor
{
x_coord=0;
y_coord=0;
}
Point (int x, int y) // non-default constr, initializing the x and y coord
{
x_coord=x;
y_coord=y;
}
void move_x (int dist) // move x coord by dist
{
x_coord+=dist;
}
void move_y (int dist) // move y coord by dist
{
y_coord+=dist;
}
void display() const // display x and y coordinates
{
cout<<"x coordinate: "<<x_coord<<" and y coordinate: "<<y_coord<<endl;
}
int getx() const // returns the value of the x coordinate
{
return x_coord;
}
int gety() const // returns the value of the x coordinate
{
return y_coord;
}
private:
int x_coord;
int y_coord;
};

class Line{
public:
Line(Point p1, Point p2) // construct a line with the two extreme points
{
segment[0]=p1;
segment[1]=p2;
}
void move_horiz(int dist) // move line along the x-axis
{
segment[0].move_x(dist);
segment[1].move_x(dist);
}
void move_vert(int dist) // move line along the y-axis
{
segment[0].move_y(dist);
segment[1].move_y(dist);
}
void draw() const // print the extreme points of the line
{
cout<<"point 1: ";
segment[0].display();
cout<<"point 2: ";
segment[1].display();
}
double length() const // calculates and returns the length of line
{
return sqrt(pow((segment[1].getx()-segment[0].getx()),2)+pow((segment[1].gety()-segment[0].gety()),2));
}
private:
Point segment[2]; // the two extreme points of the line
};

int main(){
Point p1(0,0); // declare a Point p1 with coordinates (0,0)
Point p2(3,4); // declare a Point p2 with coordinates (3,4)
Line line1(p1,p2); // declare a line line1 with extreme points p1 and p2
cout<<"length of line1 is "<<line1.length()<<endl; // output the length of line1. What is the output?
line1.move_horiz(10); // move line1 by 10 along the x-axis
line1.move_vert(5); // and by 5 along the y axis.
line1.draw(); // print the extreme points of line 1. What is the output?
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote