User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,950 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,890 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4181 | Replies: 2
Reply
Join Date: Jul 2005
Posts: 26
Reputation: Rearden is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rearden's Avatar
Rearden Rearden is offline Offline
Light Poster

Array of Class Objects?

  #1  
Jul 19th, 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: . . .

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: West Virginia
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: Array of Class Objects?

  #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  
Join Date: Jun 2005
Location: Troy
Posts: 1,276
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 36
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: Array of Class Objects?

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

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC