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

CSC 326 Data Structures Lab 2 (follow the REQUIREMENTS on the second page (Revie

ID: 3871196 • Letter: C

Question

CSC 326 Data Structures Lab 2 (follow the REQUIREMENTS on the second page (Review of classes, file separation, pointer this, initialization list, overloaded functions) I. a) Add the prototype of a new member function called quadrant to the Point class. This method will return the quadrant in which the point is located. Quadrant numbers are shown in Figure I below. Member function quadrant should have a return type different than void. Don't write the definition of the member function quadrant in point.h file. b) Add the prototype of a member function midPoint (....) to the class Point which creates an object of class Point as a midpoint of two objects of class Point. (Use only one formal parameter and determine the return type of function midpoint). Don't write the definition of the member function midPoint (....) in point.h file c) Add the prototypes of two new overloaded member functions distance. One of them determines the distance of point to the origin(0, 0) and the other one determines the distance from one object of class Point to another object of class Point.((The first one does not have formal parameters and the second one has only one formal parameter. For both functions distance the return type is double. Don't write the definition of the member functions distance in point.h file pointh #ifndef POINT H #define POINT H class Point t lI default constructor (creales Point at origin) Pointt lM Create Point using x-y coordinates Point(double xx, double yy: protorype of member function midpoins midPoint( .. Fig. I. Quadrants double getX ); void setxY( double xx, double yy); double getY O I prototype of member function quadrant quadrant(...); llprotope of member function distance to another point distance to Point... void print O double x double y : #endif (For h file-30 points; for implementation cpp file-30 points)

Explanation / Answer

Point.h

#ifndef POINT_H
#define POINT_H
class Point{
public:
    Point();
    Point(double xx, double yy);
    int quadrant();
    double distance();
    double distance(Point p);
    Point midPoint(Point a);
    void print();
    double getX();
    double getY();
    void setXY(double xx, double yy);

private:
    double x;
    double y;
};

Point midPoint(Point a, Point b);
double distanceR(Point p);
double distanceRR(Point a,Point b);
#endif

Point.cpp

#include<iostream>
#include<math.h>
#include "Point.h"

using namespace std;

Point::Point():x(0),y(0){}


Point::Point(double xx, double yy): x(xx), y(yy){}

int Point::quadrant(){
   if (x > 0 && y>0)
      return 1;
   if (x < 0 && y>0)
      return 2;
   if (x < 0 && y<0)
      return 3;
   if (x > 0 && y<0)
      return 4;
}

void Point::setXY(double xx, double yy){
   this->x = xx;
   this->y = yy;
}
double Point::getX(){
   return this->x;
}
double Point::getY(){
   return this->y;
}

double Point::distance(){
    double res = sqrt(x*x + y*y);
    return res;
}

double Point::distance(Point a){
    double res = sqrt((x - a.getX()) * (x - a.getX()) + (y - a.getY()) * (y - a.getY()));
    return res;
}

Point Point::midPoint(Point a){
    Point temp;
    temp.setXY((x + a.getX())/2, (y + a.getY())/2);

    return temp;
}

Point midPoint(Point a, Point b){
    Point temp;
    temp.setXY((b.getX() + a.getX())/2, (b.getY() + a.getY())/2);

    return temp;
   
}

double distanceRR(Point a, Point b){
    double res = sqrt((b.getX() - a.getX()) * (b.getX() - a.getX()) + (b.getY() - a.getY()) * (b.getY() - a.getY()));
    return res;
}

double distanceR(Point a){
    double res = sqrt(a.getX()*a.getX() + a.getY()*a.getY());
    return res;
}

void Point::print(){
    cout << x << " " << y << endl;
}


int main(){

   Point p1,p2;
   Point *p3,*p4;
   Point *p5;

   p1.setXY(1,2);
   p2.setXY(4,3);
   p3 = new Point(5,6);
   p4 = new Point(6,7);
   p5 = new Point();
   p5->setXY(10.2,5.6);
   p5->print();
   cout << p1.distance() << endl;
   cout << p1.distance(p2) << endl;
   Point p6 = p1.midPoint(p2);
   p6.print();
   Point p7 = midPoint(*p3,*p4);
   p7.print();
   cout << distanceR(*p3) << endl;
   cout << distanceRR(*p3,*p4) << endl;
   cout << p1.quadrant() << endl;

   return 0;
}