what is the point of setting 'speed=a' in the 4th line of this code instead if just directly using 'int speed' in the parentheses of the 'setcarspeed' (3rd line) function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is the point of setting 'speed=a' in the 4th line of this code instead if just directly using 'int speed' in the parentheses of the 'setcarspeed' (3rd line) function?

class myClass { public: void setcarspeed (int a){ speed=a; } int getcarspeed(){ return speed; } private: int speed; }; int main() { myClass obj; obj.setcarspeed (40); cout<<obj.getcarspeed ; return 0; }

23rd Dec 2016, 1:18 PM
Cody Arthur
Cody Arthur - avatar
1 Answer
+ 1
int a is the parameter of the setcarspeed() function and will only be used inside that function. Same would apply for int speed if you'd use it as a parameter. Speed = a uses the value of your parameter a and makes your class member speed use it for everything that you desire in the instance of your object obj
23rd Dec 2016, 1:41 PM
Zablas
Zablas - avatar