creating multiple objects and storing them in a list

Pages: 12
Let's say in a class called 'coordinates', I store two variables x and y. Is there a way I can make another variable in another class of 'coordinate' type so I could then store the vertices (multiple x & y coordinates) of the shape in a list? Would this just be creating objects of coordinate called points and storing them in a list?


Last edited on
1
2
3
4
5
6
7
8
9
 class Coordinate
{
  int x,y;
};

class octagon
{
  vector<Coordinate> lots_of_coordinates;
};
Last edited on
Would you be able to give example please. Let's say I want to store (30,40) and (12,32)?
Coordinate c;
c.x = 30;
c.y = 40;
lots_of_coordinates.push_back(c);
c.x = 12;
c.y = 32;
lots_of_coordinates.push_back(c);
cout << lots_of_coordinates[0].x << " " << lots_of_coordinates[0].y;


if int x and y are private, then would I be able to do the same but use a setter function instead?

so:
1
2
3
4
5
Coordinate c;
c.set(30,40);

lots_of_coordinates.push_back(c); 
//is that right?  


Also how would I be able to use the push_back function if the vector is in another class (base class)?
Last edited on
You would, but with something so simple as a coordinate class that is doing no more than holding two numbers, it feels unnecessary to make them private.

If the vector is held inside another class, such as octagon:

1
2
octagon a_new_octagon;
a_new_octagon.lots_of_coordinates.push_back(c);


Allowing for private/public access. If you make lots_of_coordinates private, you'll need a function to pass the coordinates in.

Although a well-formed octagon shouldn't be able to exist without 8 coordinates already inside it, so really the octagon constructor should insist on being given eight coordinates to begin with.
Thanks, all seems to work fine now however if int x and y are private, how would I access them as
 
lots_of_coordinates[0].x


is inaccessible? I tried using getter functions but it doesn't seem to work.
This, as a public function of octagon
1
2
3
4
Coordinate octagon::get_copy_of_coordinate(int index) const
{
  return lots_of_coordinates[index];
};
Last edited on
Thanks for that.

Sorry if this seems basic but what value would be returning when declaring the function get_copy_of_coordinate(int index) const in the header file?





it returns a 'coordinate' object, as written.
you could make one for get x and get y individually if you prefer, returning int or double or whatever x and y are.
Last edited on
thanks. I'll need to make an overload operator for the point class.

Would it be something like this:

1
2
3
4
5
6
7
ostream& operator<<(ostream& out, const Point& points)
{
	out << points.x;
	out << points.y;
	return out;
}

Last edited on
So I've managed to use the function
1
2
3
4
Coordinate octagon::get_copy_of_coordinate(int index) const
{
  return lots_of_coordinates[index];
};


but this returns both x and y (eg, (30,40) as 3040. How do I separate them? I've tried doing
1
2
3
4
Coordinate octagon::get_copy_of_coordinate(int index) const
{
  return lots_of_coordinates[index].x;
};


but it says x is inaccessible
but this returns both x and y (eg, (30,40) as 3040.

No it doesn't. It returns an object of type Coordinate. That's all.

What you're describing is simply a problem with the code you've written to display the values of a Coordinate object. Show us that code, and we can see where the problem is.
eg. (30,40)

Here's my overload operator
1
2
3
4
5
6
ostream& operator<<(ostream& out, const Point& points)
{
	out << points.x <<  " ";
	out << points.y;
	return out;
}


Function to return elements from the points vector:
1
2
3
4
Point Octagon::get_copy_of_coordinate(int index) const
{
	return points[index];
}


So when calling cout << get_copy_of_coordinate(0); from the Octagon class, 30 40 of type coordinate is called.

I need a way to return just x or y of type coordinate separately. I've tried
1
2
3
4
Point Octagon::get_copy_of_coordinate(int index) const
{
	return points[index].x; //but x is inaccessible in the octagon class
}


Last edited on
So when calling cout << get_copy_of_coordinate(0); from the Octagon class, 30 40 of type coordinate is called.

Your language is very muddled and vague here. That's making it harder for us to understand what you're really saying, and I suspect it's making it harder for you to think clearly about what your code is doing and what the problem might be.

A function is called.

A function returns a value. Hence, a value is returned by a function. That value can be an object.

So:

An object of type Point is returned by get_copy_of_coordinate().
"30 40" is what is displayed, when you stream the results to standard output.

If you need to access the values of x and y independently, you can add "getter" methods to your Point class.

However, if the only reason you need to do that is for displaying the values in a nicer format, why not simply rewrite your >> operator to do that?
Last edited on
I've created getter methods to the Points class

1
2
3
4
5
6
7
8
9
10
11
int Point::getX()
{
	return x;
}

int Point::getY()
{
	return y;
	
}


but I'm unsure on how to access the x and y values independently from the octagon class?

I'll need to access x and y independently as I need to pass them though other functions.
I'm unsure on how to access the x and y values independently from the octagon class?

Just call those methods on the Point object that is returned by get_copy_of_coordinate() .

I'll need to access x and y independently as I need to pass them though other functions.

You could consider, as an alternative, simply passing the Point object through to those other functions, and having those functions call the getter functions to get the values.
I've tried calling point.getX(get_copy_of_coordinate(0)); with 'point' being the object of Point class but this doesn't work. Could you please clarify on how to call the values independently?
Any ideas?
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
#include <iostream>
#include <vector>

using namespace std;

class Coordinate
{
  int x,y;
public:
  Coordinate(int input_x, int input_y) : x(input_x), y(input_y){}
  int getX() {return x;}
  int getY() {return y;}
};

class octagon
{
  vector<Coordinate> lots_of_points;
public:
  void addCoord(Coordinate coord) {lots_of_points.push_back(coord);}
  Coordinate getCoord(int index){return lots_of_points[index];}
};

int main()
{
    octagon oct;
    
    Coordinate some_point(1,2);
    oct.addCoord(some_point);
    Coordinate another_point(50,400);
    oct.addCoord(another_point);
    
    cout << oct.getCoord(0).getX() << ", " <<  oct.getCoord(0).getY() << endl;
    cout << oct.getCoord(1).getX() << ", " <<  oct.getCoord(1).getY() << endl;
}
Last edited on
Pages: 12