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

do (iii) and (iv) (i) This is a simple Point class interface file whose objects

ID: 3789203 • Letter: D

Question

do (iii) and (iv)

(i) This is a simple Point class interface file whose objects represent points in the cartesian plane

#include <iostream>

using namespace std;

class Point

{ public:

    Point()      // default constructor

        Point(double x, double y); // another constructor

        double x() const; // get function, return _x

        double y() const; // get function, return _y

private:

        double _x, _y;

};
(ii) Here is a test driver file for the Point class

#include "Point.h" // defines Point class

#include <iostream>

using namespace std;

int main()

{

    Point p0; // invokes default constructor

        Point p1(5,-2); // invokes constructor

        Point p2 = p1;

        p0 = p1; // invokes assignment operator

        cout << "p0.x() = " << p0.x() << " ";

        cout << "p0.y() = " << p0.y() << " ";

}
(iii) Write the implementation file of Point class

(iv) Add the following member functions to the point class, implement them, and test them in the driver

    double magnitude() const; // returns polar coordinate r = (x^2 + y^2)^(1/2)

   void move(double dx, double dy); // move the point dx in x direction and dy in y direction;

    void print(ostream & out) const ; // print the point in the format (x, y) to ostream out

Explanation / Answer


// Point.h
#include <iostream>
using namespace std;
class Point
{ public:
Point(); // default constructor
Point(double x, double y); // another constructor
double x() const; // get function, return _x
double y() const; // get function, return _y
double magnitude() const; // returns polar coordinate r = (x^2 + y^2)^(1/2)
   void move(double dx, double dy); // move the point dx in x direction and dy in y direction;
   void print(ostream & out) const ; // print the point in the format (x, y) to ostream out
private:
double _x, _y;
};



// C++ implementation Point.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include "Point.h" // defines Point class

Point::Point() // default constructor
{
   _x = 0;
   _y = 0;
}
  
Point::Point(double x, double y) // another constructor
{  
   _x = x;
   _y = y;
}

double Point::x() const
{
   return _x;
}

double Point::y() const
{
   return _y;
}


double Point::magnitude() const // returns polar coordinate r = (x^2 + y^2)^(1/2)
{
   return sqrt(_x*_x + _y*_y);
}

void Point::move(double dx, double dy) // move the point dx in x direction and dy in y direction;
{
   _x+=dx;
   _y+=dy;
}

void Point::print(ostream & out) const // print the point in the format (x, y) to ostream out
{
   out << "(" << _x << "," << _y << ")" << endl;
}


// C++ code main.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include "Point.h" // defines Point class

using namespace std;
int main()
{
Point p0; // invokes default constructor
Point p1(5,-2); // invokes constructor
Point p2 = p1;
p0 = p1; // invokes assignment operator
cout << "p0.x() = " << p0.x() << " ";
cout << "p0.y() = " << p0.y() << " ";
p0.print(cout);
}

/*
output:

p0.x() = 5
p0.y() = -2
(5,-2)


*/