| | |
Array of Class Objects?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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: . . .
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
C++ Syntax (Toggle Plain Text)
class Cat { private: int itsage; std::string itsname; public: void setage(int age); int getage(); void meow(); void setname(std::string name); std::string getname(); }; //setage sets the cat's age void Cat::setage(int age) { itsage = age; } //returns the cat's age int Cat::getage() { return itsage; } //meow has the cat meow void Cat::meow() { std::cout << "Meow\n"; } //setname sets the cat's name void Cat::setname(std::string name) { itsname = name; } //getname returns the cat's name std::string Cat::getname() { return itsname; }
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
- Dynamic class members? (C++)
- How do I delete an instance of an Array? (C++)
- Passing Class Objects, Instances (C++)
- Help : Pointers to array of class objects . (C++)
- Initializing an array as a class mamber (C++)
Other Threads in the C++ Forum
- Previous Thread: Warning Error
- Next Thread: I'd like to team up with another beginner
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






