gurkin gurkin - 3 months ago 6
C++ Question

how to organise the classes so i can call the one class to use both objects

i want to be able to call just one region and have access to choosing both TAvark (town Avark) and Forest so i can use while to check if the user inputs the correct text into the cin and still allow it to go back to the previous point such as going from the Forest back to Town but as it is currently you can't do that.

namespace areas {

class TAvark {
public:
string Avark;
int Blacksmith;
int Merchant;
int Magic;
int NPC_quest;

};
class Forest{
public:
string RForest;
string Search;
string Return;
int heal;
using namespace areas;
TAvark TAvarkregion;
Forest Forestregion;
TAvarkregion.Avark;
Forestregion.RForest;
}
}
while (TAvarkregion.Avark != "Town" & TAvarkregion.Avark != "town" & Forestregion.RForest != "Forest" & Forestregion.RForest != "forest")
if (Charactergender.gender == "Male" & Charactergender.gender == "male")
cout << "Where would you like to go Sir " << Charactername.name << endl << endl;
else cout << "Where would you like to go Madam" << Charactername.name << endl << endl;
cout << "the Forest or the Town";
cin <<


Sleep(2000);
};


here is all of my code if it is required

#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>

using namespace std;

class mainhand {
int serratedblade;
int steelsword;
int cutless;
int falchion;
int staff;
int iron_dagger;
int enchanted_oak_staff;
int wand;
int enchanted_steel_dagger
};

class offhand {
int wooden_shield;
int off_wand;
int off_steel_dagger;
int off_iron_dagger;
int tower_shield;
};

class chest {
int leather_chest;
int iron_chest;
int robes;
int tunic;
int plated_mail;
int woven_mantle;

};


class helmet {
int horned_helmet;
int wizard_hat;
int iron_helmet;
int full_helm;
int hood;
};

namespace areas {

class TAvark {
public:
string Avark;
int Blacksmith;
int Merchant;
int Magic;
int NPC_quest;

};
class Forest{
public:
string RForest;
string Search;
string Return;
int heal;

class Goblin {
int health = rand() % 50 + 20;
int damage = rand() % 15 + 8;
int armor = rand() % 6 + 4;
int goldR = rand() % 30 + 5;
int experience = rand() % 60 + 40;
string serratedblade;
};

class spider {
int health = rand() % 50 + 20;
int damage = rand() % 15 + 8;
int armor = rand() % 6 + 4;
int goldR = rand() % 30 + 5;
int experience = rand() % 60 + 40;
};

class Haunted_Deer {
int health = rand() % 50 + 20;
int damage = rand() % 15 + 8;
int armor = rand() % 6 + 4;
int goldR = rand() % 30 + 5;
int experience = rand() % 60 + 40;
};

class Witch {
int health = rand() % 50 + 20;
int damage = rand() % 15 + 8;
int armor = rand() % 6 + 4;
int goldR = rand() % 30 + 5;
int experience = rand() % 60 + 40;
};

class ghost {
int health = rand() % 50 + 20;
int damage = rand() % 15 + 8;
int armor = rand() % 6 + 4;
int goldR = rand() % 30 + 5;
int experience = rand() % 60 + 40;
};

class bear {
int health = rand() % 50 + 20;
int damage = rand() % 15 + 8;
int armor = rand() % 6 + 4;
int goldR = rand() % 30 + 5;
int experience = rand() % 60 + 40;
};
class boss {
int health = rand() % 50 + 20;
int damage = rand() % 15 + 8;
int armor = rand() % 6 + 4;
int goldR = rand() % 30 + 5;
int experience = rand() % 60 + 40;
};
};


};


class Character {
public:
string name;
string gender;
string type;
string faction;
int strength;
int agility;
int intelligence;
int level;
int health;
int experience;
int mainweapon;
int offhand;
int chest;
int helmet;
int gold;

};


int main()
{
Character Charactername;
Character Charactergender;
Character Charactertype;
Character Characteragility;
Character Characterstrength;
Character Characterintelligence;
Character Characterfaction;
string done;

while (done != "Y" & done != "y") {
cout << "what is your name?\n";
cin >> Charactername.name;
while (Charactergender.gender != "Male" & Charactergender.gender != "Female" & Charactergender.gender != "male" & Charactergender.gender != "female") {
cout << "Male or Female\n";
cin >> Charactergender.gender;
if (Charactergender.gender != "Male" & Charactergender.gender != "Female" & Charactergender.gender != "male" & Charactergender.gender != "female")
cout << "you have entered an invalid option\n";
}

while (Charactertype.type != "Warrior" & Charactertype.type != "warrior" & Charactertype.type != "Mage" & Charactertype.type != "mage" & Charactertype.type != "Rogue" & Charactertype.type != "rogue") {
cout << "What class are you, Warrior, Mage or Rogue\n";
cin >> Charactertype.type;

if (Charactertype.type == "Warrior" || Charactertype.type == "warrior")
Characteragility.agility = 6, Characterstrength.strength = 10, Characterintelligence.intelligence = 4;
else if (Charactertype.type == "Mage" || Charactertype.type == "mage")
Characteragility.agility = 4, Characterstrength.strength = 6, Characterintelligence.intelligence = 10;
else if (Charactertype.type == "Rogue" || Charactertype.type == "rogue")
Characteragility.agility = 10, Characterstrength.strength = 4, Characterintelligence.intelligence = 6;
else {
cout << "you have entered an invalid name\n";
}
}
while (Characterfaction.faction != "Good" & Characterfaction.faction != "good" & Characterfaction.faction != "Bad" & Characterfaction.faction != "bad" & Characterfaction.faction != "Neutral" & Characterfaction.faction != "neutral") {
cout << "what faction are you in\n" << "Good\n" << "Bad\n" << "Neutral\n";
cin >> Characterfaction.faction;
}
cout << "your name is " << Charactername.name << endl << " your gender is " << Charactergender.gender << endl << " your class is " << Charactertype.type << endl << " and your faction is " << Characterfaction.faction << endl << " are you sure you want to continue with these choices" << endl << "press Y to continue or any key to go back to the start ";
cin >> done;
}
cout << "Welcome to the Elysium " << Charactername.name << endl;
Sleep(5000);

Character Characterhealth;
Characterhealth.health = 100;
Character Charactergold;
Charactergold.gold = 0;
Character Characterlevel;
Characterlevel.level = 1;
Character Characterexperience;
Characterexperience.experience = 0;

using namespace areas;
TAvark TAvarkregion;
Forest Forestregion;
TAvarkregion.Avark;
Forestregion.RForest;

while (TAvarkregion.Avark != "Town" & TAvarkregion.Avark != "town" & Forestregion.RForest != "Forest" & Forestregion.RForest != "forest")
if (Charactergender.gender == "Male" & Charactergender.gender == "male")
cout << "Where would you like to go Sir " << Charactername.name << endl << endl;
else cout << "Where would you like to go Madam" << Charactername.name << endl << endl;
cout << "the Forest or the Town";
cin <<


Sleep(2000);
};

Answer

What you use in C++ is inheritance. You define a base class :

class Region {
 // common parts
};

and then you say that both towns and forests are regions:

class Town : public Region { ...
class Forest : public Region { ...

and then you say that Avark is an object of type Town :

Town Avark; // There could be other towns.
Comments