Posts: 1,080
Threads: 87
Joined: Feb 2002
how can I create an array of classes and then use a function
in my_class[n] like
Code:
my_class[42].add_number(5);
IN C++
or is it impossible?
/post]
Posts: 6,419
Threads: 74
Joined: Mar 2002
I dunno :o in C you can make an array of structures, but I don't know about classes. Probably yes.
Posts: 1,845
Threads: 44
Joined: Aug 2002
I dunno in C, but in java, you can do this:
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 then you can use everything that is in MyClass 52 times.
And
Posts: 1,080
Threads: 87
Joined: Feb 2002
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?)
/post]
Posts: 354
Threads: 11
Joined: May 2002
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";
}
In a world without walls and doors, who needs Windows and Gates?
Posts: 1,080
Threads: 87
Joined: Feb 2002
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...
/post]
Posts: 354
Threads: 11
Joined: May 2002
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.
Code:
Kitties[24].setAge(4);
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
In a world without walls and doors, who needs Windows and Gates?
Posts: 11
Threads: 0
Joined: Apr 2003
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
url=http://tikitac.topcities.com/]Give me TIKITAC, please![/url]