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();
}
| |