C++ Class Object Collection

This is a discussion on C++ Class Object Collection within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a simple class object: Code: #include <iostream> //include iostream header #include <string> //include string header enum gender ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    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

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    C++ has it's own for_each, along with several containers that do what you require. Here's a cutdown example of your class using a std::vector -

    Code:
    #include <iostream> //include iostream header
    #include <string> //include string header
    #include <vector>
    #include <algorithm>
    
    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
    	
    public:
    	Person(); //contructor
    	~Person(); //destructor //member functions
    	void SetLastName(string s_LastName); //function for setting last name
    	void SetFirstName(string);
    	
    	friend ostream& operator << (ostream& os,const Person& p)
    	{
    		return os << p.m_FirstName << ' ' << p.m_LastName << " ,etc" << '\n'; 
    	}
    };
    
    Person::Person(void) //Person Constructor
    {
    	//notify of construction
    	cout << "Constructing Person Object." << endl;
    }; 
    
    Person::~Person(void) //Person Destructor
    {
    	//notify of desruction
    	cout << "Destructing Person Object." << endl;
    }; 
    
    
    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;
    }; 
    
    template <class T>
    class print
    {
    public:
    	ostream& os;
    	print(ostream& _os):os(_os){}
    	void operator ()(T& type)
    	{
    		os<< type;
    	}
    };
    	
    
    int main()
    {
    	Person me;
    	me.SetFirstName("Mike");
    	me.SetLastName("Tyson");
    	
    	Person you;
    	you.SetFirstName("Lennox");
    	you.SetLastName("Lewis");
    	
    	vector<Person> peeps;
    	peeps.push_back(me);
    	peeps.push_back(you);
    	//output to file by creating ofstream object rather
    	//than using cout
    	for_each(peeps.begin(),peeps.end(),print<Person>(cout));
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    This doesn't have anything to do with your question but I thought I would share anyway. On my getXXX and setXXX I try to do inline because it gets alot cleaner like so

    Code:
    void Person::SetLastName(string s_LastName) m_LastName = s_LastName; 
    void Person::SetFirstName(string s_FirstName) m_FirstName = s_FirstName;

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Thank You

    Just what I needed.

    Thank you very much Sorensen.

    Set VBDeveloper = New VisualCDeveloper

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21