Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:15:15+00:00 2026-06-13T10:15:15+00:00

I’m working on a text based game for fun and I’m struggling with inheritance

  • 0

I’m working on a text based game for fun and I’m struggling with inheritance
from a base class. I have my base class Being which holds all the stats for
whatever character I create. I then have a class Combat that I want to take
all the stats from Being (Or from a Being and pass it into combat?) and do
all the combat stuff. But I’m not quite understanding inheritance, or at least
how to declare the functions in Main to get it to work. If I keep the attack
function in Being, I can just write this line in main:

human.attack(monster);

But after splitting the combat into another class, I’m not sure how to write
that in main so it works. Please & thank you for your help!

class Being // Base Class
{
public:

    string name, nameSpecialAttack; // Implement a special attack feature,
                                    // that has a specific mutliplier #
                                    // unique to each being
    int attackBonus, attackMod; // was a float attackMod method for casting
                                // spells that double or halve a Being’s
                                // attack power.
    int baseDamage; // base damage
    int health, healthMax; // current health and max health
    int mp, mpMax; // current mp and max mp
    int arrows; // change to [ranged?] ammo

    Being(); // Default Constructor
    Being(string name, string nameSpecialAttack, int attackBonus,
          int attackMod, int baseDamage, int health, int healthMax,
          int mp, int mpMax, int arrows); // Constructor 2
    // All my set and get functions
}

Then my derived class:

class Combat : public Being
{
private:

public:

    void attack(Being& target);

};

Combat.cpp:

void Combat::attack(Being& target)
{
    //unsigned seed = time(0);
    //srand(seed); 
    // Rand # 0-99, * damage+1, /100, * attackMod, parse to int.
    int damage = (int)(attackMod*( ( (rand()%100)*(baseDamage+1) /100) + attackBonus + (rand()%2))); 
    target.health -=damage;

    cout << name << " attacks " << target.name << " doing " << damage << " damage!" << endl;
    cout << target.name << "'s health: " << target.health << endl;

    // Use getHealth() instead and put this function there
    if(target.health <= 0)
    {
        cout << endl << name << " killed " << target.name << "! You have won the game! " << endl << endl;
        cout << "Terminating AMnew World, Good Bye.\n" << endl << endl;
        exit(0);
    }
}

Main:

Being human("", "FirstofFurry", 2, 1, 2, 50, 50, 20, 30, 7); // A thing of
                                                             // class Being
Being monster("Armored Goblin", "Rawr", 2, 1, 2, 65, 59, 20, 20, 6);

int main()
{
    human.attack(monster); // No longer works since attack is in
                           // combat class now
    Sleep(3000);
    cout << endl << endl;
    monster.attack(human);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:15:16+00:00Added an answer on June 13, 2026 at 10:15 am

    This wouldn’t be something you would want to have an inheritance, combat would not be a type of being. For example an apple is a kind of fruit, so a fruit base class and apple derived class would be logically correct. I would recommend creating an Enemy class and a Hero class, deriving monsters such as zombies and ninjas off of enemy.

    #include <iostream>
    #include <cmath>
    #include <string>
    using namespace std;
    
    class Character
    {
        public:
        Character(){}     //does nothing, just represents the heros class
    };
    
    class Enemy
    {
        private:
            int Health;         //base class variables
            int WeaponID;
        public:
            Enemy(int hp, int wpID);
            virtual void Attack(Character& Target); //if all monsters have same attack, no need for virtual
            //etc...etc...
    };
    
    class Zombie:public Enemy               //DERIVED CLASS, uses base class attack method
    {
        public:
            Zombie(int hp, int wpID):
                Enemy(hp,wpID)
            {}
    };
    
    class Ninja: public Enemy               //DERIVED CLASS, uses its own attack method
    {
        private:
            int NumThrowingKnives;
        public:
            Ninja(int Hp , int ID) : Enemy(Hp,ID)
            {}
            void Attack(Character& Target); //different attack
            //etc..etc..
    
    };
    
    Enemy::Enemy(int hp, int wpID)
    {
        Health = hp;
        WeaponID = wpID;
    }
    
    void Ninja::Attack(Character& Target)
    {
        cout << "Ninja attack!" << endl;
    }
    
    void Enemy::Attack(Character& Target)
    {
        cout << "Base class attack!" << endl;
    }
    
    int main()
    {
        Character Bob;
        Ninja Bill(50,12);
        Zombie Rob(50,16);
        Bill.Attack(Bob);
        cout << endl;
        Rob.Attack(Bob);
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a small JavaScript validation script that validates inputs based on Regex. I
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.