Making an array bigger? Resizing?

As part of class project I'm working with the following code...
Item.h
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
#ifndef ITEMH
#define ITEMH

#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 


container.h..
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
#ifndef CONTAINER_H
#define CONTAINER_H
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "item.h"

using namespace std;

class Container
{
public: 
	Container(int size, int newsize);
	~Container();
	void add_item(Item item);
	void numberofitems();
	void displayitems();
	void removeitems(int position);
	void resize();
private:
	Item *_items;
	int _count;
};
#endif 


container.cpp ...
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 "container.h"
// 

Container::Container(int size, int newsize)
{
	_items= new Item[size];
	//new_items= new int[newsize];
	_count=0;
}
void Container::add_item(Item item)
{
	
		Resize();
	_items[_count]= item;
	_count++;
}
void Container::displayitems()
{
	for (int i=0; i < _count; i++)
	{
		 cout<<_items[i].toString()<<endl;
	}
}
void Container::removeitems(int id)
{
	for (int i=0; i < _count; i++)
	{
		if 	(id==_items[i].getId())
		{
		_items[i] = _items[i+1];
		_count--;
		}
	}
}
void Container::numberofitems()
{
 cout<<_count<<endl;
}
Container::~Container()
{
	delete [] _items;
}
void resize()
{
}


and my main.cpp ...
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
#include <iostream>
using namespace std;

#include "item.h"
#include "container.h"

int main()
{
	Container c(2);
	
	// some items 
	Item item1( 1000, "Bread", 0.99 );
	Item item2( 2000, "Cheese", 2.99 );
	Item item3( 3000, "Stick", 4.99 );

	c.add_item(item1);
	c.add_item(item2);
	c.add_item(item3);

	c.removeitems(2000);

	c.displayitems();

	system("Pause");

	c.~Container();

	return 0;
}


The plan is to add a new method 'resize' where a new array will copy the contents of the first original array and modify the already existing 'add' method to call 'resize' when necessary.

Up till now, in the constructor I created the new array, what do you guys think? good idea? should I give the array a default value?
You should allow it to be created with a default ctor, giving it a default size. Also, what is the purpose of newsize in your constructor? I see you commented out the line that used it, but was your logic behind it at all?

So yes, create a method that allocates a new chunk of memory and copies your old array into this new space. Or moves with std::move.
Well I was thinking of using 'newsize' in order to have a dynamic size array. What I'm trying to say is that the new array should be able to change depending on the number of items added, to my understanding, it should not have a default size.


And I'm suppose to use a copy function, since we haven't learned anything about 'move'.


That 'newsize' should be used in the resize method in order to create a dynamic size array
Last edited on
As I know, one of disadvantages of using an array is the size. The array has unchanged size. Why you don't switch to vector?
Last edited on
I think OP is trying to write his own vector. Unless I'm mistaken?
If this is what you're trying to do then you should provide a default ctor that creates an array of default size. Then just have a reallocate method that allocates capacity * 2 blocks, then copy the old array over to this new space.
update on my code. Any comments will be very much appreciated!
container.h file
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
#ifndef CONTAINER_H
#define CONTAINER_H
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "item.h"

using namespace std;

class Container
{
public: 
	Container(int size, int newsize);
	~Container();
	void add_item(Item item);
	void numberofitems();
	void displayitems();
	void removeitems(int position);
	void resize();
private:
	int size;
	int newsize;
	Item *_items;
	Item *new_items;
	int _count;
	
};
#endif 


container.cpp
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
#include "container.h"
// Put methods in the header file for template method

Container::Container(int size, int newsize)
{

	_items= new Item[size];
    new_items= new Item[newsize];

	_count=0;
}
void Container::add_item(Item item)
{
	if (_items!=NULL)
	if (newsize == size){
		resize();
	_items[_count]= item;
	_count++;
	}}
void Container::displayitems()
{
	for (int i=0; i < _count; i++)
	{
		 cout<<_items[i].toString()<<endl;
	}
}
void Container::removeitems(int id)
{
	for (int i=0; i < _count; i++)
	{
		if 	(id==_items[i].getId())
		{
		_items[i] = _items[i+1];
		_count--;
		}
	}
}
void Container::numberofitems()
{
 cout<<_count<<endl;
}
Container::~Container()
{
	delete [] _items;
}
void Container::resize()
{
	Item *temp = new Item[size + newsize];
	for (int i=0; i<size;i++)
	{
		temp[i] = _items[i];
	}
	delete [] _items;
	_items = temp;
	size= size + newsize;
}
Topic archived. No new replies allowed.