Trouble with inheritance once again - what is the problem?

Hi, it´s me again, this time having a problem with inheritance itself once again.

Here´s the code:

1
2
3
4
5
6
#include <cstdlib>
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;


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
class Collectible{
    protected:
        bool this_exists, this_exists_extension, this_extendable;
        int this_amount_extension, this_amount_extension_max, this_amount_normal, this_amount_normal_max;
        std::string this_name;
        void CheckThis(bool extension = false);
    public:
        Collectible(void);
        virtual ~Collectible(void);
        inline void DisplayData(void);
        void operator=(Collectible);
        void operator=(int);
        int operator+(Collectible);
        int operator+(int);
        void operator+=(int);
        void operator++(void);
        void operator++(int);
        int operator-(int);
        void operator-=(int);
        void operator--(void);
        void operator--(int);
};
Collectible::Collectible(void) : this_exists(true), this_exists_extension(false), this_extendable(false){

}
Collectible::~Collectible(void){
    this_exists = false;
    this_exists_extension = false;
    this_extendable = false;
}
void Collectible::CheckThis(bool extension){
    if(extension){
        if(this_amount_extension > this_amount_extension_max){
            this_amount_extension = this_amount_extension_max;
            this_extendable ? this_exists_extension = true : this_exists_extension = false;
        }
        else if(this_amount_extension <= 0){
            this_amount_extension = 0;
            this_exists_extension = false;
        }
        else{
            if(this_extendable) this_exists_extension = true;
            else{
                this_exists_extension = false;
                this_amount_extension = 0;
            }
        }
    }
    else{
        if(this_amount_normal > this_amount_normal_max){
            this_amount_normal = this_amount_normal_max;
            this_exists = true;
        }
        else if(this_amount_normal <= 0){
            this_amount_normal = 0;
            this_exists = false;
        }
        else this_exists = true;
    }
}
inline void Collectible::DisplayData(void){
    std::cout << "Exists: "     << this_exists               << std::endl;
    std::cout << "Extendable: " << this_extendable           << std::endl;
    std::cout << "Exists ext: " << this_exists_extension     << std::endl;
    std::cout << "\tExt: "      << this_amount_extension     << std::endl;
    std::cout << "\tExt max: "  << this_amount_extension_max << std::endl;
    std::cout << "\tThis: "     << this_amount_normal        << std::endl;
    std::cout << "\tThis max: " << this_amount_normal_max    << std::endl;
}
void Collectible::operator=(Collectible a){
    this_amount_extension = a.this_amount_extension;
    this_amount_extension_max = a.this_amount_extension_max;
    this_amount_normal = a.this_amount_normal;
    this_amount_normal_max = a.this_amount_normal_max;
}
void Collectible::operator=(int a){
    this_amount_normal = a;
    this->CheckThis();
}
int Collectible::operator+(Collectible a){
    return this_amount_normal + a.this_amount_normal;
}
int Collectible::operator+(int a){
    return this_amount_normal + a;
}
void Collectible::operator+=(int a){
    this_amount_normal += a;
    this->CheckThis();
}
void Collectible::operator++(void){
    *this += 1;
}
void Collectible::operator++(int){
    ++*this;
}
int Collectible::operator-(int a){
    return this_amount_normal - a;
}
void Collectible::operator-=(int a){
    this_amount_normal -= a;
    this->CheckThis();
}
void Collectible::operator--(void){
    *this -= 1;
}
void Collectible::operator--(int){
    --*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
class Armor : public Collectible{
    public:
        Armor(void);
        Armor(unsigned int);
        explicit Armor(unsigned int,unsigned int);
};
Armor::Armor(void){
    this_amount_extension =
    this_amount_extension_max = 0;
    this_amount_normal =
    this_amount_normal_max = 100;
    this_extendable = false;
}
Armor::Armor(unsigned int armor){
    this_amount_extension =
    this_amount_extension_max = 0;
    this_amount_normal =
    this_amount_normal_max = armor;
    this_extendable = false;
}
Armor::Armor(unsigned int armor, unsigned int armor_max){
    this_amount_extension =
    this_amount_extension_max = 0;
    this_amount_normal = armor;
    this_amount_normal_max = armor_max;
    this_extendable = false;
}


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
class Health : public Armor{
    public:
        Health(void);
        Health(bool);
        explicit Health(bool,unsigned int);
        Health(unsigned int);
        explicit Health(unsigned int,unsigned int);
        bool operator==(Health);
        bool operator!=(Health);
        void operator<<=(int);
        void operator>>=(int);
};
Health::Health(void){
    this_amount_extension = 0;
    this_amount_extension_max = 100;
}
Health::Health(bool extendable){
    this_amount_extension = 0;
    this_amount_extension_max = 100;
    this_extendable = extendable;
}
Health::Health(bool extendable, unsigned int health){
    this_amount_extension = 0;
    this_amount_extension_max = 100;
    this_extendable = extendable;
}
Health::Health(unsigned int health){
    this_amount_extension = 0;
    this_amount_extension_max = 100;
}
Health::Health(unsigned int health, unsigned int health_max){
    this_amount_extension = 0;
    this_amount_extension_max = 100;
}
bool Health::operator==(Health a){
    if(this_amount_normal == a.this_amount_normal) return true;
    return false;
}
bool Health::operator!=(Health a){
    return !(*this==a);
}
void Health::operator<<=(int a){
    this_amount_extension -= a;
    this->CheckThis(true);
}
void Health::operator>>=(int a){
    *this<<=(a-(a*2));
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Live : public Armor{
    public:
        Live(void);
        Live(unsigned int);
        explicit Live(unsigned int,unsigned int);
};
Live::Live(void){
    this_amount_normal = 3;
    this_amount_normal_max = 99;
}
Live::Live(unsigned int lives){
    this_amount_normal = lives;
    this_amount_normal_max = 99;
}
Live::Live(unsigned int lives, unsigned int lives_max){
    this_amount_normal = lives;
    this_amount_normal_max = lives_max;
}


1
2
3
4
class Power : public Health{
        std::vector<Action> this_actions;
    public:
};


1
2
3
4
5
int main(){
    Power power1 = 125; // here it crashes!
    Health health = 125; // this one doesn´t work either...
    return 0;
}


When I create Armor -object, it does work, but when I try to create each one of those Health & Power -objects, it crashes. I don´t know what I´ve done wrong here, perhaps I have inherited from classes invalidly.
Last edited on
You mean that compiles? I don't think so.

There's no conversion from 125 (an int) to power1 (Power). The same goes for Health. You need to define a constructor on Power and Health that takes an int and pass the int in at construction, not assign it in the way you have done.

That Java style's eventually going to get you into even more trouble. If you're doing C++, do C++.
Last edited on
No, I didn´t mean it compiles. The problem appears, when I try to call Health or Power with copy constructor (taking unsigned int as parameter).

Creating an object in main function:

1
2
Health health1 = 125; // Normal implicit way
Health health1(125); // Explicit way 


And here´s the declaration:

1
2
3
4
Health::Health(unsigned int health){
    this_amount_extension = 0;
    this_amount_extension_max = 100;
}


But for some reason, this doesn´t work.
Last edited on
Never mind, my bad. I didn´t realize that signed and unsigned int vary so much that compiler though I was putting signed int instead of trying to put unsigned int in.
You should choose whether you want to use signed or unsigned types, don't let the language bully you into using what you don't want.

By default, the compiler will treat 125 as a signed int, but you can use 125U to specify an unsigned int.
Interesting, didn´t know that... Where could I possibly find more markings like that?
Topic archived. No new replies allowed.