I actively use classes, but I have never used it in practice - inheritance and polymorphism, except as learning tasks, and use classes as a generalization of variables and functions in one place ...

Interested in the question. Let's say we have a class planet. And a class solar system. In the class of the solar system there is a parameter - capacity. Suppose from 1000 to 2000 planets. The coordinates of the planets are stored in a two-dimensional array. 0 - emptiness, 1,2 ... n - planets.

0 0 1 0 2 3 0 0 5 6 0 4 0 8 0 7 

Interested in the question. How can I implement:

1) Solar system generation - the number of planets, according to this number planets are created and coordinates are assigned to them

2) The parameters of the classes of the solar system and all the planets are stored in the file (ini, txt ....). And they can be downloaded from the file.

3) Generation of the solar system, namely the creation of planets also chooses which planet we choose, let's say we have a parent class Planet and child classes LivePlanet, MoonPlanet, UsualPlanet, they all have their own functions and their parameters naturally.

  • Hello, and can the full code for the example please !! And if there is something on python too, pliiz. - BROman

2 answers 2

1) If you had a std::vector<Planet*> planets instead of a two-dimensional array in the class, you could do the generation of the planets like this:

 for (int i = 0; i < planetsCount; i++){ planets.push_back(new Planet); } 
  • Okay, my mistake, I'm not used to using high-level data storage methods, such as vectors. But actually how to organize storage with all the parameters, and how do we contact new Planet? - ParanoidPanda
  • All the parameters in the class - it is stored in the vector. There is no need to contact new Planet; it is enough to write planets [0] -> SomeMethod (foo, bar) to refer to the first element of the vector. - Vladimir Martyanov
  • I speak about storage in a file. And I understand about my third question, it is easier to choose a random one personally or somehow what kind of planet it will be. - ParanoidPanda
  • Storing in a file is necessary to watch serialization, I do this with my hands if necessary, but this approach has many opponents. - Vladimir Martyanov
  • Advise literature on serialization and extended use of classes, for example, as in my case. - ParanoidPanda

I answer the question "how to generate a solar system": here you can use a typical technique - a factory (pseudo-code):

 enum class PlanetType{Live, Usual, Moon}; std::unique_ptr<Planet> createPlanet(PlanetType type, arguments...) { switch(type) { case PlanetType::Live: return std::make_unique<LivePlanet>(arguments...); case PlanetType::Usual: return std::make_unique<UsualPlanet>(arguments...); case PlanetType::Moon: return std::make_unique<MoonPlanet>(arguments...); } } int main() { std::vector<std::shared_ptr<Planet>> planets; for(...) { PlanetType type == getTypeFromSomewhere; planets.emplace_back(createPlanet(type, getArgumentsFromSomewhere)) } } 

So you get a vector that contains all the planets. BUT, from my point of view, inheritance here generally does not fit any side. I believe that the type of planet is contrived and on whether the planet is a different satellite or not, its behavior does not change at all. Inheritance is necessary when the behavior of different heirs can be different and causing a polymorphic function, each heir behaves differently.

I do not see where the Earth behaves differently from Jupiter. Yes, they have different mass, different chemical composition, but these are details that do not affect the behavior.

  • I just brought this example out of my head, the more interesting the question of saving and loading classes is more - ParanoidPanda