8 Replies - 1019 Views - Last Post: 15 April 2014 - 11:59 AM Rate Topic: -----

#1 berooo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 14

Array in class without number of elements

Posted 10 April 2014 - 11:51 AM

Is it possible to have array in class without number of elements,
for example:
I have a class called Plane with number of motors, and string array of passengers,
but I don't have a number of passengers, but then again I have it in constructor,
so can i print the list of passengers without having the number as part of the class.

Class Plane
{
   private:
	int motors;  //number of motors
	string *list; // list of passengers

   public:
	Plane();
        Plane(int MT, int NMB, string passengers[]);  
	~Plane();
        void PrintPassengers();
};

Plane :: Plane(){
	motors = 0;
	list = NULL;
}

Plane :: Plane(int MT, int NMB, string passengers[]){
	
	motors = MT;
        int number = NMB;

	list = new string[number];

	for (int i =0; i<number; i++)
		*(list+i)=passengers[i];
}
void Plane :: PrintPassengers()
{
	for (int i=0; i< ? ; i++)
	cout << *(list+i) << endl;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array in class without number of elements

#2 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Array in class without number of elements

Posted 10 April 2014 - 12:27 PM

Yes, you can use std::vector, a sequential container that can grow in size...

Hope it Helps!
Was This Post Helpful? 0
  • +
  • -

#3 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Array in class without number of elements

Posted 10 April 2014 - 12:39 PM

With either arrays or std::vector you need the size to display, though vector is a class and stores the size as a member int, access it with .size();
Was This Post Helpful? 0
  • +
  • -

#4 berooo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 14

Re: Array in class without number of elements

Posted 11 April 2014 - 12:20 PM

This was just a little part of my exam (Object oriented programming in C++). During lectures we always had string array, and the number of elements in string array as private elements of the class, but in exam we only had string array.

Thank you for your answers, you're probably both right, but we didn't use either of those during lectures,
so I will ask my TA on monday what was the right answer, and then I'll post it here.
Was This Post Helpful? 0
  • +
  • -

#5 TriggaMike   User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Array in class without number of elements

Posted 11 April 2014 - 12:56 PM

Since this is an exam question, maybe they were looking for a different approach to solve the problem of indexing in an array without a size?

I think a relatively easy solution would be to use the theory behind Cstring and simply tack on a null terminator after your last point of data, so the code would be like:

Class Plane
/*unaltered code*/

Plane :: Plane(int MT, int NMB, string passengers[]){
	
	motors = MT;
        int number = NMB; // why assign instead of just using NMB

	list = new string[number + 1]; // Increase the size of the array by one

	for (int i =0; i<number; i++) // Left this loop unchanged
		*(list+i)=passengers[i];

        *(list + number) = NULL; //Added code to append a NULL terminator
}
void Plane :: PrintPassengers()
{
	for (int i=0; *(list+i) != NULL ; i++)    //The test is now simply scanning for NULL
	cout << *(list+i) << endl;
}


I'm fairly certain that would have been a reasonable solution to your problem. I'm fairly new to C++ so I'm not 100% sure that this *(list+i) != NULL line will do exactly what I'm looking for, but I'm pretty sure it should.
Was This Post Helpful? 2
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Array in class without number of elements

Posted 11 April 2014 - 06:56 PM

If you used a sentinel value for the last string, then you wouldn't need to bother with pointers that much.

const string SENTINEL("This is the end"); 
//...
list[size] = SENTINEL;
//or
list[size] = "This string is intentionally left blank";


Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6293
  • View blog
  • Posts: 21,632
  • Joined: 05-May 12

Re: Array in class without number of elements

Posted 11 April 2014 - 10:56 PM

Instead of the C-style with an empty last string, or the sentinel end of list marker, you code go Pascal style and store the number of passengers as the first string.

in constructor:
list = new string[NMB + 1];
stringstream str;
str << NMB;
list[0] = str.str();
for(int i = 0; i < NMB; i++)
    list[i+1] = passengers[i];

in print function:
if (list == NULL)
    return;

int NMB;
stringstream str(list[0]);
str >> NMB;
for(int i = 0; i < NMB; i++)
    cout << list[i+1];;


Was This Post Helpful? 2
  • +
  • -

#8 berooo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 14

Re: Array in class without number of elements

Posted 15 April 2014 - 11:51 AM

View PostSkydiver, on 11 April 2014 - 10:56 PM, said:

Instead of the C-style with an empty last string, or the sentinel end of list marker, you code go Pascal style and store the number of passengers as the first string.

in constructor:
list = new string[NMB + 1];
stringstream str;
str << NMB;
list[0] = str.str();
for(int i = 0; i < NMB; i++)
    list[i+1] = passengers[i];

in print function:
if (list == NULL)
    return;

int NMB;
stringstream str(list[0]);
str >> NMB;
for(int i = 0; i < NMB; i++)
    cout << list[i+1];;



I tried your code and it does work. I like the idea, but we never used stringstream.
I haven't seen my TA yet, so I don't know what will his answer be
Was This Post Helpful? 0
  • +
  • -

#9 berooo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 14

Re: Array in class without number of elements

Posted 15 April 2014 - 11:59 AM

View PostTriggaMike, on 11 April 2014 - 12:56 PM, said:

Since this is an exam question, maybe they were looking for a different approach to solve the problem of indexing in an array without a size?

I think a relatively easy solution would be to use the theory behind Cstring and simply tack on a null terminator after your last point of data, so the code would be like:

Class Plane
/*unaltered code*/

Plane :: Plane(int MT, int NMB, string passengers[]){
	
	motors = MT;
        int number = NMB; // why assign instead of just using NMB

	list = new string[number + 1]; // Increase the size of the array by one

	for (int i =0; i<number; i++) // Left this loop unchanged
		*(list+i)=passengers[i];

        *(list + number) = NULL; //Added code to append a NULL terminator
}
void Plane :: PrintPassengers()
{
	for (int i=0; *(list+i) != NULL ; i++)    //The test is now simply scanning for NULL
	cout << *(list+i) << endl;
}


I'm fairly certain that would have been a reasonable solution to your problem. I'm fairly new to C++ so I'm not 100% sure that this *(list+i) != NULL line will do exactly what I'm looking for, but I'm pretty sure it should.


This idea is the most similar to the solutions we usually used in the class,
so I guess this will probably be it.
Thank you.
I haven't seen my TA yet, and when he tells us the right answer
I will also post it here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1