How to stop money from going over

Pages: 12345... 7
main

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "test.h"
#include "Player.h"

using namespace std;

int main()
{
    Player pc;

    pc.start();
}


Test_Class

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

#include <string>
#include <iostream>

class Player
{
    public:
        Player();
        Player(int hp, std::string name);
        ~Player();
        void start();
        void output();

    private:
        int plr_health;
        std::string plr_name;
};

Player::Player(): plr_name("player1")
{
}

Player::Player(int hp, std::string name) : plr_health( hp ),
                                           plr_name( name )
                                           {              }

Player::~Player()
{

}

void Player::output()
{
    std::cout << "Name - " << plr_name << std::endl << "health - " << plr_health << std::endl;
}

#endif // TEST_CLASS_H_INCLUDED 


test

1
2
3
4
5
6
7
8
9
10
11
#include "Test_Class.h"
#include <iostream>
#include <string>

using namespace std;

void Player::start()
{
    cout << "This is the beginning" << endl;

}


Player_Source

1
2
3
4
5
6
#include <iostream>
#include "Test_Class.h"

using namespace std;

Your problem is that line 21 - 37 in test_class.h should be in your test_class.cpp and not header file.

Also in your main.cpp you are calling the constructor without an overload

If you want to call the ovelroaded one do something like
Player pc( 100 , "Giblit" );
hey giblit now you can teach me something...

your code looks like this.

Player::Player(): plr_name("player1")

what does using ":" instead of "{ some stuff }" mean?
^ im interested as well as i kind of know as someone told me once but i forget as i really never used classes. Also i got everything working, now ill keep trying to get the other code to work.
Last edited on
The colon means you are starting the initialization list I sent a link earlier that explains it better. http://www.cprogramming.com/tutorial/initialization-lists-c++.html

1
2
3
4
5
6
7
8
9
Player::Player( int hp , std::string name ) : plr_health( hp ) , plr_name( name ){}

//is almost equivilant to 

Player::Player( int hp , std::string name )
{
   plr_health = hp;
   plr_name = name;
}


The only difference is that assigning a value to them and not initializing leads to a temporary undefined variable and say you want a constant variable called "Faction" or something like that you can not do that with out initializing.

This works.
1
2
3
4
5
6
7
8
9
10
//.h
class Player
{
    public:
        Player( const std::string faction );
    private:
        const std::string faction;
};
//.cpp
Player::Player( const std::string faction ) : faction( faction ){}


while this does not
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//.h
class Player
{
    public:
        Player( const std::string faction );
    private:
        const std::string faction;
};

//.cpp
Player::Player( const std::string faction )
{
    this->faction = faction;
}
//You can not reassign a value to a constant variable you can only initialize them. 
ok i see, now i have this line in main.cpp

 
Player pc(100, "default");


but i dont really plan on writing code in main, im only going to use it to initialize the rest of the game so can i just delete whats in the parameters without any problems? also i have 2 constructors is that necessary or not?

1
2
3
4
5
6
7
Player::Player(): plr_name("player1")
{
}

Player::Player(int hp, std::string name) : plr_health(hp),
                                           plr_name(name)
                                           {              }


You don't need parameters but they are helpful if you know what the starting values are going to be. And no you don't need two constructors but overloads are always nice to have =p
ok, well i dont need to initiaqlize them in main do i? i can just copy paste the pc(100, "name") in a different function and use it right or do i have to initialize it in main?
You can initialize anywhere you are using it just don't forget to put #include "class_name.h"
So is thisw all i need to do basically is initialize variables 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
#include "Test_Class.h"
#include <iostream>
#include <string>

using namespace std;

Player::Player(): plr_name("player1")
{
}

Player::Player(int hp, std::string name) : plr_health(hp),
                                           plr_name(name)
                                           {              }

Player::~Player()
{

}

void Player::start()
{
    cout << "This is the beginning" << endl;
    getline(cin, plr_name);

    cout << "Name - " << plr_name << endl;
}
Yeah you can do that.
I suggest initializing all data members in all of your constructors, so you don't end up with junk values. If you used your default constructor, the player's hp could be any value.
initialize them? i thought i did that?
I believe he means on the default constructor also. You only initialize the name not the health so health has a jungle value.
but wont it be the same as the other constructor? why have two identical constructors?
i just have to confess. i m suprised that you never used classes @Ch1156. even though i began one month ago, even i try to work with classes which helps you keep things seperated and organized. i m suprised someone as experienced(where did i get that well you have 1000+ messages) as you never used classes before.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
Player::Player()
   : plr_name("player1")
{} //You don't assign a value to plr_health, so what would its value be?

//Revision

const int default_health(100); //<--Should probably make a static constexpr member of Player

Player::Player()
   : plr_health(default_health)//Yay, now we know plr_health for sure
   , plr_name("player1")
{}


You can always cout the player's health when you use your default constructor to see what I mean.

Edit:
@Ceset Post count means absolutely nothing.
Last edited on
Well i just forgot to put health there, so something 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
#include "Test_Class.h"
#include <iostream>
#include <string>

using namespace std;

Player::Player(): plr_health(100),
                  plr_name("player")
{
}

Player::Player(int hp, std::string name) : plr_health(hp),
                                           plr_name(name)
                                           {              }

Player::~Player()
{

}

void Player::start()
{
    cout << "This is the beginning" << endl;
    getline(cin, plr_name);

    cout << "Name - " << plr_name << endl;
}


I think i might get it a little better but im still sort of confused and i want to make sure i understand colasses once andf for all, i mean pointers are easier to understand than this for me but i digress. So in the first constructor i declare what the values are and in the second constructor i assign them to a variable? also i didnt know you could have more than one constructor, is that even needed?
Think of your constructors as specifications, like ordering a regular cup of coffee vs. ordering one with skim milk, tons of sugar, etc.

The first constructor that takes no parameters (usually called the default constructor) creates a default player. No customization.
The second one allows you to specify what the player name is and how much health he/she has.

1
2
3
4
Player
      player, //Makes the default player; calls first constructor
      player2(1200, "Bossman") //Uses second constructor to make a custom object
;


And yes, it is perfectly fine to have multiple constructors. Remember what overloading is.

Now, is it needed? That depends on your design. You could compress your two constructors into a single on (also called a default constructor because you don't have to give it parameters:
1
2
3
4
5
6
7
8
9
class Player{
   public:
      Player(int hp = default_health, string name = default_name);
};
//...
Player::Player(int hp, string name)
   : plr_health(default_health)
   , plr_name(name)
{}


Now you call this constructor thrice in the following code snippet:
1
2
3
4
5
Player
   player1, //make the default player
   player2(1200), //Specify health but use default name
   player3(110, "Bobo") //Completely custom player object
;
Ok so i deleted the other constructor, the empty one as i dont believe i needed it. So what is an example of a time i would need to use 2 constructors in the same class?

here is my modified code

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

#include <string>
#include <iostream>

class Player
{
    public:
        Player(int hp, std::string name);
        ~Player();
        void start();

    private:
        int plr_health;
        std::string plr_name;
};

#endif // TEST_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
#include "Test_Class.h"
#include <iostream>
#include <string>

using namespace std;

Player::Player(int hp, std::string name) : plr_health(hp),
                                           plr_name(name)
                                           {              }

Player::~Player()
{

}

void Player::start()
{
    cout << "This is the beginning" << endl;
    cout << "Current player name " << plr_name << endl;
    getline(cin, plr_name);

    cout << "New Name - " << plr_name << endl;
    cout << "Health " << plr_health << endl;
}


now if i write the health and name data to a file then load the data from the file the class wont overwrite it with the default data right?

I have this and it seems to work but tell me if im wrong

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
#include "Test_Class.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

Player::Player(int hp, std::string name) : plr_health(hp),
                                           plr_name(name)
                                           {              }

Player::~Player()
{

}

void Player::load()
{
    ifstream fileIn;
    fileIn.open("file.txt");

    fileIn >> plr_name;
    fileIn >> plr_health;
}

void Player::save()
{
    ofstream fileOut;
    fileOut.open("file.txt");

    fileOut << plr_name << endl;
    fileOut << plr_health << endl;
}

void Player::start()
{
    load();
    cout << "This is the beginning" << endl;
    cout << "Current player name " << plr_name << endl;
    getline(cin, plr_name);

    cout << "New Name - " << plr_name << endl;
    cout << "Health " << plr_health << endl;

    save();
}
Last edited on
Pages: 12345... 7