Doubt about the use of Static

Asked

Viewed 144 times

0

Code:

Header:

class Cliente
{
public:
    Cliente(std::string nome_c, int num_cartao_l, int livros_c);
    Cliente();

    void calc_Taxa();

    const int get_livros() const { return livros; }
    const std::string get_nome() const { return nome; }
    const int get_cartao() const { return num_cartao; }
    const double get_taxa() const { return taxa; }

private:
    int livros;
    std::string nome;
    int num_cartao;  // numero do cartão do cliente
    double taxa;
};

void const cliente_divida();

Cpp:

Cliente::Cliente(string nome_c, int num_cartao_l, int livros_c)
    :nome(nome_c), num_cartao(num_cartao_l), livros(livros_c)
{
    if (livros < 0) cout << "Livros não podem ser negativos, por favor reinicie o programa." << endl;
}

Cliente default_cliente()
{
    Cliente def("Fulano", 000, 0);
    return def;
}

Cliente::Cliente()
    :nome(default_cliente().nome),
    num_cartao(default_cliente().num_cartao),
    livros(default_cliente().livros)
{
}

void Cliente::calc_Taxa()
{
    taxa = livros * 4; // a taxa é de 4 dólares/reais
}

const void cliente_divida()
{

    if (Cliente::get_taxa() == 0)
    {
        cout << "Cliente não possui dividas." << endl;
    }
    else cout << "Cliente possui uma divida de" << Cliente::get_taxa() << " dolares." << endl;

}

I don’t have anything on main yet.

Finally, the doubt is in const void cliente_divida(), I cannot call the class member without first having declared an object, but in case I need to use the exact rate of the current main object. That would be work for a pointer or some method static that would allow me to call the member without instantiating the object. I tried to transform the function get_taxa() in static and the taxa in static also but I received a unresolved external symbol. I would like to know a clean method for the resolution.

NOTE: For better interpretation, consider that it is a program for library clients.

  • 1

    Maybe it would be interesting for the main(), the get_ taxa(), better show what you want, ie make a [mcve].

  • 1

    What you mean by: "but in case I need to use the exact rate of the current object of the main."? Anyway, how do you know the client is in debt if you don’t want to instantiate it? Because that’s what the command Cliente::get_taxa() you say

  • Opa, I solved instantiating an object in the function with the pointer &, the get_taxa() was literally just that and mine main() was an empty main scope. Anyway, thank you.

  • const void I’ve never seen it before?

1 answer

1

Your cliente_divida function can receive a client instance as a parameter, so another part of the code would be responsible for instantiating the client and calling the cliente_divida function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.