0

I'm studying C++, because in my school I'm taking it as a subject, my professor did this piece of code and I have a question.

class binario{

public: 
    int n;

    binario(); // constructor sin parametros
    binario(int); //Constructor con parametro

    void estado(); //funcion
    long int convierte(); //funcion
    long int complementouno(); //funcion
};
binario::binario(){ //binario clase :: binario constructor sin parametro
    n=0; //inicialia n = 0
};

binario::binario(int n){ //binario clase :: binario (int n) constructor con parametro
    **binario::n=n;**   
};

What does binario::n=n means?? Thank you I hope I can get some help, my professor isn't that kind enough.

joce
  • 9,624
  • 19
  • 56
  • 74
  • It means you are setting the class member `n` with the value from the parameter `n` which hides the class member `n`. I always avoid the situation by naming the parameter differently than the class member. – drescherjm May 04 '15 at 18:15
  • 3
    Just a note, the `;` after the closing bracket of functions are unnecessary. (clang gives a warning for this) – Borgleader May 04 '15 at 18:18
  • 1
    related: http://stackoverflow.com/questions/9338217/why-does-c-need-the-scope-resolution-operator – NathanOliver May 04 '15 at 18:24
  • Thank you so much! I think i'm understanding now – Erick Navarro May 04 '15 at 18:28
  • Possible duplication: [What is the "::" operator?](http://stackoverflow.com/questions/4269034/what-is-the-meaning-of-prepended-double-colon-to-class-name) – Thomas Matthews May 04 '15 at 19:08

3 Answers3

2

:: is for accessing static variables and methods of a class/struct or namespace. It can also be used to access variables and functions from another scope (actually class, struct, namespace are scopes in that case)

So, you are using this to access the n variable.

ryekayo
  • 2,341
  • 3
  • 23
  • 51
1

It sets the value of n inside the class to the value of n given as a parameter upon construction. Since both share the same name, some distinction is required.

It is preferred to write:

binario::binario(int n) {
    this->n = n;   
}

or use different names:

binario::binario(int a_number) {
    n = a_number;
}

Note: this is a pointer to the object and can be used to access all members inside it, you can use this inside any function of the class.

Edit: (Added from comments.) You would use binario::n or this->n to explicitely refer to the int n inside the class. This is needed because both the parameter name and data member have the same name: n. In the second example, the parameter has a different name, a_number, and it is clear that n refers to the data member and not the parameter.

Maarten Bamelis
  • 2,243
  • 19
  • 32
  • binario::binario(int nn){ n=nn; } Shouldn't it be binario::n=nn? Or why does this one doesn't have binario:: – Erick Navarro May 04 '15 at 18:27
  • You would use `binario::n` or `this->n` to explicitely refer to the `int n` inside the class. This is needed because both the parameter name and data member have the same name: `n`. In the second example, the parameter has a different name, `nn`, and it is clear that `n` refers to the data member and not the parameter. – Maarten Bamelis May 04 '15 at 18:31
1

That code is no good example. This might be better:

class binario{

    public: 
    // If you are are using C++11 (or higher):
    // int n = 0;
    int n;

    binario();
    binario(int);

    void estado();
    long int convierte();
    long int complementouno();
};

binario::binario()
// If you are are not using C++11 (or higher). Otherwise omit this and use 
// above initialization.
: n(0) 
{};

binario::binario(int n)
// Prefer class member initializer lists. Here the first 'n' is the member
// and the second 'n' the argument. Although, you might consider different
// names, to avoid confusion.
: n(n)
{};

As mentioned by other, you might use this->n or binario::n (please prefer this->n) in function (constructor) bodies, to distinguish a 'binario::n' from an outside 'n';

Please notice: I stripped all useless comments and added some other (commenting just for commenting is a bad habit).

  • Now I understood, my teacher did that code, and he does some wierd things, but wth all the good explanations I got from all of you I could understand it, thank you very much :D – Erick Navarro May 04 '15 at 18:54