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

Dani AI

Generated

Short answer: you can absolutely have an array of class objects without using pointers. As hinted, use the array indexing operator (square brackets) to reach elements, and as pointed out, the .at() form is a member of container classes (like std::vector), not raw arrays. Arrays are zero‑based, so the first element is index 0.

A common pitfall is trying to make an array of a type that has no default constructor. Each array element must be default-constructed when the array is created. Example of what will fail to compile:

class Widget {
public:
    Widget(int);   // no default constructor
};

Widget items[3];   // error: no default constructor available

If you need dynamic size or you want bounds-checked access, prefer the standard containers. std::vector gives dynamic sizing and at() throws std::out_of_range on invalid access; std::array (C++11) gives a safer fixed-size wrapper. Example pattern:

#include <vector>

std::vector<Cat> cats(8);    // creates 8 default-constructed Cats
auto &first = cats.at(0);    // bounds-checked reference to first element

Troubleshooting checklist: make sure you included the right headers (<string>, <vector>, <array>, <iostream>), watch method names and case, read the compiler error (it usually tells whether a constructor is missing), and avoid very large stack arrays (use std::vector or heap allocation for big buffers). If you later add parameterized constructors to Cat, either add a default constructor, construct objects in a container with emplace_back, or use a container initialized with appropriately constructed objects.

Recommended Answers

All 2 Replies

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.

The pack.at(0) behavior is of standard library vectors, not arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.