Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 09: Point Class in c++ The Point Systems that represent 2D space are very common. They could be creating images that represent physical space,

Lab 09: Point Class in c++

The Point

Systems that represent 2D space are very common. They could be creating images that represent physical space, or just the relationship between two values in a graph. Either way, graphical systems rely on an abstraction of points to determine where to show things on the screen.

In a 2D system, we typically think about a point as having a x coordinate and a y coordinate. Today we'll start work on classes by creating a Point class with some useful functionality.

Class-y

As we dicussed in class, classes are used to define new, composite data types. When you define a class, you're telling the compiler that you want to be able to create variables of that type, and what goes in the. Variables with a class type are called objects.

Classes introduce a lot of new syntax. Use the examples posted from class to help you get the syntax down.

Part I

Define a class called Point whose data members are two ints called x and y.

Add a constructor to the class that takes two int values as parameters and sets the data member values. This should enable this code to work in main:

Point a(5, 6); Point b(-2, 3);

Use the step debugger that I keep going on about to set a breakpoint in main and verify that the objects a and b have the correct values in them after those two lines execute. Take a screenshot showing those variable values in the debugger and include it with your lab.

Part II

Add a Print method to your Point class to enable this code:

Point a(5, 6); Point b(-2, 3); a.Print(); // make it print to the screen: (5,6) b.Print(); // make it print to the screen: (-2,3)

Add a DistanceTo method to your Point class that calculates the distance between two points. Hint: rhymes with bythagorean bheorem.

In this case, you're comparing two Point objects, so you call the method on one Point, and pass in the other, like so:

cout << a.DistanceTo(b) << endl; // should print 7.616...

Part III

Try declare a Point like this:

Point break;

This will error! It complains because you're not providing the constructor with the two int values that it expects. To solve this, you need to add another constructor to your Point class - the default constructor.

The default constructor for a class is named the same as the class, just like every other constructor, and has no return type, just like every other constructor. What makes it unique is that it has no parameters. It's the constructor that is called when you don't have any initial values to specify.

There's still work to do inside the default construtor, though! You should set the class data members (x and y) to sensible default values, like 0.

Side note: if you don't specify *any* constructors for your class, then the compiler is cool assuming a default constructor for you that does nothing. But if you do specify at least one other constructor, then it's on you to provide a default.

Now that you have a default constructor, you can declare an array of five Points. You need the default constructor, because there's no way syntactically to provide all the values for the five points in the array when you declare it. This way, they will all start with 0,0 in them.

Don't forget n!

Once you have your array of Points, set all their x and y values to numbers between 0 and 10. Yeah, you have to do it one value at a time.

Write a function (not a method on the class!) that takes an array of Points (and an integer n) and prints all the points to the screen like this (note the fancy square brackets and commas):

[(5,7), (3,2), (9,2), (1,8), (6,3)]

Do I have to tell you that you should be calling the Print method in your class to do this?

Part IV

Write a function (not a method on the class!) that takes an array of Points (and an integer n) and prints to the screen a graphical depiction of all the points. You can assume that the coordinate values are all between 0 and 10. You cannot assume that the Points are ordered in any useful way. It should look something like this:

. . . . . . . X . . . . . . . . . . . . . . . . . . . . . . . . . . X . . . . . . . . . . . . . . . . . . . . . . . . . . X . X . . . . . . . . . . . . . . . . . . . . . . X . . . . . . . . . . . . .

The most straightforward, brute-force version of this algorithm is m^2*n, where m is the max coordinate value (10 in this example) and n is the number of points. That is, for every point you need to draw, search for it in the array. You can do better by doing some sorting first, or by using extra space.

Part V

Write a function (not a method on the class!) that takes an array of Points (and an integer n) and prints to the screen every pair of Points that are within distance 5.0 of each other. Make sure you test it! Output should look something like:

Point pairs within 5.0 of each other: (0,0) - (1,3) (0,0) - (2,2) (1,3) - (2,2) ...

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

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions