0

The thing that i want to make is first ask what vehicle's stats do you want to know with getline

Something like this:

cout << "Write an vehicle name" << endl;
And if the user write Mustang, call to Mustang.mostrarMensaje, but I don't want to use if I want something more automatic

  #include <cstdlib>
  #include <iostream>
  #include <string> 
  using std::cout;
  using std::endl;
  using std::string;
  using namespace std;
  class Vehiculos 
  {
        public:
        int suGas;
        int suVelocidad;
        int suCondicion;
        int suTipo;  

        void mostrarMensaje()
        {
             cout << "Estadisticas de su vehiculo!" <<  endl;
             cout << "Gas:" << suGas << endl;
             cout << "Velocidad maxima:" << suVelocidad << endl;
             cout << "Condicion:" << suCondicion <<  endl;
             cout << "Tipo:" << suTipo <<  endl;
        } 
  };

  int main(int argc, char *argv[])
  {
      Vehiculos Mustang;
      Mustang.suGas = 100;
      Mustang.suVelocidad = 250;
      Mustang.suCondicion = 100;
      Mustang.suTipo = 2;
      Mustang.mostrarMensaje();
      system("PAUSE");
      return EXIT_SUCCESS;
  }
Raul Lara
  • 17
  • 1
  • 6
  • 1
    The short answer is that C++ doesn't directly support what you're asking for. One possibility would be to create a `std::unordered_map` (or something on that order) to map from strings entered by the user to objects in your program. – Jerry Coffin Aug 09 '14 at 18:25
  • Read [this answer](http://stackoverflow.com/a/25198583/841108) and adapt it to C++ .... – Basile Starynkevitch Aug 09 '14 at 18:39

1 Answers1

2

When a c++ program is compiled into assembly a lot of information is discarded by the compiler. Some languages have a feature called reflection where information such as class names are available at run time. c++ does not have this built in, but you can implement a reflection system ontop of it.

Reflection is a fairly advanced topic, and probably a bit more than you're looking for - though it is worth knowing that it exists. A simpler approach that could get the job done here would be to use the string as a key into a data structure of some kind, such as an std::unordered_map with pointers to a base class Vehiculos from which you derive Mustang with a virtual method named mostrarMensaje.

Some psuedo code (not guaranteeing this to compile) of using the method I mentioned above (polymorphism):

// Abstract base class
class Vehiculos
{
  // Look up virtual destructors if you don't understand why this is here.
  virtual ~Vehiculos() { /*...*/ }

  // Pure virtual method
  virtual void mostrarMensaje() = 0;
};

class Mustang
{
  virtual ~Mustang() { /*...*/ }

  virtual void mostrarMensaje() 
  {
    /* Implement mustang specific logic here */
  }
};

class F150
{
  virtual ~F150() { /*...*/ }

  virtual void mostrarMensaje() 
  {
    /* Implement F150 specific logic here */
  }
};

int main(int argc, char *argv[])
{
  // Ensure at least one parameter was passed to the program 
  //   (first argument will always be the program's name)
  if(argc < 2)
  {
    // Print an error message
    return -1;
  }

  std::unordered_map<std::string, Vehiculos*> vehicles;
  vehicles.insert("Mustang", new Mustang);
  vehicles.insert("F150", new F150);

  auto find_it = vehicles.find(argv[1]);
  if(find_it != vehicles.end())
  {
    (*find_it)->mostrarMensaje();
  }
  else
  {
    // User entered an invalid vehicle name, do something about it
  }

  return 0;
}
Community
  • 1
  • 1
Peter Clark
  • 2,863
  • 3
  • 23
  • 37