How do I create an optional parameter in a class method?

2

I know that to create an optional parameter in a function you can do this:

void exemplo(int a,int b=0);

But how do I do this in a function of a class
ex:

class Exemplo{
    public:
    void nada(int,int);
};
void Exemplo::nada(int a,int b){}

This would be an example with "normal" parameters, to try to leave the optional (b) I did:

void Exemplo::nada(int a,int b=0){}

void nada(int,int);

How can I solve this problem, remembering that I do not want a constructor with optional parameters, but rather a different function.

    
asked by anonymous 02.02.2018 / 21:57

1 answer

2

It's the same thing:

#include <iostream>

class Exemplo {
    public:
    void nada(int a, int b = 0);
};
void Exemplo::nada(int a, int b) { std::cout << b; }

int main() {
    Exemplo x = Exemplo();
    x.nada(1);
}
    
02.02.2018 / 22:03