Help with classes in sep. files

Hi guys, I'm just having trouble getting classes in separate files to run properly, I was just going to try out classes to set up a text-based game. so here's where I'm at;

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include "playr.h"

using namespace std;


int main ()
{
playr nm;

cout<<nm.getatk()<<" "<<nm.getdef()<<endl;
}


Playr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef PLAYR_H
#define PLAYR_H


class playr
{
    public:
        playr();
int getatk();
int getdef();
int gethp();
int getlvl();

private:
    int atk;
int def;
int fullhp;
int lvl;

};


#endif // PLAYR_H 


playr.cpp
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
#include "playr.h"
#include <iostream>
#include <string>

using namespace std;
playr::playr()
{
int atk = 19;
int def = 18;
int fullhp = 10;
int lvl = 15;
}

int playr::getatk()
{
    return atk;
}
int playr::getdef()
{
 return def;
}
int playr::gethp()
{
 return playr::fullhp;
}
int playr::getlvl()
{
    return lvl;
}


The output I get from the cout in Main() is this;
198517466 4273152


This almost sounds like when I cout a pointer, but There are no pointers anywhere in my codes, and the numbers are even longer if I use any more of the functions from my playr.cpp file. Any clues to what's happening or what I'm doing wrong here?

I haven't tried separate files for a class before, Thank you for any help.
Last edited on
In the constructor you are defining local variables that you give values. Remove the int in front of the names and it will work better.
Topic archived. No new replies allowed.