Constructor with member variable?

2

I have a question about the following main

#include "Pokemon.h"

int main()
{
Pokemon piplup("Piplup", Pokemon::Agua);
piplup.mostrar();
cout << endl << endl;
}

I must say that the main is a reference that the teacher passed us to use the classes. My question is about the first line, the constructor's parameter Pokemon::Agua , if I'm not mistaken, it refers to a member variable, that is, water. However, I'm lost when I do the pokemon class, since in the exercise instructions it says that every pokemon has a name and can only be of four types, water, earth, air and fire . In addition to the methods that I did, then the header, there must be one to get a string with the name of the type given the type code and if necessary include constants in the pokemon class . I have the class like that, but I do not understand how to implement the constructor so that from the main it is called as the reference main

#ifndef POKEMON_H
#define POKEMON_H
#include <string>
using std::string;

class Pokemon
{
        string nombre;
        string tipo;
    public:
        string getTipo ();
    //Constructores 
        Pokemon();
        Pokemon (string nombre, string tipo);

        ~Pokemon();

    //Metodos de acceso
        string getNombre ();
        string getTipo (string tipo);
        void setNombre (string nombre);
        void setTipo (string tipo);


    //Metodo utilitario
        void mostrar ();    

};

#endif

If you can help me, I would really appreciate it!

    
asked by Cristhian Ivan 11.11.2018 в 01:03
source

1 answer

3
  

If I'm not mistaken, it refers to a member variable

Incomplete. It refers to a member variable ... static . Or a enum , considering these as a special variant of variables static .

The things -member-static are those that are accessed without needing an instance of the class; we could say that they belong to the class itself.

To access them from outside of the class, it is necessary to identify them as you explain, indicating the class they belong to.

Using static-member-variables and C ++ 11, your class would look like this:

class Pokemon {
  string nombre;
  string tipo;

public:
  static int Agua = 1;
  static int Tierra = 1;
  static int Aire = 3;
  static int Fuego = 4;

  Pokemon();
  Pokemon( string nombre, int tipo );
  ...

I indicate the C ++ 11 because in previous versions you can not initialize the static variables in the class declaration itself ; it must be done in a different file.

Another possible option, which if is valid for C ++ before 11, is to use a enum :

class Pokemon {
  string nombre;
  int tipo;

public:
  enum {
    Agua = 1,
    Tierra,
    Aire,
    Fuego,
  };
  ...

So now you have 2 options for your constructor: keep using a int , which makes it prone to errors ... or take advantage of it being a enum , and the compiler will take note of it and will not allow use incorrect values.

To do this, small changes are necessary: you have to declare TipoDePoquemon before to use it, and change the type of the member-variable:

class Pokemon {
public:
  enum TipoDePoquemon {
    Agua = 1,
    Tierra,
    Aire,
    Fuego,
  };

private:
  string nombre;
  TipoDePoquemon tipo;

public:
  enum TipoDePoquemon {
    Agua = 1,
    Tierra,
    Aire,
    Fuego,
  };

  Pokemon( );
  Pokemon( string nombre, TipoDePoquemon tipo );
  ...

In the latter case, if you try to call the constructor with an incorrect value ...

Pokemon pikachu( "pikachu", 5 );
  

error: invalid conversion from 'int' to 'Pokemon :: TipoDePoquemon'
  note: initializing argument 2 of 'Pokemon :: Pokemon (std :: string, Pokemon :: TipoDePoquemon)'

It is necessary to call it with one of the correct values of TipoDePoquemon :

Pokemon pikachu( "pikachu", Pokemon::Aire );
    
answered by 11.11.2018 / 06:15
source