Class Object Help

closed account (365X92yv)
I'm trying to write this program and I'm having a hard time calling the add points function and do what the comments tell me I need to do. All the code in main is correct as of right now but I cant figure out how to pass the two objects into the add points function. The rest is much simpler and shouldn't need any help with. Thanks.

Edit: I just went back and there's probably a lot more stuff here that I need help with. If there are any ways on here to fix or clean up what I have, let me know.

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;
}
Last edited on
The comments are not your I presume?
1
2
3
4
5
6
7
class point
{
public:
	// data members go here
	double x;
	double y;
};

So you have to declare to public data members or what? If it's not obligatory consider declaring them private and using functions to access them (accessors).

If you use the same approach (i.e. public data members)
65 cout << px.x << " " << pt.y;

1
2
71	// declare an array of 10 points
72	point pts[length];

Why do you choose to declare it at global scope? If not obligatory consider defining locally (before the use of this in the array)
const int length = 10;

1
2
// set the points equal to (0, 0), (1, 10), (2, 20), etc...
	//  the 10th point should be (9, 90) 

Define a constructor in your class to use:
1
2
3
4
5
6
class point{
public:
point(double X, double Y)
:x(X),y(Y) //meaning initialize x with X and y with Y
{}
....

Now use a loop to define every element of your array. In your loop as your instruction says use the function add_points you made.

Anyway hope you got the point and continue for here...
Topic archived. No new replies allowed.