Is my code standard? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is my code standard?

I have a few more questions 1) is addrOfId is a pointer too? Do I need to declare every function like this, if it returns or work on a pointer?? 2) I thought parameters and variables are different.. but when I name the parameter like the variable, it shows a garbage value. Why is that?? Even though they have different address.. 3) Do I need to declare every set function in "setVariableName" format?? I mean can't i use "_VariableName" or "PVariableName"? https://code.sololearn.com/cPQKW974A7BB/?ref=app

24th Aug 2021, 12:05 PM
Samael Morningstar
4 Answers
+ 4
For 2) You can give them the same name but you have to refer explicitly to the class attribute with this-> so the compiler know which is which. For example: void setPassword(string password) { this->password = password; }
24th Aug 2021, 3:10 PM
Lisa
Lisa - avatar
+ 3
I don't understand what you mean by "implicit" but the problem is when the parameter is named "password" the compiler assumes that all occurances of "password" in the method refer to the parameter name: with password = password we would assign the parameter to itself. If the parameter name is, for example, "p", than the compiler can guess you mean the attribute password when you do "password = p" When you do "this-> password = password" then the compiler can be sure that the left side refers to the class attribute and the right side to the methods parameter name. I need to mention that I basically don't know any c++. On 3) You could make all attributes public to avoid writing extra getters and setters. But that kind of undermines encapsulation and you may need to validate data when setting the value anyways...
24th Aug 2021, 3:26 PM
Lisa
Lisa - avatar
+ 2
Lisa Though you don't know cpp, you have explained well and I have got the answer of my second question.. About question 3) I was just trying to learn how to protect data and use them.. ๐Ÿ™‚ And that's why I will keep them private.. And about the word "implicit", I just thought there might be something like implicit which will be the opposite of explicit..๐Ÿ˜‚ Thanks Sister๐Ÿ™‚
24th Aug 2021, 3:36 PM
Samael Morningstar
0
Lisa I guess there is a word implicit too.. Can you tell me about implicit too?
24th Aug 2021, 3:13 PM
Samael Morningstar