943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 23031
  • C++ RSS
Jul 20th, 2005
0

Array of Class Objects?

Expand Post »
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: . . .

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Rearden is offline Offline
26 posts
since Jul 2005
Jul 20th, 2005
0

Re: Array of Class Objects?

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Jul 20th, 2005
0

Re: Array of Class Objects?

The pack.at(0) behavior is of standard library vectors, not arrays.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is online now Online
2,479 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Warning Error
Next Thread in C++ Forum Timeline: I'd like to team up with another beginner





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC