Getting string variables from main into constructor/deconstructor
Feb 21, 2018 at 7:29pm UTC
I've been messing about with constructors and deconstructors, and I'm quite certain it's possible but I am unsure of how, to send variables to a class constructor/deconstructor. I will also say that the program is far from complete, which is why there is no #include<cst.lib> /etcetera for the random ints I used in the program, and the reaosn there are five if() statements evaluating the randInt. The problem I have, and I've set the randInt to 1 for now, is that whenever that runs, outputting the (monster object name).monsterName is impossible within the constructor I used below.
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
#include<iostream>
#include<string>
using namespace std;
class monster
{
public :
monster()
{
cout << "A " << monsterName << " approaches!" << endl;
} //On the above line I want the monsterName to be output, but it's blank.
//How can I let this constructor here know what the string should be?
~monster()
{
cout << "The " << monsterName << " is destroyed!" << endl;
}
float health;
float damage;
int defense;
int level;
string monsterName;
string monsterAttack;
};
class player
{
public :
int level;
int classType;
float damage;
float health;
int defense;
};
int main()
{
player mainChar;
mainChar.level = 1;
cout << "Hello, welcome to Dungeons and Dragons!" << endl;
cout << "Be ready to pick a class. You can choose warrior, archer, or mage \nType 1 for warrior, 2 for archer, 3 for mage" << endl;
cout << "Classtype: " << endl;
cin >> mainChar.classType;
bool playingGame = true ;
while (playingGame == true )
{
if (mainChar.classType == 1)
{
mainChar.health = 30 * mainChar.level * 1.05;
mainChar.damage = 15 * mainChar.level * .66;
mainChar.defense = 10 * mainChar.level * .75;
}
if (mainChar.classType == 2)
{
mainChar.health = 15 * mainChar.level;
mainChar.damage = 12 * mainChar.level * .77;
mainChar.defense = 8 * mainChar.level * .5;
}
if (mainChar.classType == 3)
{
mainChar.health = 15 * mainChar.level * .9;
mainChar.damage = 25 * mainChar.level * .80;
mainChar.defense = 6 * mainChar.level * .5;
}
int monsterInt = 1;
if (monsterInt == 1)
{
monster dragon;
dragon.level = mainChar.level;
dragon.health = 60 * dragon.level;
dragon.damage = 5 * 1.1 * dragon.level;
dragon.defense = 5 * 1.2 * dragon.level;
dragon.monsterName = "Dragon" ;
dragon.monsterAttack = "The dragon breathes fire!" ;
}
if (monsterInt == 2)
{
monster dragon;
dragon.level = mainChar.level;
dragon.health;
dragon.damage;
dragon.defense;
dragon.monsterName = "Dragon" ;
dragon.monsterAttack = "The dragon breathes fire!" ;
}
if (monsterInt == 3)
{
monster dragon;
dragon.level = mainChar.level;
dragon.health;
dragon.damage;
dragon.defense;
dragon.monsterName = "Dragon" ;
dragon.monsterAttack = "The dragon breathes fire!" ;
}
if (monsterInt == 4)
{
monster dragon;
dragon.level = mainChar.level;
dragon.health;
dragon.damage;
dragon.defense;
dragon.monsterName = "Dragon" ;
dragon.monsterAttack = "The dragon breathes fire!" ;
}
if (monsterInt == 5)
{
monster dragon;
dragon.level = mainChar.level;
dragon.health;
dragon.damage;
dragon.defense;
dragon.monsterName = "Dragon" ;
dragon.monsterAttack = "The dragon breathes fire!" ;
}
}
}
Last edited on Feb 21, 2018 at 7:32pm UTC
Feb 21, 2018 at 7:44pm UTC
Constructors can take variables.
1 2 3 4 5
monster(string input)
{
monsterName = input;
cout << "A " << monsterName << " approaches!" << endl;
}
called as:
1 2
string inputName("beans" );
monster a_new_monster(inputName);
Feb 21, 2018 at 7:47pm UTC
But if you look in the main area, the if evaluation is what is intended to define the monster::monsterName, not user input; The monster is supposed to be random.
Feb 21, 2018 at 7:54pm UTC
What user input? Your code already contains the strings you want to use. You just need to pass them into the constructor.
1 2 3 4 5 6 7 8 9 10
if (monsterInt == 5)
{
string randomName("Dragon" );
monster dragon(randomName);
dragon.level = mainChar.level;
dragon.health;
dragon.damage;
dragon.defense;
dragon.monsterAttack = "The dragon breathes fire!" ;
}
Last edited on Feb 21, 2018 at 7:55pm UTC
Feb 22, 2018 at 3:21pm UTC
Nevermind I blanked and then realized you were telling me that the constructor could use string vars like functions can.
Topic archived. No new replies allowed.