My Inheritance Code Doesn't Work. Click Me!

closed account (zb0S216C)
OK. I have this simple inheritance code that involves a pure base class, and a derived class. Here's the problem: when I print the value of a member of the base class, nothing appears. To be more specific, here's my code:

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
//------------------------------------------------------
// The base class:
//------------------------------------------------------
class SWORD_BASE_PURE
{
    protected:
        std::string Name;
        short Damage;

    public:
        virtual inline const short &GetDamage( ) const PURE;
        virtual inline const std::string &GetName( ) const PURE;
        virtual inline void SetName( const char * ) PURE;
        virtual void OnTargetHit( ) PURE;
};

//------------------------------------------------------
// The derived class:
//------------------------------------------------------
class SWORD : private SWORD_BASE_PURE
{
    public:
        inline const short &GetDamage( ) const
        {
            return this->Damage;
        }

        inline const std::string &GetName( ) const
        {
            return this->Name;
        }

        inline void SetName( const char *NewName )
        {
            this->Name = NewName;
        }

        void OnTargetHit( );
};

void SWORD::OnTargetHit( )
{
    // Not yet defined.
}

int main( )
{
    SWORD Sword;
    Sword.SetName( "Player's Sword" );
    
    std::cout << "Sword Name: " << Sword.GetName( ) << std::endl;

    //...
}

When Sword.GetName( ) is encountered, Player's Sword supposed to appear on the screen, but it doesn't. What am I doing wrong.

Wazzak
Last edited on
Beats me. When I compile and run the following code:

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
#include <string>
#include <iostream>

using namespace std;


//------------------------------------------------------
// The base class:
//------------------------------------------------------
class SWORD_BASE_PURE
{
    protected:
        std::string Name;
        short Damage;

    public:
        virtual inline const short &GetDamage( ) const =0;
        virtual inline const std::string &GetName( ) const =0;
        virtual inline void SetName( const char * ) =0;
        virtual void OnTargetHit( ) =0;
};

//------------------------------------------------------
// The derived class:
//------------------------------------------------------
class SWORD : private SWORD_BASE_PURE
{
    public:
        inline const short &GetDamage( ) const
        {
            return this->Damage;
        }

        inline const std::string &GetName( ) const
        {
            return this->Name;
        }

        inline void SetName( const char *NewName )
        {
            this->Name = NewName;
        }

        void OnTargetHit( );
};

void SWORD::OnTargetHit( )
{
    // Not yet defined.
}

int main( )
{
    SWORD Sword;
    Sword.SetName( "Player's Sword" );
    
    std::cout << "Sword Name: " << Sword.GetName( ) << std::endl;

    //...
    return 0;
}


I get this output

# ./a.out 
Sword Name: Player's Sword

@ Framework: A simular issue happend with some code you posted a few months ago if I remember correctly, it wasn't working for you but when I went to compile it I couldn't recreate the issue you were describing. Have you considered reinstalling your IDE suite?
Last edited on
closed account (zb0S216C)
Thanks for taking the time to look at my code, lads. I'll try use the same source code in a new project; new object code, new definition files - the works. If that fails, I'll have to reinstall my IDE ... again ... for the third time.

Thanks again :)

Edit: [I hope it's not VC++ taking revenge on me since I swore at it because my code wouldn't compile.]

Final Edit: [Making a new project didn't work. I'll have to reinstall the IDE :-(]

Wazzak
Last edited on
Topic archived. No new replies allowed.