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

CSC 326 Data Structures (Review classes, file separation, initialization list, p

ID: 3871197 • Letter: C

Question

CSC 326 Data Structures (Review classes, file separation, initialization list, pointer this, non-member functions) Hw2 1. Declare a class Rectangle (rectangle.h file) with four private data members: x and y of type int (center of rectangle) and width and height of double type. Include the following member functions: one default constructor without parameters, one constructor initializer (use initialization list in implementation file) four get functions (use pointer this in implementation file one set function with four parameters ((use pointer this in implementation file) function area function distance (from O (0,0) to the center of the rectangle) function distanceR between the centers of two rectangles function print (use pointer this in implementation file) For all member functions use minimum number of parameters and follow the requirements in red when writing definitions in the implementation (cpp) file. Keep the function names as they are given above. Do not use friend functions. (25 points) Write implementation file (rectangle.cpp). Include the definitions of the above member functions. 2. (25 points) 3. Declare and defing the following non-member functions function areaR, function distanceR (from O (0, 0) to the center of the rectangle), function distanceRR between the centers of two rectangles. 4. In main) and declare two pointers named a and b to class Rectangle; Rectangle objects; (obj1, obj2 and dynamically created array). Use objects and pointers to implement all member and non-member functions. (25 points) · Create two objects (objl and obj2) ofclass Rectangle (use two different co structors) Declare a pointer d to and use it to dynamically allocate a memory for an array of two . Use the pointers a, b and d and implement set function with them for all of the objects (25 points)

Explanation / Answer

1)

/*rectangle.h***/

#ifndef RECTANGLE_H

#define RECTANGLE_H

class rectangle

{

private:

int x;

int y;

double width;

double height;

public:

rectangle();

rectangle(int x, int y, double width, double height);

int * getx( );

int *gety();

double * getwidth();

double * getheight();

void set(int *x, int *y, double *width, double *height);

double area();

double distance();

double distanceR(class rectangle r1,class rectangle r2);

print();

};

#endif

2)

/* rectangle.cpp******/

#include "rectangle.h"

rectangle::rectangle(){ }

rectangle::rectangle(int a, int b, double w, double h)

{

x=a;

y=b;

width=w;

height=h;

}

int * getx( )

{

return &x;

}

int * gety()

{

return &y;

}

double * getwidth()

{

return &width;

}

double * getheight()

{

return &height;

}

void rectangle::set(int *a, int *b, double *w, double *)

{

x=*a;

y=*b;

width=*w;

height=*h;

}

double rectangle::area()

{

return width*height;

}

double rectangle::distance()

{

return sqrt(x*x+y*y);

}

double rectangle::distanceR(class rectangle r1,class rectangle r2)

{

return sqrt((r1.x-r2.x)*(r1.x-r2.x)+(r1.y-r2.y)*(r1.y-r2.y));

}

rectangle::print()

{

/* put code for print*/

/* you have not given the requiements of print function */

}

3)

double areaR(class rectangle r1)

{

return r1.width*r1.height;

}

double distanceR(class rectangle r1)

{

return sqrt(r1.x*r1.x+r1.y*r1.y);

}

double distanceRR(class rectangle r1,class rectangle r2)

{

return sqrt((r1.x-r2.x)*(r1.x-r2.x)+(r1.y-r2.y)*(r1.y-r2.y));

}