Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In c++ 1. Define the class Point2D that represents an ordered pair (x, y) in a rectangular coordinate system. The Point2D class has two private

In c++

1. Define the class Point2D that represents an ordered pair (x, y) in a rectangular coordinate system. The Point2D class has two private data members x(double), y(double). The class must have a default constructor as well as a constructor with parameters. Have a get/set methods for the private data members.

a. Include the function distance () that returns the distance of the point from the origin using the formula sqrt x^2 + y^2.

b. Overload the insertion (<<) operator for the Point2D class such that when applying cout to a point object it displays the ordered pair (x , y), where x is the value of the x component and y is the value of the y component of the object.

c. Overload the operator less than (<) as a nonmember function with the header

bool operator< (Point2D p1, Point2D p2)

the operator returns true if P1s distance from the origin is less that p2s distance from the origin.

d. Test the Point2D class by writing a main function where you declare two Point2D objects using the constructor with parameters.

Use the overloaded operator (<<) to display the points.

Call the distance function and display the distance of the points from the origin.

Finally use the operator < to compare the two objects and display the smaller object.

2. Continue with problem 1

a. Add exception handling to the Point2D class as follows. Validate the values assigned to x and y components of the point in the constructor and the set functions. The x- and y- components of a point must be between -5 and 5 inclusive. So, validate the values assigned to the x and y coordinates if the values are not within this range throw an invalid_argument exception with the message Invalid parameter.

b. Write a main function to test the exception handling. Use a try and catch block in the main, create a Point2D object with proper values then create a Point2D object with at least one improper component value to activate the exception handling.

3. Define the class Point3D that represents an ordered triplet (x, y, z) in a 3 dimensional coordinate system.

a. Class Point3D inherits from class Point2D and has an additional private data member z(double).

b. Define a default constructor as well as a constructor with parameter for the Point3D class.

c. Include a get/set functions for the private data member z.

d. Redefine the function distance() in Point3D that uses the formula sqrt x^2 + y^2 + z^2 to compute the distance of the point from the origin.

e. Test the class Point3D by writing a main function in which you create a Point2D object as well as a Point3D object using the constructor with parameters. Next display their distances from the origin.

f. Demonstrate polymorphic behavior by declaring a Point2D pointer type variable and separately assigning a Point2D object and then a Point3D object to the pointer and calling the distance function using the pointer. Which distance function will be called? What does your code show?

4. Write the function minPoint() that receives a vector of Point2D type and returns the smallest point. Here a Point2D object p1 is considered smaller than Point2D object p2 if its distance from the origin is smaller. Test this function by writing a main function where you declare a vector of at least 5 Point2D objects. First display the distance of the five objects from the origin then call the function minPoint() to find and display the smallest point.

Thank you for any and all help offered!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions

Question

6. What questions would you suggest should be included?

Answered: 1 week ago

Question

5. Who should facilitate the focus group?

Answered: 1 week ago