class example
{
public:
int boo;
example() : boo(56)
{
}
};
example character;
int main()
{
cout <<character.boo;
}
This output of this is 56.
When i put:
class example
{
public:
example(int health)
{
}
};
example character(99);
int main()
{
cout <<character.health;
}
I get a compiler error, can you tell me what im doing wrong, im trying to output the character object's health parameter
#include<iostream>
#include<string>
#include<tchar.h>
usingnamespace std;
class example
{
public:
int health;
example(int health)
{
}
};
example character(99);
int main()
{
cout <<character.health;
}
And the output is 0, i thought by putting "example character(99);" i would be assigning health the value of 99? Obviously not, could you tell me the correct way of doing this please.
Scroll down until you see "Constructors and Destructors", and the read everything from there down to "Pointers to Classes". That should be pretty comprehensive. :)
P.S: It sounds like you're reading some book about game programming... that's probably a bit advanced if you haven't gotten to classes yet...
class example
{
public:
int health;
example(int h)
{
health = h;
}
};
example character(99);
int main()
{
cout <<character.health;
}
If you answer me this one question ill be able to understand whats going on.
When ive put "example(int h)" i thought that by giving the constructor int h i was declaring a variable which i would be able to assign values to, could you tell me why i have to put health = h instead of communicating with h directly. Many thanks.
By putting int h, am i just telling this compiler that the constructor can hold an integer value, but to actually use it i have t actually assign a variable to it.
When you say example character(99);, this assigns 99 to 'h' (as a function parameter), but does not do anything with 'health' because health is a completely different variable.
You need to say health = h; so that the 99 gets copied from 'h' (the variable as a function parameter) to 'health' (the member variable of the class)
Think about scope, the h you send the constructor only exists in the constructor. If you dont do anything with it its lost. Just do this example(int h):health(h){}
So as far as i can make out, this SHOULD work because i am send the variable "h" to output
No, because you still have two separate variables:
1) the function parameter
2) the member variable
They just happened to be named the same thing, but they're still 2 different variables.
Basically anytime you use the 'int' keyword, you're creating a new variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class example
{
public:
int h; // 'int' here, so this creates a variable
example(int h) // 'int' again here, so this creates ANOTHER variable
// and before you try it, no, leaving out the 'int' here won't work
{
}
};
There is no way to "join" two variables so that they're the same -- at least not like what you're trying to do.
things passed as parameters have to be their own variable. It's just how it works.
Not really. You're getting your terminology mixed up.
It's not a matter of public/private.
I don't really know how to explain it any better than I already did. When you pass a parameter to a function, it goes in a new variable. That's just the way C++ does it. You you want that value to go to an existing variable, you need to copy it from the parameter to the existing variable.
The parameters of constructor functions are private
Not exactly. They are only in scope within the constructor (just like any other function)
Example:
1 2 3 4 5 6 7 8 9 10 11
void someFunc(int h) {
// here a "new" h is in scope, and can be used
h = 7; // change the value of the "new" h
//when the function ends the "new" h leaves scope and is lost
}
int main() {
int h = 5; // declare a variable called h and give it the value of 5
someFunc(h); // pass h "by value" to the someFunc function
cout << h;
}
Output:
5
You see the 5 gets copied but changing the h in someFunc does nothing to the h outside it's scope. Constructors are just function.