views:

115

answers:

3

I'm new to OOP, so please bear with me if this is a simple question. If I create a class, which has attributes "a", "b", and "c", is it possible for the attributes to be an array, such that attribute a[2] has a meaning?

+10  A: 

Member variables can ofcourse be arrays. Example:

class MyClass {
    int a[3];  // Array containing three ints
    int b;
    int c;
};
Jesper
+5  A: 

Assuming that by "attributes" you mean what C++ refers to as "member variables" (i.e. members of a particular objects):

class MyClass:
public:
    MyClass() {
       a.push_back(3);
       a.push_back(4);
       a.push_back(5);
       cout << a[2] << endl; // should output "5"
    }
private:
    std::vector<int> a;
};
Dominic Rodger
Ok, but a `std::vector` is not an array.
Jesper
@Jesper - fair point. I just figured that was possibly a helpful way to go for someone new to C++.
Dominic Rodger
A: 

Wai wai,

You can create the class. Then have an Array of that class type.

Then in order to fetch the information.

For Java: You can use an array list

which is

ArrayList<"this is where your class name goes"> Array = new ArrayList<"classname">();

Then you can say. ArrayList[0].getAttribute;

kevin
This is a C++ question, not Java.
GMan