Hello, I´m yellow and I have a problem. Can anybody help me, I dunno...
Well, anyway, I have some things to talk about the inheritance itself. Please take a look at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Character{
protected:
HGDK::Features::Possibilities::Action *this_action;
HGDK::Features::Equipment::Armor *this_armor;
HGDK::Features::Possibilities::Health *this_health;
HGDK::Features::Possibilities::Power *this_power;
public:
// Constructors & destructors
Character();
Character(pos_small);
~Character();
};
class Boss : private Character{
public:
};
class Player : private Character{
public:
};
You can see that there are pointers: these are object pointers and they will be memory allocated in each Character -constructor and deallocated in Character -destructor. Then I would like to inherit as you can see from those two derived classes below. Now the big question is if it is possible to inherit both pointers and memory allocation things into both of these derived classes. Got the idea?
but that will automatically be inherited in the derived classes. the constructor of the base class will run and will allocate memory for these pointer's. You can use these pointer's in your derived classes.
I dont know if I understood the question correctly.
#include <iostream>
class Character
{
protected:
char *Character_Ptr;
public:
// Constructors & destructors
Character()
{
std::cout << "I will be created first, Character created and initialized its pointers" << std::endl;
Character_Ptr = newchar[32];
strcpy(Character_Ptr, "I am character class");
std::cout << "The value of charcter pointer is >> " << Character_Ptr << std::endl;
}
~Character()
{
std::cout << "Character going to end now." << std::endl;
delete Character_Ptr;
}
};
class Boss : private Character
{
protected:
char *Boss_Ptr;
public:
Boss()
{
std::cout << "I will be created after my base class, Boss created and initialized its pointers" << std::endl;
Boss_Ptr = newchar[32];
std::cout << "I inherited the character ptr value also, its value is >> " << Character_Ptr << std::endl;
}
~Boss()
{
std::cout << "Boss going to end now." << std::endl;
delete Boss_Ptr;
}
};
int main()
{
Boss b;
return 0;
}
now you will see that the base class constructor will run first, initialize its members. Than the derived class constructor will run and initialize its members. But the derived class can use the base class members also and they are already initialized for it to be used. So actually Boss has inherited the properties of Character class.