constructor parameters

Mar 30, 2011 at 8:27pm
Hi, from looking at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
Last edited on Mar 30, 2011 at 8:28pm
Mar 30, 2011 at 8:32pm
Take a look at both classes again, Tp. In particular notice that your second class doesn't seem to have a member named "health".

-Albatross
Mar 30, 2011 at 8:37pm
Ahhh, thanks, I have changed the second one to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>
#include<tchar.h>

using namespace 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.
Mar 30, 2011 at 8:50pm
The tutorials here I think will help sort things out:
http://cplusplus.com/doc/tutorial/classes/

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...

-Albatross
Last edited on Mar 30, 2011 at 8:52pm
Mar 30, 2011 at 9:35pm
You are passing 99 into the constructor
1
2
3
example(int health) {
	// constructor does nothing! 
}
but then you are not doing anything with the health variable.

Try:
1
2
3
4
example(int h) {
	// assign object's health variable to whatever was passed to h 
	health = h;
}
Mar 30, 2011 at 11:55pm
Ok, this works,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.

Last edited on Mar 31, 2011 at 12:25am
Mar 31, 2011 at 12:51am
:) ?
Mar 31, 2011 at 12:55am
'h' and 'health' are two separate variables.

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)
Mar 31, 2011 at 1:00am
Thanks for your reply and clear explanation. So as far as i can make out, this SHOULD work because i am send the variable "h" to output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

using namespace std;

class example
{
      
public:
       
int h; 

example(int h) 
 
   {
            
      
      
   }
   
};

example character(99);

int main()
{
    
cout <<character.h;

}


But that outputs 0.
Last edited on Mar 31, 2011 at 1:16am
Mar 31, 2011 at 1:34am
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){}
Mar 31, 2011 at 2:57am
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.
Mar 31, 2011 at 3:10am
Ok, so am i right in thinking this:

The parameters of constructor functions are private, so we have to use a public variable to act as the middle man?


Last edited on Mar 31, 2011 at 3:20am
Mar 31, 2011 at 3:56am
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.
Mar 31, 2011 at 3:56am
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.
Last edited on Mar 31, 2011 at 4:33am
Mar 31, 2011 at 9:50pm
Thanks :) all of the explanations helped.
Topic archived. No new replies allowed.