Thread: stl container to store more than 1 type

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    stl container to store more than 1 type

    Hi i got a design issue here.

    I got
    Code:
         class patient{
         }
      
        class dayPatient:public patient{
         }
        
         class outPatient:public patient{
         }
    int main void(){
       // stl container
    }
    i need a stl container to store the day patients and outpatients.any type of container allows it? i tried vector,but vector allows only 1 type of patient.
    Hope someone can tell me how to store both the daypatient and outpatient in a single stl container.

    Thanx

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    88
    If your classes are polymorphic, you can do it like this:
    Code:
    patient* foo = new dayPatient;
    patient* bar = new outPatient;
    std::vector<patient*> patients;
    patients.push_back(foo);
    patients.push_back(bar);
    //etc...

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    note that if you do it this way you should really wrap vector in a class so that patients are deleted in the destructor e.g.
    Code:
    class PatientColl
    {
        std::vector<patient*> m_patients;
    
        static void DeletePatient(patient *pPatient)
        {
            delete pPatient;
        }
    public:
    
        ~PatientColl()
        {
            for_each(m_patients.begin(), m_patients.end(), DeletePatient);
        }
    
        void AddPatient(patient *pPatient)
        {
            m_patients.push_back(pPatient);
        }
    };
    or you could save yourself the trouble and use a boost.PtrContainer which does this for you (see my sig)
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    24
    hey umr student, thanx for your reply.my problem does get partly solved by using pointers.

    Code:
    class patient{
    private:
    public:
    	void print(){
    		cout<<"\nprint method from patient class ";
    	}
    };
    
    class outPatient:public patient{
    public:
    	void print(){
    		cout<<"\n OVER RIDED METHOD  ";
    	}
    	void print2(){
    		cout<<"\nOUT newly defined print method ";
    	}
    };
    
    int main(void){
    	
    	patient *a1=new patient;
    	patient *b1=new outPatient;
    	a1->print(); //works
    	b1->print(); // doesn't output correctly.want it to output "OVER RIDED METHOD"
    	//b1->print2(); // DOESNT compile
    }
    hope someone can help me out to get it to output the right data.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You need to declare your methods as virtual if you want the child class to override the parent class
    Code:
    #include <iostream>
    #include <string>
    using std::cout;
    using std::string;
    using std::endl;
    using std::cin;
    
    class CBaseClass
    {
    public:
    	virtual string GetClassMessage()
    	{
    		return "I am the Base Class";
    	}
    };
    
    class CChildClass : public CBaseClass
    {
    public:
    	virtual string GetClassMessage()
    	{
    		return "I am the Child Class";
    	}
    };
    
    int main()
    {
    	CBaseClass *ptrBase = new CBaseClass();
    	CBaseClass *ptrChild = new CChildClass();
    
    
    	cout<<"Base Message = "<<ptrBase->GetClassMessage()<<endl;
    	cout<<"Child Message = "<<ptrChild->GetClassMessage()<<endl;
    
    	cin.get();
    }
    Woop?

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    24
    thanx..i got 1 error rectified. but i still got one last wierd error left.

    Code:
    class CChildClass : public CBaseClass
    {
    public:
    	virtual string GetClassMessage()
    	{
    		return "I am the Child Class";
    	}
    	
    	string print()
    	{
    		return "HEY,HAVE A GOOD DAY";
    	}
    };
    
    int main()
    {
    	CBaseClass *ptrBase = new CBaseClass();
    	CBaseClass *ptrChild = new CChildClass();
    	
    	cout<<"Base Message = "<<ptrBase->GetClassMessage()<<endl;
    	cout<<"Child Message = "<<ptrChild->GetClassMessage()<<endl;
    	cout<<"Child Message = "<<ptrChild->print()<<endl;
    }
    i added the print method and tried to call.but it doesn't work? whats my mistake?

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can only call methods of the base class from that pointer, if you want to cast that pointer to the child you can then call the child methods.
    Code:
    #include <iostream>
    #include <string>
    using std::cout;
    using std::string;
    using std::endl;
    using std::cin;
    
    class CBaseClass
    {
    public:
    	virtual string GetClassMessage()
    	{
    		return "I am the Base Class";
    	}
    };
    
    class CChildClass : public CBaseClass
    {
    public:
    	virtual string GetClassMessage()
    	{
    		return "I am the Child Class";
    	}
    
    	string Print()
    	{
    		return "Hey, Whats Up";
    	}
    };
    
    int main()
    {
    	CBaseClass *ptrBase = new CBaseClass();
    	CBaseClass *ptrChild = new CChildClass();
    
    
    	cout<<"Base Message = "<<ptrBase->GetClassMessage()<<endl;
    	cout<<"Child Message = "<<ptrChild->GetClassMessage()<<endl;
    
    	CChildClass *ptrRealChild = reinterpret_cast<CChildClass*>(ptrChild);
    
    	cout<<"Child Print Message = "<<ptrRealChild->Print()<<endl;
    
    	cin.get();
    }
    Woop?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    CChildClass *ptrRealChild = reinterpret_cast<CChildClass*>(ptrChild);
    Yikes! Absolutely not!!!

    If you really, really, really, really, really have to cast a base to the child, you use a dynamic_cast. (And you check if the return value is NULL, because it will be if the object is not actually of the child type.)
    But it would be far better to have a properly designed base class where the cast isn't necessary.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  2. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  3. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM