Qbasicnews.com

Full Version: How can I create... (c++)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
I dunno :o in C you can make an array of structures, but I don't know about classes. Probably yes.
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
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?)
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";

}
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...
Dunno but my book always used cats too Smile

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);
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 Smile
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