How to implement a class in another file

Hi guys,

I am pretty new with C++, I have some Java experience from a few years ago but most of it has left me and I am having to relearn everything and right now I am trying with classes from other files.

I understand the idea for classes but the actual implementation eludes me.
What I want to do is to take a function below, for example the warriorPicked function and put it in a class that i will call something appropriate and then call on that class or from a function in a class for that "character" type so like...

I have my main code, then when I need it, I call on the that function giving me that information, from a class in a different file.

My current code is as follows:

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;

#include "HeroClasses.h"
#include "HeroClasses.cpp"

//hero specs
string heroName;
string heroClass;

string attrn[6] = {"Str","Agi","Wis","Spd","HP","MP"};
int attr[6] = {1,1,1,1,1,1};

void warriorPicked()
{
            attr[0]= 7;
            attr[1]= 5;
            attr[2]= 2;
            attr[3]= 4;
            attr[4]= 100;
            attr[5]= 30;

        cout << "Ah, so you are a mighty Warrior!" << endl << endl;
        cout << "Your starting attributes will be: " << endl
        << attrn[0] << " is  " << attr[0] << endl
        << attrn[3] << " is  " << attr[3] << endl
        << attrn[4] << " is  " << attr[4] << endl
        << attrn[5] << " is  " << attr[5] << endl;
}

void magicianPicked()
{
            attr[0]= 2;
            attr[1]= 4;
            attr[2]= 7;
            attr[3]= 4;
            attr[4]= 80;
            attr[5]= 100;

        cout << "Ah, so you are a mighty Warrior!" << endl << endl;
        cout << "Your starting attributes will be: " << endl
        << attrn[0] << " is  " << attr[0] << endl
        << attrn[3] << " is  " << attr[3] << endl
        << attrn[4] << " is  " << attr[4] << endl
        << attrn[5] << " is  " << attr[5] << endl;
}

void roguePicked()
{
            attr[0]= 5;
            attr[1]= 7;
            attr[2]= 3;
            attr[3]= 5;
            attr[4]= 90;
            attr[5]= 50;

        cout << "Ah, so you are a mighty Warrior!" << endl << endl;
        cout << "Your starting attributes will be: " << endl
        << attrn[0] << " is  " << attr[0] << endl
        << attrn[3] << " is  " << attr[3] << endl
        << attrn[4] << " is  " << attr[4] << endl
        << attrn[5] << " is  " << attr[5] << endl;
}

int main (void)
{
            cout << "Hello, what is your name?" << endl;
            cin >> heroName;
            cout << endl << "Ah, so you're name is " << heroName << "!" << endl;
            cout << "Welcome to the world of Lazarus where great adventures awaits!" << endl <<endl;

            for ( ;; ){ //start of for loop
            cout << "So " << heroName << ", are you a Warrior, Magician or Rogue?" << endl;
            cin >> heroClass;

    //start lowercase function
        for (int i=0;i<heroClass.length();i++)
            { // input.length() gets the length of the string
                heroClass[i]=tolower(heroClass[i]);// convert every character of input to lowercase
            }//end lowercase function

if (heroClass == "warrior"){
            warriorPicked();
            break;
}
else if (heroClass == "magician"){
            magicianPicked();
            break;
}
else if (heroClass == "rogue"){
            magicianPicked();
            break;
}
else{
    cout << "I'm not sure I understood you, can you repeat?" << endl << endl;
    continue; //at top of for loop
}

return 0;
}
}

How would you create an object for your class first?
Topic archived. No new replies allowed.