Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How make array of class? Give me some example...
Posted
Comments
PJ Arends 26-Mar-14 22:28pm    
Use std::vector< myclass>;

You can do on stack or heap using arrays or pointers. Std::vector is excellent option.

C++
#include <iostream>
#include <vector>

using namespace std;

class MyClass
{
public:
	MyClass() {};
	MyClass(const int val) : myVal(val) {};
	int getVal() { return myVal;}; 
private:
	int myVal;
};

int main()
{
	const int arraySize = 10;

	// You can create on stack like so (which is not always a good idea)
	MyClass myArray[10];

	////////////////////////////////////////////////////////////////////
	// Better is to use heap and pointers.
	MyClass **classptr = new MyClass *[arraySize];

	for(int i = 0; i < arraySize; i++)
		classptr[i] = (MyClass *) new  MyClass(i);

	for(int i = 0; i < arraySize; i++)
		cout << classptr[i]->getVal() << endl;

	for(int i = 0; i < arraySize; i++)
		delete  classptr[i];

	delete classptr;

	//////////////////////////////////
	// Or you can use vector
	vector <MyClass *> myVector;

	for(int i = 0; i < arraySize; i++)
	{
		MyClass *myClass  = new  MyClass(i);
		myVector.push_back(myClass);
	}

	for(int i = 0; i < arraySize; i++)
		cout << myVector[i]->getVal() << endl;

	for(int i = 0; i < arraySize; i++)
		delete  myVector[i];

}
 
Share this answer
 
v3
Comments
Mohibur Rashid 27-Mar-14 2:20am    
OP is still far away to understand vector. +5 for your effort
[no name] 27-Mar-14 2:27am    
Thank you sir.
Refer: Example[^]
 
Share this answer
 
Array of classes Video[^].
 
Share this answer
 
A class by itself, such as the Dog class from the "Classes" tutorial, is quite useless. To make good use of such a class for video game programming purposes we need to be able to group several of them, this way we can minimize code when it comes to controling several objects at a time. This can be done by creating an Array of the object just like you would with any other data type. I will be splitting this programming up into several files as well, this will help keep things organized once your programs get bigger. Heres the example code:
C++
//Dog.h
#ifndef DOG_H
#define DOG_H 1

class Dog{

public:

	Dog();
	int getAge();	
	void setAge(int newValue);

protected:
	int age;


};

#endif

This is the header file for the Dog class, it simply holds the prototype for the class, the actual code will be in a seperate file, Dog.cpp. The "#ifndef DOG_H" line is used to make sure Dog isnt declared twice.

C++
//Dog.cpp
#include "Dog.h"

Dog::Dog(){

}

int Dog::getAge(){

    return age;

}

void Dog::setAge(int newValue){

    age = newValue;

}


Dog.cpp first includes its header file in order to have the prototype for the class, the code here should be familiar from the last lesson.
C++
//main.cpp
#include <iostream>
#include "Dog.h"

using namespace std;

int main()
{
    
    Dog myDogs[5];
    
    for (int i = 0; i <= 4; i++)
        myDogs[i].setAge( i * 2);
    
    for (int i = 0; i <= 4; i++)
        cout << "Dog number " << i << " is " << myDogs[i].getAge() << " years old!\n";
        
    delete [] myDogs;
    
    return 0;
}</iostream>



main.cpp holds the code for the actual program. It only needs to include the header file for the class, not the actuall code file (Dog.cpp). Dog myDogs[5]; simply creates an array of 5 Dogs, just like using any other built in data type.
In order to access each individual Dog, simply include its index number in the brackets, and use it as you would any normal class. This example simply sets their age and displays it.

-KR
 
Share this answer
 
Comments
[no name] 27-Mar-14 0:28am    
This appears to be straight out theft of another's work:
http://www.cppgameprogramming.com/cgi/nav.cgi?page=arrayclasses
Krunal Rohit 27-Mar-14 0:32am    
Yes it is.
I forgot to mention the link in the answer itself. But you did it. Thanks mate :)

-KR
Krunal Rohit 27-Mar-14 0:35am    
Yes it is..

I forgot to mention the link in answer. You did it. Thanks mate. :) :)

-KR
[no name] 27-Mar-14 2:26am    
Obviously you don't even understand what you have appropriated:
delete [] myDogs;
is twaddle.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900