Can i initialize my variables in the constructor?

I have been initializing my variables in main, but i was wondering if i could initialize them in the constructor, is there a reason i would choose one way over the other?

variables initialized in my main

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
int main()
{
    Player player("Player1",    //Players name
                  200);         //Players money

    Game game(true);            //Is the first time the game has been started

    Pet pet("PetName",          //Pets Name
            "PetSpecies",       //Pets Species
            100,                //Pets health
            0,                  //Pets thirst level
            100,                //Pets stamina
            0.8,                //Pets age; 0 = years 8 = weeks
            100,                //Pets happiness
            0,                  //Pet hunger
            "000",              //Players pet ID number
            true,               //Pet is happy
            false,              //Pet is sad
            false,              //Pet is angry
            false,              //Pet is tired
            false,              //Pet is hungry
            false,              //Pet is sleeping
            false,              //Pet is thirsty
            false,              //Pet is dead
            false,              //Pet is a dog
            false,              //Pet is a cat
            false,              //Pet is a mouse
            false,              //Pet is a hamster
            false,              //Pet is a parrot
            false,              //Pet is a turtle
            false,              //Pet is a goldfish
            false,              //Pet is a monkey
            false,              //Pet is male
            false);             //Pet is female

    NewGameSetup(player, game, pet);

    return 0;
}


also this is my pet class, am i making it right?

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
class Pet
{
    public:
        Pet(std::string PETNAME,
            std::string PETSPECIES,
                 float PETHEALTH,
                 float PETTHIRST,
                 float PETSTAMINA,
                 float PETAGE,
                 int PETHAPPINESS,
                 int PETHUNGER,
                 std::string PLAYERPETID,
                 bool PETISHAPPY,
                 bool PETISSAD,
                 bool PETISANGRY,
                 bool PETISTIRED,
                 bool PETISHUNGRY,
                 bool PETISSLEEPING,
                 bool PETISTHIRSTY,
                 bool PETISDEAD,
                 bool PETISDOG,
                 bool PETISCAT,
                 bool PETISMOUSE,
                 bool PETISHAMSTER,
                 bool PETISPARROT,
                 bool PETISTURTLE,
                 bool PETISGOLDFISH,
                 bool PETISMONKEY,
                 bool PETISMALE,
                 bool PETISFEMALE);
        ~Pet();

    std::string petName;
    std::string petSpecies;
    float petHealth;
    float petThirst;
    float petStamina;
    float petAge;
    int petHappiness;
    int petHunger;
    std::string playerPetID;

    //Pet stats

    bool petIsHappy;
    bool petIsSad;
    bool petIsAngry;
    bool petIsTired;
    bool petIsHungry;
    bool petIsSleeping;
    bool petIsThirsty;
    bool petIsDead;

    //Pet Information

    bool petIsDog;
    bool petIsCat;
    bool petIsMouse;
    bool petIsHamster;
    bool petIsParrot;
    bool petIsTurtle;
    bool petIsGoldfish;
    bool petIsMonkey;
    bool petIsMale;
    bool petIsFemale;

};
Last edited on
Your pet ultimately doesn't set any of those members in its constructor. To do that, you'd need to do something similar to this:
1
2
3
4
5
6
7
8
class AClass {
bool boringClass, degreeNeeds;
public:
    AClass(bool is_boring, bool degree_requirement) {
        boringClass = is_boring;
        degreeNeeds = degree_requirement;
    }
};


Is being a dog and being a cat mutually exclusive in your program? Additionally, is being happy and being angry mutually exclusive? If either is true, you may want to look into enums rather than have all those boolean parameters.

http://www.cplusplus.com/doc/tutorial/other_data_types/ (Scroll down a bit.)

-Albatross
"Your pet ultimately doesn't set any of those members in its constructor."

What do you mean by that? so no variables are being set? or just the boolean variables?

"Is being a dog and being a cat mutually exclusive in your program? Additionally, is being happy and being angry mutually exclusive?"

What do you mean by this? In the game you can only have one pet, you cant have 2 or play as one.

If that is all that you have for your class, then no member variables are being set. Of course, if you have an implementation file somewhere (which is generally a good practice rather than making a class in one file) and the member variable initialization is done there, then disregard what I said. :P

Is it possible for a pet to be a dog and a cat at the same time? Also, is it possible for a pet to be happy and angry at the same time?

-Albatross
To build upon what Albatross was saying, the only reason you would need all those bools is if a Pet can be, for example, both a cat and a dog, or both male and female at the same time.

Can i initialize my variables in the constructor?


That's what it's for. Let us help you put you on the right track concerning classes.
For someone who is familiar with classes, it's sometimes hard to come up with a simple example. I've tried to strip it down to the bare bones - but with a twist: inheritance - I think you could benefit from learning about it.

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
#include <iostream>
#include <string>

/*

In this example, I want a Cat class, and a Dog class.
Cats and Dogs are both pets, therefore, we can make a Pet class from which Cat and Dog with inherit properties (common members).

*/

class Pet {
public :
	Pet();//								Pet constructor with no parameters
	Pet(std::string _type);//				Pet constructor with one parameters
	std::string type, name;
	bool isDead;

	void setName(std::string _name);

};

Pet::Pet() {
	type = "Generic Pet";
	name = "Generic Name";
	isDead = false;
}

Pet::Pet(std::string _type) {
	type = _type;
	isDead = false;
}

void Pet::setName(std::string _name) {
	name = _name;
}




class Cat : public Pet {//Cat inherits from Pet
public :
	Cat() : Pet("Kitty") {}//					initializer list
	//With the use of an initializer list, you can specify and invoke a parent-class constructor.
	//In this case, I'm calling the Pet constructor which takes one parameter, which sets the type string.

};

class Dog : public Pet {//Dog inherits from Pet
public :
	Dog() : Pet("Doggie") {}//same thing for the Dog class, just a bit different.

};

int main(int argc, char* argv[]) {

	Cat cat;
	Dog dog;

	cat.setName("Mittens");
	dog.setName("Snowy");

	std::cout << cat.name << " the " << cat.type << " says hello!" << std::endl;
	std::cout << dog.name << " the " << dog.type << " says hello!" << std::endl;


	std::cin.get();
	return 0;
}


If you have any questions, I'll try to make my code more friendly, and provide explanations where necessary.
Sorry if the formatting is butchered.
Last edited on
Do i need to use setters? getters and setters are a thing im unsure of as people say there good and bad.
Last edited on
No, you do not. Replace line 56 with Cat cat("Mittens"); to get the same result as line 59. Hence, no need for setting unless you wish to change it.

EDIT: Actually, not quite how it would work. You need to create a Cat constructor that also includes a parameter for name. So just copy what is there for the Pet constructor when provided a type, but include the initializer list for calling the parent constructor. Then just replace type with name, and you're set. Setters can mostly be replaced with constructors. It is only when you need to change the values that you would actually need one. Hell, even having the user input the name doesn't require a setter- either have the input function be a part of the class, or create the object of the class after the name is inputted by the user- just pass the variable you dumped the inputted text in to instead of whatever is in the quotes. Ta-da, user-inputted names without setters.
Ok so i made my own classes based upon xism's class and here it is, am i doing it right?

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
#ifndef CHARACTERTRAIT_CLASS_H_INCLUDED
#define CHARACTERTRAIT_CLASS_H_INCLUDED

#include <string>

/*
Character Trait Class

CODE EXPLANATION

Confidence levels

20  = will probably run
40  = might stay and help out
60  = will think twice about running
80  = will only run when in danger
100 = will never run regardless of danger
*/

class CharacterTrait
{
    public:
        CharacterTrait();
        CharacterTrait(const std::string _traitName, int _baseStamina, int _baseConfidence);
        ~CharacterTrait();

        std::string characterName, traitName;
        int baseStamina;    //All characters have stamina
        int baseConfidence; //All characters will have some confidence

        void SetCharacterName(std::string _characterName);
};

class TraitHardWorker: public CharacterTrait
{
    public:
        TraitHardWorker(): CharacterTrait("Hard Worker", 100, 80){}

        bool isHardWorker;     //Weather or not the character has this trait as all traits are randomized
        int staminaModifier;
};

class TraitCoward: public CharacterTrait
{
    public:
        TraitCoward(): CharacterTrait("Coward", 100, 20){}

        bool isCoward;
        int staminaModifier;
};

class TraitEngineer: public CharacterTrait
{
    public:
        TraitEngineer(): CharacterTrait("Engineer", 100, 80){}

        bool isEngineer;
        int staminaModifier;
};


#endif // CHARACTERTRAIT_CLASS_H_INCLUDED


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
#include <iostream>
#include <vector>
#include "CharacterTrait_Class.h"

using namespace std;

CharacterTrait::CharacterTrait()
{
    characterName = "Name1";
    traitName = "SomeTrait";
    baseStamina = 100;
    baseConfidence = 60;
}

CharacterTrait::CharacterTrait(const string _traitName, int _baseStamina, int _baseConfidence)
{
    traitName = _traitName;
    baseStamina = _baseStamina;
    baseConfidence = _baseConfidence;
}

CharacterTrait::~CharacterTrait()
{

}

void CharacterTrait::SetCharacterName(string _characterName)
{

}

int main()
{
    vector<string> V_name;
    string name;

    TraitHardWorker thw;
    TraitCoward tc;
    TraitEngineer te;

    cout << "Enter Hard Worker Characters Name" << endl;
    getline(cin, name);

    thw.SetCharacterName(name);

    V_name.push_back(name);

    cout << "Enter Coward Characters Name" << endl;
    getline(cin, name);

    tc.SetCharacterName(name);

    V_name.push_back(name);

    cout << "Enter Engineer Characters Name" << endl;
    getline(cin, name);

    te.SetCharacterName(name);

    V_name.push_back(name);

    cout << "Engineers name is " << V_name[0] << " his base stamina is " << thw.baseStamina << " his base confidence is " << thw.baseConfidence << endl;
    cout << "Cowards Name is " << V_name[1] << " his base stamina is " << tc.baseStamina << " his base confidence is " << tc.baseConfidence << endl;
    cout << "Engineers name is " << V_name[2] << " his base stamina is " << te.baseStamina << " his base confidence is " << te.baseConfidence << endl;


    return 0;
}



so what your saying is that if i want a variable to be edited it needs to go in a setter? i thought thats what constructors were for.
bump
Maybe my usage of a setName() method was ill-advised.

You don't NEED getters and setters. There are certain design situations in which it would be wise to have getters and setters.

The following is perfectly valid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>

class Person {
public :
	Person() {name = "Generic Name";}

	std::string name;

};

int main(int argc, char* argv[]) {
	Person person;
	person.name = "Bob";

	return 0;
}


The following is perfectly valid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>

class Person {
public :
	Person() {name = "Generic Name";}
	Person(const std::string _name) {name = _name;}

	std::string name;

};

int main(int argc, char* argv[]) {
	Person person("Bob");

	return 0;
}


The following is perfectly valid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

class Person {
	std::string name;
public :
	Person() {name = "Generic Name";}

	void setName(const std::string _name) {name = _name;}

};

int main(int argc, char* argv[]) {
	Person person;

	person.setName("Bob");

	return 0;
}


When you become more comfortable with certain design schemes and classes in general, you'll slowly understand why and when you may want to prefer one way of doing it over another.

In addition, why doesn't your CharacterTrait class have a staminaModifier?
All the classes inheriting from it have their own, when the base class could just have it.
so like 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <fstream>
#include "CharacterTrait_Class.h"

using namespace std;

CharacterTrait::CharacterTrait()
{
    //These are just temp values right?

    characterName = "Name1";
    traitName = "SomeTrait";
    baseStamina = 100;
    baseConfidence = 60;
    staminaModifier = 30;
    conscienceLevel = 4;
    characterName = "SomeName";
}

CharacterTrait::CharacterTrait(const string _traitName, int _baseStamina, int _baseConfidence, int _staminaModifier, int _conscienceLevel, string _characterName)
{
    traitName = _traitName;
    baseStamina = _baseStamina;
    baseConfidence = _baseConfidence;
    staminaModifier = _staminaModifier;
    conscienceLevel = _conscienceLevel;
    characterName = _characterName;
}

CharacterTrait::~CharacterTrait()
{

}

void CharacterTrait::SetCharacterName(string _characterName)
{

}

void Save()
{
    ofstream saveFile;
    TraitHardWorker thw;
    TraitEngineer te;
    TraitCoward tc;

    saveFile.open("Test.txt");

    saveFile << thw.baseConfidence << endl;
    saveFile << thw.baseStamina << endl;
    saveFile << thw.characterName << endl;
    saveFile << thw.conscienceLevel << endl;

    saveFile << te.baseConfidence << endl;
    saveFile << te.baseStamina << endl;
    saveFile << te.characterName << endl;
    saveFile << te.conscienceLevel << endl;

    saveFile << tc.baseConfidence << endl;
    saveFile << tc.baseStamina << endl;
    saveFile << tc.characterName << endl;
    saveFile << tc.conscienceLevel << endl;
}

void Load()
{
    ifstream loadFile;
    TraitHardWorker thw;
    TraitEngineer te;
    TraitCoward tc;

    loadFile.open("Test.txt");

    loadFile >> thw.baseConfidence;
    loadFile >> thw.baseStamina;
    loadFile >> thw.characterName;
    loadFile >> thw.conscienceLevel;
    loadFile >> te.baseConfidence;
    loadFile >> te.baseStamina;
    loadFile >> te.characterName;
    loadFile >> te.conscienceLevel;
    loadFile >> tc.baseConfidence;
    loadFile >> tc.baseStamina;
    loadFile >> tc.characterName;
    loadFile >> tc.conscienceLevel;
}

int main()
{
    string name;
    srand(time(0));
    int conscienceLevelRand;
    int staminaModifierRand;

    conscienceLevelRand = rand() % 4;
    staminaModifierRand = rand() % 20;

    TraitHardWorker thw;
    TraitCoward tc;
    TraitEngineer te;

   cout << "Enter Hard Worker Characters Name" << endl;
    getline(cin, thw.characterName);

    thw.SetCharacterName(thw.characterName);
    cout << "Enter Coward Characters Name" << endl;
    getline(cin, tc.characterName);

    cout << "Enter Engineer Characters Name" << endl;
    getline(cin, te.characterName);


    cout << "Engineers name is " << te.characterName << " his base stamina is " << thw.baseStamina << " his base confidence is " << thw.baseConfidence << endl;
    cout << "Cowards Name is " << tc.characterName << " his base stamina is " << tc.baseStamina << " his base confidence is " << tc.baseConfidence << endl;
    cout << "hard workers name is " << thw.characterName << " his base stamina is " << te.baseStamina << " his base confidence is " << te.baseConfidence << endl;

    cout << "\n";


    //Randomize this so it either chooses to add or subtract from base stamina;
    staminaModifierRand += thw.baseStamina;
    thw.conscienceLevel = conscienceLevelRand;

    cout << "Hard Workers stamina modifier is: " << thw.baseStamina << endl; //Its not modifying stamina, its always 100
    cout << "Hard Workers conscience level is: " << thw.conscienceLevel << endl;

    //Randomize this so it either chooses to add or subtract from base stamina;
    staminaModifierRand += tc.baseStamina;
    tc.conscienceLevel = conscienceLevelRand;

    cout << "Cowards stamina modifier is: " << tc.baseStamina << endl; //Its not modifying stamina, its always 100
    cout << "Cowards conscience level is: " << tc.conscienceLevel << endl;

    //Randomize this so it either chooses to add or subtract from base stamina;
    staminaModifierRand += te.baseStamina;
    te.conscienceLevel = conscienceLevelRand;

    cout << "Engineers stamina modifier is: " << te.baseStamina << endl; //Its not modifying stamina, its always 100
    cout << "Engineers conscience level is: " << te.conscienceLevel << endl;

    Save();

    return 0;
}




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
#ifndef CHARACTERTRAIT_CLASS_H_INCLUDED
#define CHARACTERTRAIT_CLASS_H_INCLUDED

#include <string>

/*
Character Trait Class

CODE EXPLANATION

Confidence levels

20  = will probably run
40  = might stay and help out
60  = will think twice about running
80  = will only run when in danger
100 = will never run regardless of danger

Conscience Levels

The level of conscience in a character will determine how they act in certain situations

1 = Psychopathic
2 = Anti Social Personality
3 = Sociopathic
4 = Normal Person

*/

class CharacterTrait
{
    public:
        CharacterTrait();

        //Question, could all the ints go in an enum? that way when im looking at it in the constructors i can
        //see 3the names instead of a number because when i look at this: CharacterTrait("Hard Worker", 100, 80){}
        //The 100 and 80 are a little confusing as i ALWAYs have to look at the base class to see what they are.
        //If i can add them to an enum, how would i set that up in the class?
        CharacterTrait(const std::string _traitName,
                       int _baseStamina,
                       int _baseConfidence,
                       int _staminaModifier,
                       int _conscienceLevel,
                       std::string _characterName);
        ~CharacterTrait();

        std::string characterName, traitName;
        int baseStamina;    //All characters have stamina
        int baseConfidence; //All characters will have some confidence
        int staminaModifier; //Modifies stamina value to either give more or remove some stamina
        int conscienceLevel; //For Psychopaths and others lacking conscience

        void SetCharacterName(std::string _characterName);
};

class TraitHardWorker: public CharacterTrait
{
    public:
        TraitHardWorker(): CharacterTrait("Hard Worker", 100, 80, +34, 4, "GenericName"){}
        
        bool isHardWorker;     //Weather or not the character has this trait as all traits are randomized
};

class TraitCoward: public CharacterTrait
{
    public:
        TraitCoward(): CharacterTrait("Coward", 100, 20, -5, 4, "GenericName"){}

        bool isCoward;
};

class TraitEngineer: public CharacterTrait
{
    public:
        TraitEngineer(): CharacterTrait("Engineer", 100, 80, +50, 4, "GenericName"){}

        bool isEngineer;
};


#endif // CHARACTERTRAIT_CLASS_H_INCLUDED
Last edited on
also the name isnt outputting to the save file, but everything else is.
Help me understand exactly how you envision these classes to interact with one-another.
By looking at your classes, I'm having trouble coming up with a mental model of what these classes should do.
Here's what I think I've understood. Please correct anything that's wrong:

You wish to have a CharacterTrait class, which modifies attributes (like stamina) in a so-far non-existant Person class.

For instance, if a person has an athletic trait, that might raise their base stamina.
correct, i didnt make the person yet, so should the base class be the person? or should i make a person class and have CharacterTrait derive from it?
Well, let's establish the correct "has a" and "is a" relationships.

A person has Attributes
A person has Traits

An AthleticTrait is a trait
A SlowpokeTrait is a trait

Traits modify Attributes

Here's a hacky example:

trait.h
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
#ifndef _TRAIT_H_
#define _TRAIT_H_

/*Things like stamina, Conscience and Confidence are Attributes*/

class Attribute {
public :
	Attribute() {value = 0;}
	Attribute(signed short _value) : value(_value) {}

	signed short value;

};

class Trait {
public :
	Trait() {}

	std::string name;
	Attribute staminaMod;//Traits only affect stamina right now

};

class AthleteTrait : public Trait {
public :
	AthleteTrait() {name = "Athlete"; staminaMod.value = 50;}

};

class SlowpokeTrait : public Trait {
public :
	SlowpokeTrait() {name = "Slowpoke"; staminaMod.value = -50;}

};

#endif 


person.h
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
#ifndef _PERSON_H_
#define _PERSON_H_
#include "trait.h"
#include <string>

/*

A Person has Attributes, and a trait that affects those Attributes.
Right now, Traits only affect stamina.

*/

class Person {
public :
	Person() : stamina(50), conscience(50), confidence(50) {alloc=false;}
	~Person();

	std::string name;
	Attribute stamina, conscience, confidence;
	Trait* trait;//A person only has one trait so far
	bool alloc;

};

Person::~Person() {
	if(alloc) {
		delete trait;
		alloc=false;
	}
}

#endif 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "person.h"
#include "trait.h"

int main(int argc, char* argv[]) {
	Person bob, steve;
	bob.name = "Bob";
	steve.name = "Steve";

	bob.trait = new AthleteTrait();
	bob.alloc = true;

	steve.trait = new SlowpokeTrait();
	steve.alloc = true;

	std::cout << bob.name << " is a(n) " << bob.trait->name << " and has " << bob.stamina.value + bob.trait->staminaMod.value << " Stamina" << std::endl;
	std::cout << steve.name << " is a(n) " << steve.trait->name << " and has " << steve.stamina.value + steve.trait->staminaMod.value << " Stamina" << std::endl;
	std::cin.get();
	return 0;
}
Im going to start over and make a person class then a traits class, its hard for me to understand other peoples code, but i sort of get whats going on. Im going to use your example to make it.
Last edited on
im so confused, i dont know what to do anymore ive been trying to lear classes for 2 years now and i can NEVER get them right.

is this good so far:

its not done but what else do i need to do i want to try to keep things simple im confused enough. Id like to know, what is the way to make a class that can be used for most programming scenarios? i would like to stick with that, get used to it then move on to other ways.

character trait

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
#ifndef CHARACTERTRAIT_CLASS_H_INCLUDED
#define CHARACTERTRAIT_CLASS_H_INCLUDED

#include <string>
#include "Person_Class.h"

/*
Character Trait Class

CODE EXPLANATION

Confidence levels

20  = will probably run
40  = might stay and help out
60  = will think twice about running
80  = will only run when in danger
100 = will never run regardless of danger

Conscience Levels

The level of conscience in a character will determine how they act in certain situations

1 = Psychopathic
2 = Anti Social Personality
3 = Sociopathic
4 = Normal Person

*/

class CharacterTrait: public Character
{
    public:
        CharacterTrait(){}
        ~CharacterTrait();

        const std::string traitName;
        int modifyBaseStamina;
        int modifyBaseShootingSkill;
};

class ShootingTrait: public CharacterTrait
{
    public:
        ShootingTrait(){traitName = "Shooting"; modifyBaseShootingSkill = 50;}
};
#endif // CHARACTERTRAIT_CLASS_H_INCLUDED


person class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef PERSON_CLASS_H_INCLUDED
#define PERSON_CLASS_H_INCLUDED

#include <string>

class Character
{
    public:
        Person();
        Person();
        ~person();

        std::string characterName;
        int baseStamina;
        int baseShootingSkill;

        void setName(std::string _characterName);
};

#endif // PERSON_CLASS_H_INCLUDED


Last edited on
ok so i was watching a video on classes and stuff so i have 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
#include <iostream>
#include "Person_Class.h"

using namespace std;

int main()
{
    Monster Godzilla;

    Godzilla.LifeMutator(500);
    Godzilla.MSizeMutator(1000);
    Godzilla.WeightMutator(5000.8);
    Godzilla.MonsterNameMutator("Godzilla");

    Godzilla.DisplayStats();

    //The heap

    Monster * Mothra = new Monster();
    Mothra->LifeMutator(450);
    Mothra->MSizeMutator(3000);
    Mothra->WeightMutator(1000);
    Mothra->MonsterNameMutator("Mothra");

    Mothra->DisplayStats();
    delete Mothra;

    return 0;
}




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
#ifndef PERSON_CLASS_H_INCLUDED
#define PERSON_CLASS_H_INCLUDED

#include <string>
#include <iostream>
/*
class Person
{
    public:
        unsigned ageAccessor() const{return age;} //Accessors are almost always constant.
        void ageMutator(unsigned a){age = a;}
    private:
        unsigned age;
        std::string name;
};
*/


class Monster
{
    public:
        Monster(){std::cout << "Building monster." << std::endl;}
        ~Monster(){std::cout << "Destroying Monster." << std::endl;}

    //Member methods
    void Rampage(){std::cout << "ff1" << std::endl;}
    void DisplayStats()
    {
        std::cout << "Monster Stats\n" << std::endl;

        std::cout << "Name: " << monsterName << std::endl;
        std::cout << "Life: " << life << std::endl;
        std::cout << "Size: " << msize << std::endl;
        std::cout << "Weight: " << weight << std::endl;
    }

    //Accessor Methods
    int LifeAccessor(){return life;};
    void LifeMutator(int _life){life = _life;}

    double MSizeAccessor(){return msize;}
    void MSizeMutator(double _msize){msize = _msize;}

    double WeightAccessor(){return weight;}
    void WeightMutator(double _weight){weight = _weight;}

    std::string MonsterNameAccessor(){return monsterName;}
    void MonsterNameMutator(std::string _monsterName){monsterName = _monsterName;}

    private:
        int life;
        double msize;
        double weight;
        std::string monsterName;
};
#endif // PERSON_CLASS_H_INCLUDED 


Now this has getters and setters, i call them accessors and mutators, but same thing. Now i know that using setters and getters all the time is bad so what would i use in place of them? I believe that issue was mentioned once in this thread but i would like to go into detail on that, when to use getter and settors with a simple example and when to use whatever else with a simple example. Also in the video he said using the -> operator to access information from the heap is much better to do because putting it on the stack all the time can make the program slow if theres alot of information, or if the objects are big.
bump.
Topic archived. No new replies allowed.