Array of Class Objects?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 26
Reputation: Rearden is an unknown quantity at this point 
Solved Threads: 0
Rearden's Avatar
Rearden Rearden is offline Offline
Light Poster

Array of Class Objects?

 
0
  #1
Jul 20th, 2005
Hello. I'm learning C++ out of Sams Teach Yourself C++ in 24 Hours and Accelerated C++ Practical Programming by Example. I've heard these are both good references. Anyway, I just learned about classes and I was wondering how one makes an array of class objects. here is the Cat class I made (no, not exactly the same as the one from the book, but just about :mrgreen: . . .

  1. class Cat
  2. {
  3. private:
  4. int itsage;
  5. std::string itsname;
  6. public:
  7. void setage(int age);
  8. int getage();
  9. void meow();
  10. void setname(std::string name);
  11. std::string getname();
  12. };
  13.  
  14. //setage sets the cat's age
  15. void Cat::setage(int age)
  16. {
  17. itsage = age;
  18. }
  19.  
  20. //returns the cat's age
  21. int Cat::getage()
  22. {
  23. return itsage;
  24. }
  25.  
  26.  
  27. //meow has the cat meow
  28. void Cat::meow()
  29. {
  30. std::cout << "Meow\n";
  31. }
  32.  
  33. //setname sets the cat's name
  34. void Cat::setname(std::string name)
  35. {
  36. itsname = name;
  37. }
  38.  
  39. //getname returns the cat's name
  40. std::string Cat::getname()
  41. {
  42. return itsname;
  43. }

I think that I have the right method for initializing the array. I am doing Cat pack [8];

however. I think I'm doing it wrong when trying to call a Cat member function to a Cat object in the array. I tried pack.at(0).setage(4); which turned up a lot of errors.

Is this something that can only be accomplished using pointers? I haven't learned enough about pointers yet to do that. If it requires a pointer, I understand and will wait until I learn more, i'd just like to know. Thanks in advance.

-Matt
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Array of Class Objects?

 
0
  #2
Jul 20th, 2005
try this.... pack[0].setage(4);

arrays start out at 0 and go up... if you declared pack[8] the elements go from 0-7. and when you use arrays you have to include the [?] where you use it at with ? being the position you want to use.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Array of Class Objects?

 
0
  #3
Jul 20th, 2005
The pack.at(0) behavior is of standard library vectors, not arrays.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC