0

class person{ private : int age=8; public : person(int age){ cout<<this->age; } }; int main() {person p(2);}

https://code.sololearn.com/cF16N6hiAzVQ/?ref=app

5th Jul 2017, 2:17 PM
Kushagra Agarwal
Kushagra Agarwal - avatar
6 odpowiedzi
0
but according to the constructor in the class age should be = to 2.....
5th Jul 2017, 2:28 PM
Kushagra Agarwal
Kushagra Agarwal - avatar
0
You never change age's value. In the constructor, you only display age's default value (8). To change age's value, write this : person(int e): age(e){ cout << this->age;}
5th Jul 2017, 2:36 PM
Jojo
0
Are the 2 age variables (one at starting with value = 8 and other in the brackets) different?
5th Jul 2017, 3:06 PM
Kushagra Agarwal
Kushagra Agarwal - avatar
0
In my code the one in the brackets is a parameter. It takes the value of the value in brackets when the function is called: void f(int x){} int main(){ f(2); } // x's value is 2.
5th Jul 2017, 3:26 PM
Jojo
0
This the code you want to do : class person{ private : int age=8; public : person(int x){age=x cout<<this->age; } }; int main() {person p(2);} And this is how it runs: person p(2); // Create an object p from class person and give value 2 to parameters. person(int x){ // x=parameters' value (x=2) age=x; // age=2; cout << this->age; // cout<<age; (2) Have you understood ? If not ask !
5th Jul 2017, 3:33 PM
Jojo
0
Okay...understood thnx
5th Jul 2017, 4:27 PM
Kushagra Agarwal
Kushagra Agarwal - avatar