04-10-2003, 11:46 AM
04-10-2003, 04:50 PM
I dunno :o in C you can make an array of structures, but I don't know about classes. Probably yes.
04-10-2003, 08:05 PM
I dunno in C, but in java, you can do this:
And then you can use everything that is in MyClass 52 times.
And
Code:
class MyClass {
int a;
int b;
// some more
}
public static void main () {
MyClass a[] = new MyClass[52];
public void init() {
for (int i=0;i<52;i++)
a[i] = new MyClass();
}
}
And
04-10-2003, 08:09 PM
because when I do as in the example code I get some
error I don't remember....
ALSO: When I tried to compile (make) allegroGL I got some strange
error (error number 5) do anyone know what this is?
Some of the example programs got compiled by make but
the strange thing is that the allegroGL polygon stuff
was way slower than normal polygon routines (doe sthis
have to do with my crappy integrated graphics card?)
error I don't remember....
ALSO: When I tried to compile (make) allegroGL I got some strange
error (error number 5) do anyone know what this is?
Some of the example programs got compiled by make but
the strange thing is that the allegroGL polygon stuff
was way slower than normal polygon routines (doe sthis
have to do with my crappy integrated graphics card?)
04-10-2003, 10:40 PM
You can't create an 'array of classes' as a class is just a blueprint for objects. However, if you want to call a member function on an object in an array, some example code:
Code:
#include <iostream.h>
//Define a class..
class Cat
{
public:
int getAge() const { return itsAge; }
void setAge(int age) { itsAge = age; }
private:
int itsAge;
}
int main ()
{
Cat Kitties[52]; // Create an array of Cats
// Some test code..
// function call with parameter
Kitties[24].setAge(5);
// and heres one without a parameter
cout << "Kitty number 24 is " << Kitties[24].getAge() << " years old";
}
04-11-2003, 12:22 AM
Uh? I got a little confused there because the code looks like
it does what I want (creates an Cat array (so cat's is standard huh?
the examples in my book does also use cats) and calls a member
function of "one" of the cats)
and that was what I "asked for" but you say I can't create a class
array...
it does what I want (creates an Cat array (so cat's is standard huh?
the examples in my book does also use cats) and calls a member
function of "one" of the cats)
and that was what I "asked for" but you say I can't create a class
array...
04-11-2003, 04:51 AM
Dunno but my book always used cats too
It's not a class array, it's an object array. There is a subtle difference; the class is only defined once in your program - think of it as a blueprint on how to make cats. From this blueprint you can create any number of cat objects. eg.
This creates 53 new cat objects.
This sets the age of Kitty number 24 only. It doesn't set the age of all cats, and it doesn't change anything about class Cat at all. We're just dealing with 1 object.
So the code in my previous post, there is 1 Cat class which is used to create an array of 53 Cat objects. An array of Cat classes doesn't exist and wouldn't make sense.
Still confused? :???:
But basically, the code does do what you really meant to ask
It's not a class array, it's an object array. There is a subtle difference; the class is only defined once in your program - think of it as a blueprint on how to make cats. From this blueprint you can create any number of cat objects. eg.
Code:
Cat Kitties[52];
This creates 53 new cat objects.
Code:
Kitties[24].setAge(4);
So the code in my previous post, there is 1 Cat class which is used to create an array of 53 Cat objects. An array of Cat classes doesn't exist and wouldn't make sense.
Still confused? :???:
But basically, the code does do what you really meant to ask
04-15-2003, 07:35 AM
classes are templates for objects. if objects are of the same class, they have the same fields and the same functions
you can create an array of objects, for instance, drawing on Piptol's code (Cat is already defined):
Cat catarray[10];
for(int i=0;i<10;i++)
{
catarray[i]= new Cat(/*put args to constructor here */);
catarray[i].setAge(3); //all cats will be 3 years old'
}
catarray[7].getAge();
for (int i=0;i<10;i++)
cout<<catarray[i].getAge();
hopefully that helps
you can create an array of objects, for instance, drawing on Piptol's code (Cat is already defined):
Cat catarray[10];
for(int i=0;i<10;i++)
{
catarray[i]= new Cat(/*put args to constructor here */);
catarray[i].setAge(3); //all cats will be 3 years old'
}
catarray[7].getAge();
for (int i=0;i<10;i++)
cout<<catarray[i].getAge();
hopefully that helps