Why the garbage results?

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>

class character {
    int health; // health instance private by default
    int damage; // damage instance private by default
      
    public:
       character(int h) { h = health ; } // character constructor
       int get_health() { return health; } // accessor function to return characters health
       int get_damageDone() { return health - damage; } 
};

int main()
{
    character God(100);
    character Satan(100);
    int gH = God.get_health(), sH = Satan.get_health();
	
    std::cout << "God's health is " << gH << "\n";
    std::cout << "Satan's health is " << sH << "\n";
    
    std::cin.get();
    return 0;
}


It's outputting results lke -858993460 :S. Can someone tell me why?
Last edited on
You're never initializing 'health':

 
       character(int h) { h = health ; } // character constructor 


There, you're assigning 'h', not 'health' (it's backwards)
Silly me, thank you Disch!
Topic archived. No new replies allowed.