Classes questions

Alright so I don't know if there is a better way to go about this but I'm somewhat doing it like how we had to do in an assignment during the previous semester. I'm just doing this on my own its not for a class.

The problem I'm having is figuring out how to get an object added to an array list. For example what I want to do is to have a class for a vehicle, the class specifies the mpg, cost, fuel tank capacity and more, and from here I have another class called fleet. What I want to do is add the vehicle object to the fleet class to keep track of what vehicles are in the fleet and how many.

So far what I have is not much because I can't really remember where to go from here. We did do an addtolist function before with classes in the previous class I took but I can't quite remember how to do it. I can't remember if there are other steps I need to do before adding it or just jump straight to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const int MAX_FLEET_SIZE = 10000;

class Vehicle
{
private :

	string VehicleType;
	int mpg;
	int maxCargo;
	int cost;
	int topSpeed;
	int gasTankCapacity;

public:

	Vehicle()
	{
		VehicleType = "";
		mpg = 0;
		maxCargo = 0;
		cost = 0;
		topSpeed = 0;
		gasTankCapacity = 0;
	}

	Vehicle(string Vt, int mPg, int mC, int c, int tS, int gTC)
	{
		VehicleType = Vt;
		mpg = mPg;
		maxCargo = mC;
		cost = c;
		topSpeed = tS;
		gasTankCapacity = gTC;
	}
};

class Fleet
{
private:

	Fleet fleet[ MAX_FLEET_SIZE ];

public:

	void addVehicle()
	{

	}

};

int main()
{
	Vehicle vehicle;

}
Line 41. This is not how to express what you want. What you should say is in line with your description: "A fleet has a collection of vehicles." It looks like you want to use an array to represent the collection of vehicles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Fleet
{
private:
    Vehicle vehicles[MAX_FLEET_SIZE];
    int numVehiclesInFleet;

public:
    Fleet()
    {
        numVehiclesInFleet = 0;
    }

    void addVehicle(Vehicle v)
    {
        if (numVehiclesInFleet < MAX_FLEET_SIZE)
        {
            vehicles[numVehiclesInFleet] = v;
            numVehiclesInFleet++;
        }
    }

};
Topic archived. No new replies allowed.