Array of Objects in Class

sorry. new here. i have found similar cases but i can't decipher what i need and don't need so i'll start with very basic information. we are making queue data structures to hold vehicle objects. working with several files but will condense to two for simplicity.

**************************************************

Vehicle.h

1
2
3
4
5
6
7
8
class Vehicle{
protected:
	char type;
public:
	Vehicle(){
		type = 'c';
	}
}


**************************************************

Queue.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Queue
{
friend class Vehicle; // this doesn't really seem to help much
private:
	int front;
	int back;
	int count;
	Vehicle queue[10]; // part i am having trouble with
public:
	Queue();
	~Queue();
	bool is_Empty(void);
	bool is_Full(void);
	//the char returns/parameters were for testing
	void Enqueue(char);
	char Dequeue(void);
	char Front(void);
	char Back(void);
};


**************************************************

Would you mind helping me along in a simplistic manner? Thank you very much.
Last edited on
And the question is?
(+1 for code tags tho, and welcome)
Last edited on
Make sure vehicle class is defined before queue class. Not sure what the bottom 4 methods should do. So you want your queue methods to access your vehicles private members?
Last edited on
if(!reader->tldr()){//humor?
the question was suppose to be "how to create an array of objects in another class?" but after combining them into the same file, it works fine.

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
class Vehicle{
private:
	char type = 'c';
	int no;
public:
	Vehicle(){
		no = 0;
	}
	Vehicle(int value){
		no = value;
	}
	int getNo(void){
		return no;
	}
};

class Queue
{
private:
	int front;
	int back;
	int count;
	Vehicle *queue[ARR_SIZE];
public:
	Queue();
	Queue(int, int, int);
	~Queue();
	bool is_Empty(void);
	bool is_Full(void);
	void Enqueue(int);
	int Dequeue(void);
	int Front(void);
	int Back(void);
};


so now i suppose the question would have been "how do you create an array of objects in a class where the objects are defined in another file?"

}//end humor

but by testing and ripping apart old code, i came up with this (marked below, in comments, are references for two topics):

*****************************************

Vehicle.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Vehicle{
private:
	int no;
public:
	Vehicle(){
		no = 0;
	}
	Vehicle(int value){
		no = value;
	}
	int getNo(void){
		return no;
	}
};


*****************************************

Queue.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define ARR_SIZE 10
#include "Vehicle.h"

class Vehicle; //Topic 1

class Queue
{
private:
	int front;
	int back;
	int count;
	Vehicle *queue[ARR_SIZE]; //Topic 2
public:
	Queue();
	Queue(int, int, int);
	~Queue();
	bool is_Empty(void);
	bool is_Full(void);
	void Enqueue(int);
	int Dequeue(void);
	int Front(void);
	int Back(void);
};


Topic 1: I believe this is the main reason my initial code wasn't working. even though i had included the Vehicle class header file. i didn't initialize it in the Queue class header file. (excuse any incorrect terminology) I am not 100% sure what initializing it does here.

Topic 2: not really quite sure how making it a pointer to an array affects the code. i got the idea from a post on this website:

http://www.cplusplus.com/forum/general/52820/

now the code works. all the functions work as i would like them to, but based on those two topics above, i have no clue why it works lol. would anyone mind talking to me about them? below is the functions for the Queue. i don't think it is really necessary but in case anyone asks to see them in relation to Topic 2 (mind you not a functional queue yet; just getting the array to work):

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
Queue::Queue(){
	front = 0;
	back = 0;
	count = 0;
	for (int i = 0; i < ARR_SIZE; i++)
		queue[i] = NULL;
}

Queue::~Queue(){
	front = 0;
	back = 0;
	count = 0;
	while(!this->is_Empty())
		cout << this->Dequeue() << endl;
}

bool Queue::is_Empty(void){
	if (count == 0)
		return true;
	else
		return false;
}

bool Queue::is_Full(void){
	if (count == ARR_SIZE)
		return true;
	else
		return false;
}

void Queue::Enqueue(int value){
	queue[back] = new Vehicle(value);
	count++;
	back++;
}

int Queue::Dequeue(void){
	if (count != 0){
		int tmp = Front();
		delete queue[front];
		count--;
		front--;
		return tmp;
	}
	else
		return 0;
}

int Queue::Front(void){
	return queue[front]->getNo();
}

int Queue::Back(void){
	return queue[back-1]->getNo();
}


Last edited on
On Topic 1, the line:

class Vehicle; //Topic 1

does nothing. You've already defined the Vehicle class in Vehicle.h, which is included in Queue.h, so that forward declaration has no effect. You can remove it.

On Topic 2:

not really quite sure how making it a pointer to an array affects the code.


You haven't made it a pointer to the array. You've made it an array of pointers. In the line:

Vehicle *queue[ARR_SIZE]; //Topic 2

Vehicle * gives the type of the elements of the array, and ARR_SIZE gives the number of elements in the array.
Last edited on
Topic archived. No new replies allowed.