How to make it work ?

Posts 1–4 of 4 · Page 1 of 1
How to make it work ?
Hello ! So I was practicing class and stuff like that , and I was wondering how to make this work :

Code:
#include <iostream>

using namespace std;

class User
{
private:
	int hp, mana;
public:
	void showstat();
	User();
	User(int, int);
};

User::User() : hp(100), mana(200)
{

}

User::User(int a, int b)
{
	hp = a;
	mana = b;
}
void User::showstat()
{
	cout << "Hp : " << hp << endl;
	cout << "Mana : " << mana << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	User Jack;
	User Jo(200, 500);
	Jack.showstat();
	Jo.showstat();
	return 0;
}
I wonder how to make it so when I declare a class , like this :
Code:
User kenny;
User Jo(200, 500);
If I want to make a new class , how to make it so I can only call 1 function ? Like , make a new User with modified mana but default HP ?

Code:
User kenny( , 500 )
? I want to change only the mana but keep the default HP for the new user :/ Any help please ?
If I understood your question correctly you would do something like this:

Code:
User::User(int b, int a = default_value)
{
}
If you don't specify a value for a then the call would look like this:

Code:
User user(value_for_b);
And it would set the value for the second argument (a) to the default value specified.

Google default parameter/argument and you'll understand why I placed a second in the argument list.
add a function setMana and setHp, initialize a User with default values and then call setHp(new value).
Alright thanks
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us