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

1.2 Class PointXY class PointXY { public: PointXY() { x = 0; y = 0; } void set(d

ID: 3918399 • Letter: 1

Question

1.2 Class PointXY

class PointXY {

public:

PointXY() { x = 0; y = 0; }

void set(double a, double b) { x = a; y = b; }

double getx() const { return x; }

double gety() const { return y; }

private:

double x, y;

};

This question uses the class PointXY defined in Sec. 1.2.

• Declare a class Polygon as follows. •

The data in class Polygon is an integer num and a pointer PointXY *pxy, both private.

class Polygon {

public: Polygon(int n); // write code

// copy constructor // write code

// assignment operator // write code

// destructor // write code

(return type) get_point(int n) const; // write code

void set_point(const PointXY &p, int n); // write code

int get_num() const { return num; } // nothing to do, code is given to you

private:

int num;

PointXY *pxy;

};

• Write a non-default constructor with an input int n.

1. If n > 0, set num=n and dynamically allocate pxy to an array of length num.

2. Else if n ? 0, set num = 0 and pxy = NULL.

• Write a copy constructor, assignment operator and destructor to perform appropriate copies and management of dynamic memory.

• Write a method get point(int n).

1. Return the address of pxy[n] if the value of n is valid.

2. Else return NULL.

3. The method is const.

4. Write the function value to have the correct type.

• Write a method set point(const PointXY &p, int n).

1. Set pxy[n] = p if the value of n is valid.

2. Else do nothing.

Explanation / Answer

class PointXY { public: PointXY() { x = 0; y = 0; } void set(double a, double b) { x = a; y = b; } double getx() const { return x; } double gety() const { return y; } private: double x, y; }; class Polygon { public: Polygon(int n) { if(n > 0) { num = n; pxy = new PointXY[n]; } else { num = 0; pxy = NULL; } } ~Polygon() { if(pxy != NULL) { delete pxy; } } // copy constructor // write code Polygon(const Polygon &p) { num = p.num; pxy = p.pxy; } // assignment operator // write code // destructor // write code PointXY * get_point(int n) const { if(n >= num) { return NULL; } return &pxy[n]; } void set_point(const PointXY &p, int n) { if(n
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