0
class person{ private : int age=8; public : person(int age){ cout<<this->age; } }; int main() {person p(2);}
6 odpowiedzi
0
but according to the constructor in the class age should be = to 2.....
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;}
0
Are the 2 age variables (one at starting with value = 8 and other in the brackets) different?
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.
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 !
0
Okay...understood thnx