TheCoder95
TheCoder95

Reputation: 44

C++ Inheritance and Pointer Errors (Beginner)

I'm a relative beginner to C++, I've been learning for the past 4 months. We have an an assignment to do for school; create a text based adventure game using what we have learnt so far from C++. We went over classes, inheritance and pointers today so I thought that I'd get some practice using classes/inheritance.

My code so far is:

Character.h (The header file)

#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>

class Character
{
private:
    int Health;

public:
    Character();
    ~Character();

    int  GetHealth()                        {return Health;}
    void SetHealth(int newHealth)           {Health = newHealth;}       


};

class Monster:public Character
{
    int  GetHealth()                        {return Health;} 
    void SetHealth(int newHealth)           {Health = rand()% 50+100;}
}

#endif

Character.cpp

#include "Character.h"

Character::Character()
{
    Health = 100;
}

Character::~Character()
{
}

Battle.cpp (Where everything takes place)

#include <iostream>
#include <cstdlib>
#include "Character.h"
using namespace std;

int main()
{
    Character* Player = new Character();
    Monster* Monster1 = new Monster();

    cout << "Player's Health is: " << Player->SetHealth << endl << endl;
    cout << "Monster's Health is: " << Monster1->SetHealth << endl << endl;
}

Let me explain what I'm trying to do...

I'm simply trying to get the program to display the health of both the player and the monster.

In the character header file, the class 'Character' is a base for every character in the game. Then I Get and Set the health for the class. Then I'm trying to create a child class called 'Monster' derived from the 'Character' class. This will have different health compared to the standard character class but I'm not sure how to do that. I wanted to make the health randomly generate between 50 and 100. Then in the 'Battle.cpp' file I am trying to create a new character called 'Player' which includes the default amount of health (which is 100) and a new monster called 'Monster1'. Then I want to display the health for both characters. I also aim to create multiple monsters with varying amounts of health.

When I try to run the program I seem to get a large amount of errors saying 'See declaration of Character::Health'.

I'm not too sure what I've done wrong as I'm still fairly new to C++ and still trying to get my head around the concepts of pointers and inheritance.

Upvotes: 0

Views: 262

Answers (2)

Michał
Michał

Reputation: 2282

Let me answer inside the code:

#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>

class Character
{
    // this property will be used in the derived classes, we need protected instead of private
protected:
    int Health;

public:
    Character();
    ~Character();

    // This method is to be repeated in the derived class, and publicly accessible
    // (not only inside the class, but also outside (like you do in main())
public:
    int  GetHealth()                        {return Health;}
    // This method needs to be redefined in Monster, that's a candidate for a virtual method
    virtual void SetHealth(int newHealth)           {Health = newHealth;}       


};

class Monster:public Character
{
    // You don't need this anymore - the GetHealth from class Character is accessible
    // int  GetHealth()                        {return Health;} 

    // you override a method - write a different version. Use virtual to note that
    virtual void SetHealth(int newHealth)           {Health = rand()% 50+100;}
}; // lacked a semi-colon here :)

#endif

And the code for Battle.cpp

#include <iostream>
#include <cstdlib>
#include "Character.h"

using namespace std;

int main()
{
    Character* Player = new Character();
    Monster* Monster1 = new Monster();

    // The moment you cout, you need to provide a value (a method that returns something).
    // You want to "GetHealth()" in order to show it :)
    cout << "Player's Health is: " << Player->GetHealth() << endl << endl;
    cout << "Monster's Health is: " << Monster1->GetHealth() << endl << endl;
}

I also don't like what you do with Health in the class. First you declare it private - so nobody can change it in an unauthorized way. Then you declare a plain SetHelth() which lets you do anything with the value.

Instead you could make a initial value in the constructor:

#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>

class Character
{
    // this property will be used in the derived classes, we need protected instead of private
protected:
    int Health;

public:
    Character();
    ~Character();

    // This method is to be repeated in the derived class, and publicly accessible
    // (not only inside the class, but also outside (like you do in main())
public:
    int  GetHealth()                        {return Health;}
};

class Monster:public Character
{
    // You don't need this anymore - the GetHealth from class Character is accessible
    // int  GetHealth()                        {return Health;} 

public:
    Monster();
}; // lacked a semi-colon here :)

#endif

And the Character.cpp

#include "Character.h"

Character::Character()
{
    Health = 100;
}

Character::~Character()
{
}

Monster::Monster()
{
    Health = rand()% 50+100;
}

And then you can change it with methods like DrinkHealthPotion(); or GetDamage();

Upvotes: 1

qrikko
qrikko

Reputation: 2603

I think your problem might be a typo, where you have written:

cout << "Player's Health is: " << Player->SetHealth << endl << endl;
cout << "Monster's Health is: " << Monster1->SetHealth << endl << endl;

I believe you mean GetHealth and not SetHealth, also GetHealth is a function so you will have to call it with parenthesis GetHealth().

So I believe that you want it to say:

cout << "Player's Health is: " << Player->GetHealth() << endl << endl;
cout << "Monster's Health is: " << Monster1->GetHealth() << endl << endl;

Hope that helps, and good luck with your future endeavors with C++

Upvotes: 0

Related Questions