Hi, just have a question regarding classes/inheritance and a variable.
ive been trying to work out the best way to do it, but would like some outside input. ok..here goes:
i have a base class Ship.
classes will inherit from this class.
i also have a class tinyShip (and later medShip and largeShip) that inherits from the base class.
these classes would have an 'int maxCargo' variable which would be the maximum capacity of the ship.
my question/part im having trouble with is what would be the best way to implement a cargo system.
each class will have a different max, and i was thinking of going with an array or vector that would be created taking maxCargo in as its size, but how would i go about coding something like that?
the reason i was thinking in terms of arrays is because i want to be able to pull the name of whats stored in a particular "cell" of the cargo.

hope i havent been too confusing...
I appreciate any tips or suggestions, thanks in advance.
-g

Recommended Answers

All 6 Replies

Hmm.. I'm slighty confused, but perhaps you're thinking of a static member?

It would sorta work like this:

class CShip {

public:
    // ....
    static int maxCargo;
};

Then for each class you can set this variable, and it will remain the same throughout all instances of the classes.

The tricky part is where you derive your classes. You could simply override the variable each time you derive from the class. It might not be the best way of doing it, but if you left it as it was in the base class, it would remain the same not only throughout all instances of CShip but the value would also be the same in CMedShip and CLargeShip . Not what you want if I understood you correctly.

Thanks for the reply..
the maxCargo wasnt really an issue...
maybe if i explain it alittle better...
maxCargo is in the base class
when inherited, tinyShip will get maxCargo of lets say 10
medShip will get 15...and so on...i have that part pretty much figured out.
(and btw, there will only be one instance at a time (the player can trade in ships))
its the array part that i cant get an idea on...

im thinking of something like

char cargo[maxCargo];

where each ship will have a character array that will be loaded with cstrings

im not sure that will even work now that i think about it...
any ideas on how i could put it into my base class to be inherited?

Hi, just have a question regarding classes/inheritance and a variable.
ive been trying to work out the best way to do it, but would like some outside input. ok..here goes:
i have a base class Ship.
classes will inherit from this class.
i also have a class tinyShip (and later medShip and largeShip) that inherits from the base class.
these classes would have an 'int maxCargo' variable which would be the maximum capacity of the ship.
my question/part im having trouble with is what would be the best way to implement a cargo system.
each class will have a different max, and i was thinking of going with an array or vector that would be created taking maxCargo in as its size, but how would i go about coding something like that?
the reason i was thinking in terms of arrays is because i want to be able to pull the name of whats stored in a particular "cell" of the cargo.

hope i havent been too confusing...
I appreciate any tips or suggestions, thanks in advance.
-g

I guess it depends on how much detail you want with your cargo. Probably the best solution is to have a cargo class that can be constructed with an initial size.

class ShipCargo {
public:
  ShipCargo( int capacity );
};


class Ship {
private:
  ShipCargo cargo_;
public:
  Ship( ShipCargo cargo ): cargo_( cargo ) { }
};


class TinyShip: public Ship {
public:
  TinyShip(): Ship( ShipCargo( 10 ) ) {}
};


class MediumShip: public Ship {
public:
  MediumShip(): Ship( ShipCargo( 15 ) ) {}
};


class LargeShip: public Ship {
public:
  LargeShip(): Ship( ShipCargo( 20 ) ) {}
};

Now you can do something as simple as an array or as complicated as a full inventory system without changing how your ships work. :) I know that doesn't really answer your question, but how does cargo work in your program? That is what defines your solution.

thanks, that really shed some light on what i can do. :cheesy:
as far as how the cargo system works in my program...what i want to do is as follows..
say you have cargo space of 10
you buy 2 waters and 3 food, so from a null filled array it goes to:
0[water]
1[water]
2[food]
3[food]
4[food]
5[\0]
6[\0]
7[\0]
...
9[\0]
(it really doesnt matter whats inside...but i was thinking of linking it to a texture to display whats in the cell)
ill also have maxCargoSpace and currCargoSpace, to keep track of space availability..

Thanks for the reply..
the maxCargo wasnt really an issue...
maybe if i explain it alittle better...

Sorry, I misunderstood you! You may want to use vectors instead of arrays; you can dynamically add and remove to them. So you could have something like:

void Cargo::addCargo(string newCargo) {
    name.push_back(newCargo);
}

Of course, if the cargo class contains more than simply a name, it might be a better idea to make a vector of the entire class, and not of individual members. However, the idea is the same, except that the vectors would be controlled by Ship instead of ShipCargo .

i was leaning toward vectors too. thanks for the advice, it helps alot. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.