How to store data from a class into a container?

I'm having trouble understanding on how to actually store into a container from another class. Can anybody show me a simple example.
What kind of data? What kind of container?

The reference section of this web site contains lots of examples:
http://www.cplusplus.com/reference/stl/

My very crude attempt. If you don't like it. Just comment on what is better. I'm kind of a beginner with only knowledge of beginner stuff.

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class first
{
    public:
    int number;
};

class second
{
    public:
    int number2;
};

int main(int nNumberofArgs, char* pszArgs[])
{
    first First;
    second Second;

    First.number = 10;
    Second.number2 = 20;

    int container[16] = {0};
    int container2[16] = {0};
    cout << "Number: " << First.number << endl;
    cout << "Number2: " << Second.number2 << endl;
    cout << "Container: " << container[0] << endl;
    cout << "Container2: " << container2[0] << endl;
    cout << "Now transferring..." << endl;

    container[0] = First.number;
    container2[0] = Second.number2;

    cout << "Number: " << First.number << endl;
    cout << "Number2: " << Second.number2 << endl;
    cout << "Container: " << container[0] << endl;
    cout << "Container2: " << container2[0] << endl;

    system("PAUSE");
    return 0;
}
Its a container that stores objects from another class called item.
char or int?
int but I think it may be string not really sure?
I hope your drugs are cheap - Where do you buy them?

Can anybody explain me the main question at least?
Simplifying the question it becomes "how to store into a container from another class" and that makes no sense.
this is the container class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "item.h"
#include<iostream>
using namespace std;



class Container
{
private:
	string items[100];
	int counter;
public:

	void add_item(); /// Item New
	int size();
	void show();
	void remove(); // int id 
	Container();   ////// constructor 


};

#endif 


and this is my item class and i want to store into the container class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;

class Item
{
public:
	Item();
	Item( int item_id, string desc, double price );
	int getId();
	string getDescription();
	double getPrice();
	string toString();

private:
	int _item_id;
	string _desc;
	double _price;
};

#endif 



and this is the item class methods

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
include "item.h"

/**************************************************
* Constructors
***************************************************/
Item::Item()
{
	_item_id = 0;
	_price = 0.0;
}

Item::Item( int item_id, string desc, double price )
{
	_item_id = item_id;
	_desc = desc;
	_price = price;
}

/**************************************************
* Accessors
***************************************************/
int Item::getId() { return _item_id; }
string Item::getDescription() { return _desc; }
double Item::getPrice() { return _price; }

/**************************************************
* Convert to string for display (e.g. "(81729) Frozen Peas   $5.63")
***************************************************/
string Item::toString()
{
	// more efficient and flexible than using + to concatenate strings
	ostringstream oss;

	// acts like printing to cout, but stores in a string stream
	oss << "(" << _item_id << ") " << _desc << "\t$" << setprecision(2) << _price;

	// which can then convert to a string
	return oss.str();
}


right know im working on the container methods to store item objects from the item class.
What about storing it as raw data, or using templates, or using std::vector.
I'm not quite there yet , maybe in a couple of days ill get into vecotrs and pointers.
Topic archived. No new replies allowed.