Question: Please answer all questions with C++ code, point.cpp #include class Point { double x; double y; public: Point(double x, double y): x(x), y(y) {} double

Please answer all questions with C++ code,

point.cpp

#include

class Point {

double x;

double y;

public:

Point(double x, double y): x(x), y(y) {}

double get_x() const { return x; }

double get_y() const { return y; }

void set_x(double x) { this->x = x; }

void set_y(double y) { this->y = y; } };

double dist(const Point& pt1, const Point& pt2) {

// Compute distance between pt1 and pt2 and return it

// This is a function stub; add the actual code later

return 0.0; // Just return zero for now

} int main() {

Point p1(2.5, 6), p2(0.0, 0.0);

std::cout << dist(p1, p2) << ' ';

}

2. Given the denition of the geometric Point class above (point.cpp), complete the function nameddistance:

double distance(Point r1, Point r2) {

// Details go here

} that returns the distance between the two points passed as parameters.

3. What is the purpose of a class constructor?

4. May a class constructor be overloaded?

class Point {

public:

double x;

double y;

};

7. Given the denition of the geometric Point class iabove add a method named distance:

class Point {

// Other details omitted

// Returns the distance from this point to the // parameter p double distance(Point p) {

// Details go here

} };

that returns the distance between the point on whose behalf the method is called and the parameter p.

8. Consider the following C++ code:

#include

class IntPoint { public: int x; int y;

IntPoint(int x, int y): x(x), y(y) {}

};

class Rectangle {

IntPoint corner; // Location of the rectangle's lower-left corner

int width; // The rectangle's width

int height; // The rectangle's width public: Rectangle(IntPoint pt, int w, int h):

corner((pt.x < -100) ? -100 : (pt.x > 100 ? 100 : pt.x),

(pt.y < -100) ? -100 : (pt.y > 100 ? 100 : pt.y)),

width((w < 0) ? 0 : w), height((h < 0) ? 0 : h) {} int perimeter() {

return 2*width + 2*height;

} int area() {

return width * height;

} int get_width() {

return width;

} int get_height() {

return height;

} // Returns true if rectangle r overlaps this

// rectangle object. bool intersect(Rectangle r) {

// Details omitted

}

// Returns the length of a diagonal rounded to the nearest

// integer. int diagonal() {

// Details omitted

} // Returns the geometric center of the rectangle with

// the (x,y) coordinates rounded to the nearest integer.

IntPoint center() {

// Details omitted

} bool is_inside(IntPoint pt) {

// Details omitted

}

}; int main() {

Rectangle rect1(IntPoint(2, 3), 5, 7),

rect2(IntPoint(2, 3), 1, 3),

rect3(IntPoint(2, 3), 15, 3),

rect4(IntPoint(2, 3), 5, 3);

std::cout << rect1.get_width() << ' ';

std::cout << rect1.get_height() << ' ';

std::cout << rect2.get_width() << ' ';

std::cout << rect2.get_height() << ' ';

std::cout << rect3.get_width() << ' ';

std::cout << rect3.get_height() << ' ';

std::cout << rect4.get_width() << ' ';

std::cout << rect4.get_height() << ' ';

std::cout << rect1.get_perimeter() << ' ';

std::cout << rect1.get_area() << ' ';

std::cout << rect2.get_perimeter() << ' ';

std::cout << rect2.get_area() << ' ';

std::cout << rect3.get_perimeter() << ' ';

std::cout << rect3.get_area() << ' ';

std::cout << rect4.get_perimeter() << ' ';

std::cout << rect4.get_area() << ' ';

}

(b) With regard to a Rectangle objects lower-left corner, what are the minimum and maximum values allowed for the x coordinate? What are the minimum and maximum values allowed for the y coordinate?

(e) What happens when a client attempts to create a Rectangle object with parameters that are outside the acceptable ranges?

(f) Implement the diagonal method.

(g) Implement the center method.

(i) Implement theis_insidemethod.

(j) Complete the following function namedcorner:

IntPoint corner(Rectangle r) {

// Details go here

};

that returns the lower-left corner of the Rectangle object r passed to it. You may not modify theRectangle class.

9. Develop a Circle class that, like the Rectangle class above, provides methods to compute perimeter and area. The Rectangle instance variables are not appropriate for circles; specically, circles do have corners, and there is no need to specify a width and height. A center point and a radius more naturally describe a circle. Build yourCircleclass appropriately.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!