Accessing variables in base class C++ - c++

Hi this is the header file for my base class Ranger, and in it I have protected variables fov_, usb_ ... that I wish to access with my getter functions, I have three child classes on this one.
Ranger.h
#ifndef RANGER_H
#define RANGER_H
using namespace std;
class Ranger
{
//private contructor prevents contruction of base class
Ranger();
public:
void setBaud(int baud);
virtual void setFOV(int fov) = 0;
void setSamp(int sam);
int getFOV();
int getBaud();
int getMaxRange();
int getUSB();
protected:
//protected variables that are each indivdualy owned by each sensor
int fov_;
int maxRange_;
int usb_;
int baud_;
int samp_;
double data[];
//protected contructors for the child classes to use to set fixed parameters
Ranger(int fov, int maxRange, int port);
Ranger(int maxRange, int port);
};
#endif // RANGER_H
This is my cpp file for the base class that includes the getter files, it just has a return of the portected variables.
Ranger::Ranger()
{
}
Ranger::Ranger(int fov, int maxRange, int port)
{
fov_ = fov;
maxRange_ = maxRange;
usb_ = port;
}
Ranger::Ranger(int maxRange, int port)
{
maxRange_ = maxRange;
usb_ = port;
}
void Ranger::setBaud(int baud)
{
switch(baud)
{
case 0: baud_ = 38400; break;
case 1: baud_ = 115200; break;
default: baud_ = 38400; break;
}
}
void Ranger::setSamp(int sam)
{
samp_ = sam;
}
int Ranger::getFOV()
{
return fov_;
}
int Ranger::getBaud()
{
return baud_;
}
int Ranger::getMaxRange()
{
return maxRange_;
}
int Ranger::getUSB()
{
return usb_;
}
And in my main I want to access the protected variables from the base class to prevent re writting code, so each childs variables are protected in the base class. I try to access these by las.getFOV() but I get a segmentation fault error meaning I don't have access to them, and I don't quite understand why.
main.cpp
int main( int argc, char ** argv)
{
Laser las;
int baud;
cout << "Baud:" << endl;
cout << "0 - 38400" << endl;
cout << "1 - 115200" << endl;
cin >> baud;
las.setBaud(baud);
cout << "Baud for Lazer sensor is "+las.getBaud() << endl;
cout << "Lazer sensor created..." << endl;
cout << "Lazer's FOV: " + las.getFOV() << endl;
cout << "Lazer's Max Range: " + las.getMaxRange() << endl;
cout << "Lazer's Port: " + las.getUSB() << endl;
Radar rad;
int baud2;
cout << "Baud:" << endl;
cout << "0 - 38400" << endl;
cout << "1 - 115200" << endl;
cin >> baud2;
rad.setBaud(baud2);
cout << "Baud for Radar sensor is "+rad.getFOV() << endl;
int fov;
cout << "Feild of View Of Radar:" << endl;
cout << "0 - 20 degrees" << endl;
cout << "1 - 40 degrees" << endl;
cin >> fov;
rad.setFOV(fov);
cout << "FOV is set to " + rad.getFOV() << endl;
cout << "Radar sensor created..." << endl;
cout << "Radar's FOV: ' " + rad.getFOV() << endl;
cout << "Radar's Max Range: " + rad.getMaxRange() << endl;
cout << "Radar's Port: " + rad.getUSB() << endl;
Sonar son;
//rad.setFOV(user);
}
and here is one of the child class's cpp file for reference (Lazer)
laser.cpp
#include "laser.h"
Laser::Laser() : Ranger(180,8,0)
{
};
void Laser::setFOV(int fov)
{
fov_ = fov;
}
laser.h
#ifndef LASER_H
#define LASER_H
#include "ranger.h"
#include "rng.h"
class Laser : public Ranger
{
public:
Laser();
void setFOV(int fov);
};
#endif // LASER_H

Thanks everyone who commented, I understand I put way too much code to help you guys out, sorry about that I'll know for next time, and thankyou to letting me know the difference between the errors, I've done more research and found that the issue was when I was printing it out you can't use operators like:
cout<<""+function()<<endl;
Instead you need to separate the functions from the array like so:
cout<<""<<function()<<endl;
Thanks guys.

Related

Semantic error in the selection condition of the type of LED lamps

I have a class of light bulbs. There are methods and constructors in this class. There is even a destructor) The problem is that I have to determine and display information about class members with type "n" in the TEST() method (LED lamps).
To implement this task, he developed the gettype() method, which returns the type of an object, and, in fact, the TEST() method, which displays information about light bulbs.
The problem is that nothing works for me. I tried a lot of things, but it doesn’t work out for me to implement this task. I'm new to programming (
Code:
#include <iostream>
using namespace std;
class lamp
{
public:
// methods
void TEST(void);
char* gettype (void);
void INIT(void);
void SHOW(void);
// construcrors
lamp();
lamp(const char *t, int powe, const char *c, double cos);
lamp(const lamp & obj);
// destructor
~lamp();
private:
// data
char type[100]; // LED, energy-saving or incandescent lamp
int power; // LED lamp - "n"
char color[100];
double cost;
};
lamp::lamp() {
cout << "This object was created in the default constructor.\n";
strcpy(type, "");
power = 0;
strcpy(color, "");
cost = 0;
}
lamp::lamp(const char *t, int powe, const char *c, double cos) {
cout << "This object was created in the constructor with parameters.\n";
strcpy(type, t); //*t
power = powe;
strcpy(color, c); //*c
cost = cos;
}
lamp::lamp(const lamp & obj) {
cout << "This object was created in the copy constructor.\n";
strcpy(type, obj.type);
power = obj.power;
strcpy(color, obj.color);
cost = obj.cost;
}
lamp::~lamp() {
cout << "Deletion of object by destructor.\n";
}
void lamp::SHOW(void) {
cout << "Lamp Information:\n";
cout << "\nType > " << type;
cout << "\nPower > " << power;
cout << "\nColor > " << color;
cout << "\nCost > " << cost << endl;
}
void lamp::INIT(void) {
cout << "Enter lamp information:\n";
cout << "\nType (if LED, then n) > "; cin >> type;
cout << "\nPower > "; cin >> power;
cout << "\nColor > "; cin >> color;
cout << "\nCost > "; cin >> cost;
}
char* lamp::gettype (void) {
return type;
}
void lamp::TEST(void) {
cout << "\nType > " << type;
cout << "\nPower > " << power;
cout << "\nColor > " << color;
cout << "\nCost > " << cost << endl;
}
void main() {
setlocale(0, "");
// default constructor for 1 class instance
lamp l1;
cout << "Entering data for the first product." << endl;
l1.INIT();
// constructor with parameters for 2 class instances
cout << endl << "Information about the second object: \n";
lamp l2("n", 950, "yellow", 1580);
// copy constructor for the third object
cout << endl << "Information about the third object: \n";
lamp l3(l2);
// Derived information about all the lamps using the method SHOW
l1.SHOW();
l2.SHOW();
l3.SHOW();
// I create an array of two objects using the default constructor
lamp la[2];
I enter data into an array of objects using the method INIT
cout << "Fill an array of objects with 2 elements." << endl;
for(int i = 0; i < 2; i++) {
la[i].INIT();
}
// I output data from an array of objects using the method SHOW
cout << "Showing items." << endl;
for (int i = 0; i < 2; i++) {
la[i].SHOW();
}
// looking for and displaying information about LED lamps
cout << "Search and display information about LED lamps." << endl;
for (int i = 0; i < 3; i++) {
if (la[i].gettype() == "n") {
cout << endl << " lamp number : " << (i + 1) << endl;
la[i].TEST();
cout << endl;
}
}
system("pause");
}
There are several errors in your code:
strcpy is included in <cstring> which is missed. You need to add it in the beginning:
#include <cstring>
main() function should be declared as int main() and you need to add a return statement
int main() {
//YOUR CODE HERE
return 0;
}
You missed a comment sign at line 104
lamp la[2];
//I enter data into an array of objects using the method INIT
cout << "Fill an array of objects with 2 elements." << endl;
After fixed, your code should be able to run.

C++ Using array and for loop to output from multiple classes

I am asked to do this code and i need to use array or something similar to print out different classes. The only way i know is individually doing every single class is there a faster way of doing this. Following is the way i am using at the moment.
Ground_Transport Gobj;
Air_Transport Aobj;
Sea_Transport Sobj;
Car Cobj;
Train Tobj;
Bus Bobj;
Gobj.estimate_time();
Gobj.estimate_cost();
cout << Gobj.getName() << endl;
Bobj.estimate_time();
Bobj.estimate_cost();
cout << Bobj.getName() << endl;
Sobj.estimate_time();
Sobj.estimate_cost();
cout<<Sobj.getName()<<endl;
Aobj.estimate_time();
Aobj.estimate_cost();
cout << Aobj.getName() << endl;
Cobj.estimate_time();
Cobj.estimate_cost();
cout << Cobj.getName() << endl;
Tobj.estimate_time();
Tobj.estimate_cost();
cout << Tobj.getName() << endl;
Transport_KL_Penang Kobj;
cout << Kobj.getName() << endl;
This is the header file Transport_KL_Penang
#include <iostream>
#include <string>
using namespace std;
class Transport_KL_Penang
{
public:
Transport_KL_Penang() {}
virtual string getName() {
return Name;
}
int Time_in_hours1 ;
int Time_in_hours2 ;
int Cost_in_RM1 ;
int Cost_in_RM2 ;
void estimate_time() ;
void estimate_cost() ;
private:
static string Name;
};
void Transport_KL_Penang::estimate_time()
{
cout << "It takes " << Time_in_hours1 << "-" << Time_in_hours2 <<
" hours if you use " << Name << endl;
}
void Transport_KL_Penang::estimate_cost()
{
cout << "It will cost around " << Cost_in_RM1 << "-" << Cost_in_RM2 <<
"RM if you use " << Name << endl;
}
If you don't need a specific object name, you can write something as a code below, creating a multiples generics objects:
#include <iostream>
#include <cstdlib>
#include <time.h>
class Myclass {
private:
int randTime;
float cost;
public:
void estimate_time(){
randTime = rand()%100;
}
void estimate_cost(){
cost = randTime * 0.2;
}
float getEstimateCost(){
return cost;
}
};
int main(){
srand(time(NULL));
int numberOfObjects = 7;
Myclass obj[numberOfObjects];
//input
for(int i = 0; i < numberOfObjects; i++){
obj[i].estimate_time();
obj[i].estimate_cost();
}
// printing
for(int i = 0; i < numberOfObjects; i++){
std::cout << obj[i].getEstimateCost() << std::endl;
}
return 0;
}

Error Variable is Protected

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void armySkirmish();
void battleOutcome();
string commander = "";
int numberOfHumans = 0;
int numberOfZombies = 0;
class ArmyValues
{
protected:
double attackPower;
double defensePower;
double healthPoints;
public:
void setAttackPower(double a)
{
attackPower = a;
}
void setDefensePower(double d)
{
defensePower = d;
}
void setHealthPoints(double h)
{
healthPoints = h * (defensePower * .1);
}
};
class Zombies: public ArmyValues
{
};
class Humans: public ArmyValues
{
};
int main(int argc, char ** argv)
{
cout << "Input Commander's Name: " << endl;
cin >> commander;
cout << "Enter Number of Human Warriors: " << endl;
cin >> numberOfHumans;
cout << "Enter Number of Zombie Warriors: " << endl;
cin >> numberOfZombies;
armySkirmish();
battleOutcome();
return 0;
}
void armySkirmish()
{
cout << "\nThe Humans tense as the sound of the undead shuffle towards them." << endl;
cout << commander << " shuffles forward with a determined look." << endl;
cout << "The undead form up into ranks and growl a war chant!" << endl;
cout << commander <<" shouts, CHARGE!!!" << endl;
cout << endl;
cout << "Warriors from both sides blitz across the field!" << endl;
cout << endl;
cout << "*The Carnage has begun!*" << endl;
cout << "*Steal, Sparks, and Flesh flies" << endl;
}
void battleOutcome()
{
int zombieLives = numberOfZombies;
int humanLives = numberOfHumans;
int randomNumber = 0;
int humanDeath = 0;
int zombieDeath = 0;
double newHumanLife = 0;
double newZombieLife = 0;
Zombies zombieBattleData;
Humans humanBattleData;
srand(time(NULL));
zombieBattleData.setAttackPower(20.0);
humanBattleData.setAttackPower(35.0);
zombieBattleData.setDefensePower(15.0);
humanBattleData.setDefensePower(20.0);
zombieBattleData.setHealthPoints(150.0);
humanBattleData.setHealthPoints(300.0);
while(zombieLives && humanLives > 0)
{
randomNumber = 1+(rand()%10);
if(randomNumber < 6)
{
newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
if(newHumanLife <= 0)
{
humanLives--;
humanDeath++;
}
}else
{
newZombieLife = zombieBattleData.healthPoints - humanBattleData.attackPower;
if(newZombieLife <= 0)
{
zombieLives--;
zombieDeath++;
}
}
}
if(zombieLives <= 0)
{
cout << "Humans have emerged victorious!" << endl;
cout << "Human Deaths: " << humanDeath << "Zombie Deaths: " << zombieDeath << endl;
}else if(humanLives <= 0)
{
cout << "Zombies have emerges victorious!" << endl;
cout << "Human Deaths: " << humanDeath << "Zombie Deaths: " << zombieDeath << endl;
}
I know the code wont run properly as of now. What I was doing was a test run to make sure I was receiving no errors. The two errors I'm getting are:
armySimulatorMain.cpp:25:10: error: 'double ArmyValues::healthPoints' is protected
armySimulatorMain.cpp:115:67: error: within this context.
newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
This is the case for Attack Power and Health Power however, Defense power is clearing the errors. i don't understand why they are getting flagged. I'm changing the variable through the public function so shouldn't this be allowed?
Also, I'm calling three variables outside of all functions because they are being used by multiple functions. How can I plug those variables somewhere I don't like that they are floating freely above everything?
Thanks guys I can't believe I forgot about getters... Anyway the code runs now much appreciated I'll make sure to remember this time xD
It's not complaining about the line where you set the values; as you say, that uses a public function. But here, you try to read the protected member variables:
newHumanLife = humanBattleData.healthPoints - zombieBattleData.attackPower;
You only try to read two variables, and those are the ones it complains about.
You'll need a public getter function to read the values.
You need to do something like:
public:
double gethealthPoints()
{
return healthPoints;
}
because attackPower, defensePower, healthPoints are all protected, so if you want to access to any of them you need a getter, otherwise you will always receive an protect error

C++ Method won't call correct information from variable

I am working a a text adventure and have come across an error when I try to call a method from another method. The method returns the private int player_health and private int player_attack this work when I call it in main but when I call it in my combat_handler class it just give me a random number. I call it the exact same way in both class. Here is my code
Main.cpp
#include <iostream>
#include "player_handler.h"
#include "enemy_handler.h"
#include "combat_handler.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
player_handler phobj;
enemy_handler ehobj;
combat_handler cbobj;
phobj.test_player_systems();
ehobj.test_enemy_stats();
cbobj.player_turn();
phobj.get_player_stats();
return 0;
}
player_handler header
#ifndef PLAYER_HANDLER_H
#define PLAYER_HANDLER_H
class player_handler
{
public:
player_handler();
int get_player_health();
int get_player_attack();
void get_player_stats();
void add_player_health(int x);
void remove_player_health(int x);
void add_player_attack(int y);
void set_player_stats(int x, int y);
int test_player_systems();
private:
int player_health;
int player_attack;
};
#endif // PLAYER_HANDLER_H
player_handler.cpp
#include "player_handler.h"
#include <iostream>
using namespace std;
player_handler::player_handler()
{
//cout << "working"<< endl;
}
//Getting the players stats
int player_handler::get_player_health()
{
return player_health;
}
int player_handler::get_player_attack()
{
return player_attack;
}
void player_handler::get_player_stats()
{
cout << "the players health is " << get_player_health()<<endl;
cout << "The players attack is " << get_player_attack() <<endl;
}
//setting the player stats
void player_handler::set_player_stats(int x, int y)
{
player_handler::player_health = x;
player_attack = y;
//cout << "player health is " << player_health << endl;
//cout << "player attack is " << player_attack << endl;
}
//Player health control
void player_handler::add_player_health(int x)
{
//cout << "The old player health was " << player_health << endl;
player_health = player_health + x;
//cout << "Your health is now " << player_health << endl;
}
void player_handler::remove_player_health(int x)
{
//cout << "you lost " << x << " health" << endl;
player_health = player_health - x;
//cout << "Your new health is " << player_health << endl;
}
//player attack control
void player_handler::add_player_attack(int y)
{
//cout << "Your attack has been upgraded by " << y << " points" << endl;
player_attack = player_attack+y;
//cout << "You attack is now " << player_attack <<endl;
}
//test all player systems
int player_handler::test_player_systems()
{
set_player_stats(10,10);
cout <<"The player health is " << get_player_health()<<endl;
cout <<"the player attack is " << get_player_attack() << endl;
add_player_health(5);
remove_player_health(5);
add_player_attack(5);
get_player_stats();
return 0;
}
Now keep in mind that all this works it returns the right value for player_health and player_attack. now here is my problem it just gives me a random number
combat_handler header
#ifndef COMBAT_HANDLER_H
#define COMBAT_HANDLER_H
class combat_handler
{
public:
combat_handler();
int player_turn();
int enemy_turn();
private:
int player_input;
int player_dmg_given;
int enemy_dmg_given;
};
#endif // COMBAT_HANDLER_H
combat_handler.cpp
#include "combat_handler.h"
#include <iostream>
#include "player_handler.h"
using namespace std;
combat_handler::combat_handler()
{
cout << "combat handler on-line\n";
}
int combat_handler::player_turn()
{
player_handler phobj;
cout << "Would you like to (1)check stats (2) Enter the room?\n";
cin >> player_input;
switch(player_input){
case 1:
cout << "one"<<endl;
phobj.get_player_stats();
break;
default:
cout << "entering the room.\n";
break;
}
return 0;
}
The error happens when I call get_player_stats from the phobj in the combat handler class.
player_handler phobj;
This would call default constructor for player_handler( from your code it seems you are doing nothing in default constructor). And then you are calling get_player_stats() without setting the values. So, it would surely give you random values.
Earlier in main,
phobj.test_player_systems(); <<<< This is setting values.
phobj.get_player_stats(); <<<< Then you are querying on values already set.

How to initialize instances of classes?

//main.cpp
#include "Monsters.h"
#include "Hero.h"
#include "Monsters.h"
#include "Characters.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
When i created my object it took those parameters and initialised them to it right?
So why when i called their methods, it returned a different value?
For example, I initialized them here while created my object at the same time:
Hero Me(100,20,30,40);
Monsters m(100,16,18,20);//creates a monster object and uses overloaded constructor to initialize
Me.getHp();//expected 100, but got a long negative random number
m.getHp();//expected 100, but got a long negative random number also
//Why must i use setHp() and setAttack() when i already initialized them with the constructor?
Me.setAttack(89);
Me.setHp(100);
m.setAttack(40);
m.setHp(100);
cout << "\nAttacking!\n";
while ((Me.getHp() > 0) && (m.getHp() > 0))
{
cout << "\nYour hp is: " << Me.getHp() << endl;
cout << "The enemy's hp is: "<< m.getHp() << endl;
cout << "\nThe monster has attacked you!\n";
cout << "You received " << m.getAttack() << " damage;" << endl;
Me.damageTaken(m.getAttack());
if(Me.getHp() > 0)//Check if still alive
{
cout << "\nYour hp is now: " << Me.getHp() << endl;
//cout << "Enemy hp is: "<< m.getHp() << endl;
cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack() << " Damage" << endl;
m.damageTaken(Me.getAttack());
if(m.getHp() > 0)//Check if still alive
{
cout << "Enemy hp is now: " << m.getHp() << endl;
cout << "\nAttacking again!\n";
}
}
}
if ((Me.getHp() > 0) && (m.getHp() <= 0))
cout <<"\nCongratulations! You killed the enemy!" << endl;
else if ((Me.getHp() <= 0) && (m.getHp() > 0))
cout << "You have died!" << endl;
cin.sync();
cin.get();
return 0;
}
Here's the rest of the code in case you need it:
//Hero.h
#pragma once
#include "Characters.h"
class Hero:
public Characters
{
private:
int Herolevel;
int HeroHp;
int HeroStrength;
int HeroAttack;
int HeroDefense;
public:
//Hero();
Hero(int, int, int, int);
~Hero();
};
//Hero.cpp
#include "Monsters.h"
#include "Hero.h"
#include "Characters.h"
#include <iostream>
using namespace std;
//Hero::Hero()
//{
// cout << "HOLA! Hero Created using normal constructor\n";
//}
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
: HeroHp(newHp), Herolevel(newLevel), HeroAttack(newAttack), HeroDefense(newDef)
{
cout << "Hero created using Overloaded function!\n";
HeroHp = newHp;
cout << "Hp is: "<< HeroHp << endl;
Herolevel = newLevel;
cout << "level is: " << Herolevel << endl;
HeroAttack = newAttack;
cout << "Attack is: " << HeroAttack << endl;
HeroDefense = newDef;
cout << "Defense is: " << HeroDefense << endl;
}
Hero::~Hero()
{
cout << "Hero destroyed!\n";
}
//Monsters.h
#pragma once
#include "Characters.h"
class Monsters:
public Characters //Hero
{
private:
int Monsterlevel;
int MonsterHp;
int MonsterStrength;
int MonsterAttack;
int MonsterDefense;
public:
//Monsters();
Monsters(int, int, int, int);
//Monsters(int);
~Monsters();
};
//Monsters.cpp
#include "Monsters.h"
#include "Hero.h"
#include "Characters.h"
#include <iostream>
using namespace std;
Monsters::Monsters(int newHp, int newLevel, int newAttack, int newDef)
: MonsterHp(newHp), Monsterlevel(newLevel), MonsterAttack(newAttack), MonsterDefense(newDef)
{
cout << "Monster created using Overloaded function!\n";
MonsterHp = newHp;
cout << "Hp is: "<< MonsterHp << endl;
Monsterlevel = newLevel;
cout << "level is: " << Monsterlevel << endl;
MonsterAttack = newAttack;
cout << "Attack is: " << MonsterAttack << endl;
MonsterDefense = newDef;
cout << "Defense is: " << MonsterDefense << endl;
}
Monsters::~Monsters()
{
cout << "\nMonster Destroyed";
}
//Characters.h
#pragma once
class Characters
{
private:
int level;
int Hp;
int Strength;
int Attack;
int Defense;
public:
Characters();
Characters(int);
Characters(int, int, int, int);
~Characters();
int getAttack();
int getDefense();
int getStrength();
int getHp();
int getLevel();
void setAttack(int);
void setDefense(int);
void setStrength(int);
void setHp(int);
void setlevel(int);
void damageTaken(int);
};
//Characters.cpp
#include "Characters.h"
#include "Hero.h"
#include "Monsters.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
Characters::Characters()
{
cout << "\nCharacter has been created!\n";
}
Characters::Characters(int random)
{
cout << "Level " << level << " character created with: \n";
srand ((unsigned)time(0));
random = rand() % 10 + 1;
//setlevel(int random);
level = random;
}
Characters::~Characters()
{
cout << "Character has been destroyed!\n";
}
void Characters::setAttack(int att)//get Character left over hp
{
Attack = att;
}
void Characters::setDefense(int def)//get Character left over hp
{
Defense = def;
}
void Characters::setStrength(int str)//get Character left over hp
{
Strength = str;
}
void Characters::setHp(int health)//get Character left over hp
{
Hp = health;
}
void Characters::damageTaken(int damage)//get Character left over hp
{
Hp -= damage;
}
void Characters::setlevel(int lvl)//get Character left over hp
{
level = lvl;
}
int Characters::getAttack()
{
//cout << "Your attack is: " << Attack << endl;
return Attack;
}
int Characters::getDefense()
{
//cout << "Your defense is: " << Defense << endl;
return Defense;
}
int Characters::getStrength()
{
//cout << "Your strength is: " << Strength << endl;
return Strength;
}
int Characters::getHp()
{
//cout << "Your hp is: " << Hp << endl;
return Hp;
}
int Characters::getLevel()
{
//cout << "Your level is: " << level << endl;
return level;
}
How to initialize instances of classes?
You need to correctly initialize your base classes by calling their constructor, passing on the relevant arguments. Maybe it is easier to illustrate this with a simpler example:
class Foo
{
public:
explicit Foo(int i) : foo_i(i) {}
int getFoo() const { return foo_i; }
private:
};
class Bar : public Foo
{
public:
explicit Bar(int i) : Foo(i) // calls Foo(int) constructor, setting Foo::foo_i
{}
};
#include <iostream>
int main()
{
Bar b(42);
b.getFoo(); // prints 42
}
Hero inherits Characters, including all its data members (level, hp, etc.).
So, it doesn't need to add its own HeroLevel, HeroHP etc. You're just duplicating data (and work).
Now, it also doesn't initialize the base class members - the default constructor for Characters doesn't even zero them, so their values are undefined.
You should be looking for something like this:
class Characters
{
private:
int level;
int hp;
int strength;
int attack;
int defense;
protected:
Characters(); // zero everything by default
Characters(int); // randomly generate everything
Characters(int, int, int, int); // populate explicitly
public:
int getAttack() const { return Attack; }
// etc.
};
Characters::Characters() : level(0), hp(0), strength(0), attack(0), defense(0) {}
Characters::Characters(int seed) {
// NB. your code still doesn't initialize hp, strength etc.
// it also logs level before initializing it, so that will be garbage
}
Characters::Characters(int hit, int lvl, int att, int def)
: level(lvl), hp(hit), attack(att), defense(def)
{
// you only pass 4 attributes, what should strength be?
}
and finally:
Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
: Characters(newHp, newLevel, newAttack, newDef)
{
// logging goes here
// note that you don't need HeroLevel etc. at all any more, just use level
}
You should really try to understand the basics of this before designing an entire class hierarchy - see juanchopanza's answer for a clear example. A smaller example would also have been much easier to paste, read and understand.