C++ Class Object Collection
Hi,
I have a simple class object:
Code:
#include <iostream> //include iostream header
#include <string> //include string header
enum gender {Male = 1, Female = 2}; //enumeration of gender type
using namespace std; //use the std namespace from this execution point forwards
class Person //define Person class object
{
protected:
string m_FirstName; //internal first name storage variable
string m_LastName; //internal last name storage variable
int m_Age; //internal age storage variable
gender m_Gender; //internal gender storage variable
public:
Person(); //contructor
~Person(); //destructor
//member functions
string GetFirstName(void) const; //function for getting first name
string GetLastName(void) const; //function for getting last name
int GetAge(void) const; //function for getting age
gender GetGender(void) const; //function for getting gender
void SetFirstName(string s_FirstName); //function for setting first name
void SetLastName(string s_LastName); //function for setting last name
void SetAge(int s_Age); //function for setting age
void SetGender(gender s_Gender); //function for setting gender
};
Person::Person(void) //Person Constructor
{
//notify of construction
cout << "Constructing Person Object." << endl;
return;
};
Person::~Person(void) //Person Destructor
{
//notify of desruction
cout << "Destructing Person Object." << endl;
return;
};
string Person::GetFirstName(void) const //function for getting first name
{
//return internal first name storage variable
return m_FirstName;
};
string Person::GetLastName(void) const //function for getting last name
{
//return internal last name storage variable
return m_LastName;
};
int Person::GetAge(void) const //function for getting age
{
//return internal age storage variable
return m_Age;
};
gender Person::GetGender(void) const //function for getting gender
{
//return internal gender storage variable
return m_Gender;
};
void Person::SetFirstName(string s_FirstName) //function for setting first name
{
m_FirstName = s_FirstName;
return;
};
void Person::SetLastName(string s_LastName) //function for setting last name
{
m_LastName = s_LastName;
return;
};
void Person::SetAge(int s_Age) //function for setting age
{
m_Age = s_Age;
return;
};
void Person::SetGender(gender s_Gender) //function for setting gender
{
m_Gender = s_Gender;
return;
};
If you have programmed with Visual Basic you will probably know that you can create object collections and use the For...Each language construct to enumerate through a VB Object Collection.
In the very same way, I am looking for the "proper" way of doing this in C++.
The above Person object needs to be implemented in a proper collection, so that I can use methods such as Add GetItem and Remove and clear to manage a collection of Person objects as the application is running. Also i need to use a construct similar to the For..Each contruct so then I can output each record to a text file.
Thanks
VD