1. Declare a class Rectangle (rectangle.h file) with four private data members:
ID: 3869742 • Letter: 1
Question
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 public 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) 2. Write implementation file (rectangle.cpp). Include the definitions of the above member (25 points) 3. Declare and define 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. (25 points) 4. In main) Create two objects (obj1 and obj2) of class Rectangle (use two different constructors) and declare two pointers named a and b to class Rectangle Declare a pointer d to and use it to dynamically allocate a memory for an array of two Rectangle objects; Use the pointers a, b and d and implement set function with them for all of the objects (objl, obj2 and dynamically created array). . . Use objects and pointers to implement all member and non-member functions (25 points)Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
class Rectangle {
private:
int x;
int y;
double width;
double height;
public:
Rectangle(){
x = 0;
y = 0;
width = 0;
height = 0;
}
Rectangle(int a, int b, double c, double d){
x = a;
y = b;
width = c;
height = d;
}
void set(int a, int b, double c, double d){
x = a;
y = b;
width = c;
height = d;
}
int getX(){
return x;
}
int getY(){
return y;
}
double getWidth(){
return width;
}
int getHeight(){
return height;
}
double area(){
return width * height;
}
double distanceR(){
return sqrt( x*x + y*y);
}
void print(){
cout << x << " " << y << " " << width << " " << height << endl;
}
};
double distanceR(Rectangle a){
return sqrt(a.getX()*a.getX() + a.getY()*a.getY());
}
double areaR(Rectangle a){
return a.getWidth() * a.getHeight();
}
double distanceRR(Rectangle a, Rectangle b){
return sqrt((a.getX() - b.getX())* (a.getX() - b.getX()) + (a.getY() - b.getY())* (a.getY() - b.getY()));
}
int main(){
Rectangle obj1;
Rectangle obj2(4,5,5,6);
Rectangle *a,*b,*d;
d = new Rectangle[2];
a = &obj1;
b= &obj2;
a->set(3,4, 6, 7);
a->print();
d[0].set(3,5, 6, 7);
d[0].print();
d[1].set(3,5, 6, 8);
d[1].print();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.