Assigning private members values through public functions in C++ -


i have bit of confusion on assigning private members value in c++.
far think both of following ways of setting length , breadth should work. difference , 1 way correct?

class box {    public:       void setlength(double len)       {          length = len;       }       void setbreadth(double bread)       {          this->breadth = bread;       }    private:       double length;     // length of box       double breadth;    // breadth of box }; 

both correct. within body of class member functions, class members in scope. this-> implicit. while this->breadth explicitly accesses breadth member, length in setlength() implicitly accesses this->length.

explicit access allow use same name argument , member:

void setbreadth(double breadth) {     this->breadth = breadth; } 

in case, unqualified access refers argument , qualified access refers member.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -