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
2009-10-11 21:10:14
+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
2009-10-11 21:10:56
Ok, but a `std::vector` is not an array.
Jesper
2009-10-11 21:19:08
@Jesper - fair point. I just figured that was possibly a helpful way to go for someone new to C++.
Dominic Rodger
2009-10-11 21:38:45
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
2009-10-11 21:12:54