Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1363
  • Last Modified:

Creating an array within a class

Hi.

The layout of my program is as follow:
class Person
{
 public:

   string firstName;
   string lastName;
   int social_security_number;
 
void setPersondata(){
}

Void displayPersondata(){

};
int main()
Create a new object of the class Person

}

And here comes the question:
The plan is that i'm going to create a new object of the class Person and this object shall hold many numbers. I think that the best way is to store all of these numbers within an array.

How do i do that?
0
ErikVest
Asked:
ErikVest
  • 2
1 Solution
 
lhl60Commented:


you can use an STL container
see http://www.cplusplus.com/reference/stl/

STL containers can be a little tricky, but read the examples, and you'll get a feel for it.

for instance the vector<> class, so if your number are an int type your code could look like this

#include <vector>
class Person
{
 public:

string firstName;
 string lastName;
 vector<int> numbers;
  int social_security_number;

void addnumber(int num);
 
void setPersondata(){
}

Void displayPersondata(){

};

void Person::addnumber(int num)
{
numbers.push_back(num)
}


int main()
{
Person person;

// add 1000 ints to the vector
for (int i=0;i<1000; i++)
{
person.addnumber(i);
}
}


STL is a C++ standard avaliable  in all C++ compilers

If you prefer to stay on the "Gates side",you can use an MFC collection, only windows then
see:  http://msdn.microsoft.com/en-us/library/942860sh(VS.80).aspx


You must consider what kind of data you want to add, how big etc. should it be int, double or floats maybe even strings.
You can also add class objects to a container, this way you can have different objects in the same container if they all have the same base class


0
 
Infinity08Commented:
You can simply add an array of ints (or floats or whatever you need) to the object members.

Here's a tutorial on arrays :

        http://www.cplusplus.com/doc/tutorial/arrays.html


Btw, the main should probably be outside of the class.
0
 
lhl60Commented:
yes, a compile time defined array is the simplest thing to use,but you must at compile time know how big the array has to be.

like:

#define NUMBEROFNUMBERS 300

int numbers[NUMBEROFNUMBERS];

gives you space for NUMBEROFNUMBERS int's

you also have to keep track of how many elements in the array that is actually used, so you know where to put the next one.
if you don't know how many number you have to put in the array you need a dynamic container
0
 
evilrixSenior Software Engineer (Avast)Commented:
Picking up on the point made by lhl60 on using a vector...

Unless you know in advance roughly how big your vector is likely to get (so you can presize it) or if you need to perform lots of inserts and deletes from the middle of the array, you might want to consider using a std::list instead. Growing a vector and inserting/deleting from the middle are very costly in terms of performance. A list does not incur this overhead. On the other hand, elements in a vector are accessed in constant time, where as the time to access a node in a list is dependent upon the size of the list since the nodes must be traversed. Also, a list uses a little more memory than a vector to store the pointers pointing to the next and previous node.

http://www.cplusplus.com/reference/stl/list/

The point is, the STL provides more than one type of container, each has good and bad points. Pick the correct container that suits your needs. The following link lists the most common STL containers and the time complexity for the most common operations on them all:

http://www.cplusplus.com/reference/stl/
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now