C++ programming 1.2 Class PointXY • The class PointXY will be used below in ques
ID: 3918723 • Letter: C
Question
C++ programming
1.2 Class PointXY • The class PointXY will be used below in questions. 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
HI please go threw code. I don't know hot to test it. so test by self. and if having any error please reply back to me.
/*
1.2 Class PointXY
• The class PointXY will be used below in questions.
*/
#include <iostream>
#include <cstddef>
using namespace std;
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) {
if(n >0)
{
num = n;
pxy = new PointXY[num];
}
else if (n <= 0)
{
num = 0;
pxy = NULL;
}
}// write code
// copy constructor // write code
Polygon(Polygon & obj)
{
num = obj.num;
if(obj.pxy != NULL)
{
pxy = new PointXY[num];
for(int i =0 ; i<num; i++)
{
pxy[i] = obj.pxy[i];
}
}
}
void operator= (Polygon & obj)
{
num = obj.num;
if(obj.pxy != NULL)
{
pxy = new PointXY[num];
for(int i =0 ; i<num; i++)
{
pxy[i] = obj.pxy[i];
}
}
}
// assignment operator // write code
// destructor // write code
~Polygon()
{
if(num <= 0)
{
/*
for(int i =0; i <num ; i++)
{
delete pxy[i];
}
*/
delete pxy;
}
num = 0;
}
PointXY * get_point(int n) const
{
if(pxy != NULL)
return &pxy[n];
else
return NULL;
}
void set_point(const PointXY &p, int n)
{
if (n > 0)
{
pxy[n] = p;
}
}
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.
*/
int main()
{
// I don't know how to test it but it is compiling properly
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.