^ 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.
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.
//.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.
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?
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?
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.
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.
Player::Player()
: plr_name("player1")
{} //You don't assign a value to plr_health, so what would its value be?
//Revision
constint 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.
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