1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
// ***********************************************************************************
// Author:
// Class: CSCI/CMPE 1170
// Lab 25: array of objects
// Revised on:
// Comment: The code here is meant to be revised.
//
//------------------------------------------------------------------------------------
//
// This lab exercise is to practice using an array of objects for structured data.
//
// It only uses one file, just to remind you that you can do that too.
//
// ***********************************************************************************
#include <iostream>
#include <string>
using namespace std;
/***********************************************************************************
In this lab you will create a simple class to hold a 2d point. A 2d point has
two numbers, an x coordinate and a y coordinate. We want to be able to print
points like this:
(5.1, 3.4)
where 5.1 is the x and 3.4 is the y.
To add two points together, you add the x values and the y values, so:
(1, 2) + (3, 4) = (4, 6)
To complete this lab you'll need to finish the class definition for point,
finish and/or fix a few functions that work with points, and follow the
directions in main.
***********************************************************************************/
const int length = 10;
// finish this class definition
class point
{
public:
// data members go here
double x;
double y;
};
// finish this function that adds 2 points together
point add_points( point a, point b )
{
// declare local point to hold the answer
point answer;
// add points a and b and store the answer in answer
answer.x = a.x + b.x;
answer.y = a.y + b.y;
// return the answer, just like any other data type
return answer;
}
// fix this function to print a point like (10.2, 13.4)
void print_point( point pt )
{
cout << "Err...";
}
// good old main
int main( )
{
// declare an array of 10 points
point pts[length];
// set the points equal to (0, 0), (1, 10), (2, 20), etc...
// the 10th point should be (9, 90)
// (use a loop, don't set them all individually)
int i;
for(i = 0; i < length; i++)
{
pts[i].x = i;
pts[i].y = i * 10;
}
// set the first point equal to the second point added to the third
// (use the add_points function you finished above)
// use the print_point function you finished above to print all the points in the array (in a loop, of course)
return 0;
}
|