My class is not doing what I want.

I expect the program to run and say this...

My name is Bob! FEAR ME!

I have 100 health!
I can move at 5 MILES PER HOUR!!!
======================================
But I get this.
===================================
My name is ! FEAR ME!

I have -858993460 health!
I can move at -858993460 MILES PER HOUR!!!

Here is the code
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include "BasicMathFunctions.h"//this is not needed.
#include "Measages.h"
using namespace std;

class Warrior{
private:
	string name;
	int health;
	int speed;
public:
	void setname(string setname);
	string getname();

	void sethealth(int sethealth);
	int gethealth();

	void setspeed(int setspeed);
	int getspeed();

	void boast();
};

void Warrior::setname(string setname){
	setname = name;
};
string Warrior::getname(){
return name;
};

void Warrior::sethealth(int sethealth){
sethealth = health;
};
int Warrior::gethealth(){
return health;
};

void Warrior::setspeed(int setspeed){
setspeed = speed;
};
int Warrior::getspeed(){
return speed;
};

void Warrior::boast(){
	cout << "My name is " << getname() << "! FEAR ME! " << endl;
	cout << "\nI have " << gethealth() << " health! \n";
	cout << "I can move at " << getspeed() << " MILES PER HOUR!!!\n";
	};


int main()
{
	Warrior Bob;
	
	Bob.setname("Bob");
	Bob.sethealth(100);
	Bob.setspeed(5);

	Bob.boast();
pause();//a function that pauses the program. (from Measages.h)
};
Last edited on
1
2
3
void Warrior::setname(string setname){
	setname = name;
};


I see here that you set the value of the temporary variable setname. Did you mean to be setting the value of the class variable name?
So name = setname?
ill try. Worked, thank you.
Last edited on
By the way, you can declare a constructor to do it faster, for example:

 
Warrior(string name, int health, int speed);
This was a really dumb mistake dude. And wats up with all the public and private stuff? Wat does it do? And the :: in the modules wat is that?
And wats up with all the public and private stuff? Wat does it do? And the :: in the modules wat is that?


http://www.cplusplus.com/doc/tutorial/
he can explain it to me when i see him, will be way better
Topic archived. No new replies allowed.