Questions about inheritance

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.
You understood well what I ment, writetonsharma. However, how does the constructor & destructor inherit? Is it like this:

1
2
3
4
5
class Boss : private Character{
    public:
        Character(); // like it really is here?
        ~Character(); // this one too?
};


I haven´t understood this yet.
Last edited on

run this:

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
#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 = new char[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 = new char[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.
Last edited on
That explained a lot. Thanks again, writetonsharma!
Topic archived. No new replies allowed.