Object arrays

I am self learning c++. I am confused as to object array and whether the number in the square brackets refers to the actual number of objects.

IE: Plane sopwith [4];

Does this mean there are 4 identical sopwith objects belonging to the class Plane?
If this is the case, then is each sopwith object then automatically numbered from 0 to 3?

Thank you.
Does this mean there are 4 identical sopwith objects belonging to the class Plane?

The standard constructor will be called for each of the four objects.
Whether the objects end up being identical depends on the constructor.

If this is the case, then is each sopwith object then automatically numbered from 0 to 3?

Yes, if that's how you want to put it...
Class is a model from where the objects are created.

Let me explain:
1
2
3
4
5
class Plane {
public:
void something(void);
// ....
};

This tells the compiler what kind of an entity a Plane is.

Then you can create 4 Plane objects in an array:
Plane sopwith[4];

Who you can access by saying:
1
2
3
4
sopwith[0].something();
sopwith[1].something();
sopwith[2].something();
sopwith[3].something();

Thankyou very much for those fast, informative replies.

Is it possible then, for the class Plane to have several of those object arrays?
ie; Plane sopwith [4] and Plane sopwithPup [8] and perhaps even more... say... sopwithCamel[10] etc?
Also, Athar you said in response to my "If this is the case, then is each sopwith object then automatically numbered from 0 to 3?"

You wrote " Yes, if that's how you want to put it..."
I was just wondering what the correct terminology is that I should have used.
Is it possible then, for the class Plane to have several of those object arrays?

You've got a bit wrong way. The class Plane does not "have" the objects (or arrays of objects) created from its description. Instead, you should think of objects as variables, such as int, float, etc.. Variables and objects are "owned" by the context:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Plane B52;  // This is a global object, destroyed upon program exit
int i;  // This is a global variable, destroyed upon program exit

int Function(void)
{
	Plane Airbus[4];  // These are 4 Airbus planes "owned" by the Function() and destroyed upon "return 0;"

	if (...) {
		Plane Jumbo;
		// ....
	} // Jumbo is owned by if()'s context and is destroyed here

	return 0;
}

That said, there is not much difference in how a regular variable (int, float, ..) and objects (Plane, ...) are treated.
Thanks for the reply.
I think I'm getting it.
Is it possible to pass an object array by reference?
I think i've worked out how to do it.
Thanks.
It is better to pass the array by reference (Plane &PlaneArr[]). If you pass the array by copy (Plane PlaneArr[]), the compiler will have to do a copy operation to every Plane in that array, and this:
- Might take a lot of processing time.
- Will leave the original array unchanged because the copy of the array is modified.

If you have a function that is not supposed to change anything in the array, pass it by const reference.
@ELEGA

Major Error there that needs to be corrected before we go any further.

Arrays are NOT passed by copy. (Plane PlaneArr[]) may look as though it is being passed by copy-
but a pointer is passed to the function.
Last edited on
@guestgulkan

Ah, thank you good sir. I had forgotten that.
The intention is that the object array goes to a function which may alter whatever was sent. ie; if sopwith[0] vs baron[3] are sent to the battle function its class attributes are supposed to be accessed and damage updated to either instance as required.
ie (pseudo)

class Plane{};
class Enemy{};

Plane sopwith [10];
Enemy baron [8];


battlefunc (sopwith [2], baron[0]);

battlefunc (Plane &sop, Enemy &red)
Topic archived. No new replies allowed.