How to call a function from a derived class in a base class?

Damian

I spent hours and hours looking online but none had the same problem as me. Basically, I have a base class called MainShop and it has 3 derived classes which are SwordShop, SpellBookShop and BowShop. I want the base class to be able to call a function from one of the derived classes but no matter what i do, it doesn't seem to work! Here is my code:

#include "MainShop.h"
//BaseClass cpp

void MainShop::EnterShop(Hero& hero)
{
    //Display Choices
        switch (choice)
        {
            //Swords
            case 1: SwordShop::soldierShop(hero);//DOES NOT WORK!!
                        break;
            case 2: SpellBookShop::MageShop(hero);//Staffs
                        break;
            case 3: BowShop::ArcherShop(hero);//Bows
                        break;
            default: cout << "Error!, Please try again.";
                        MainShop::EnterShop(hero);


        }
}

I have two other derived classes, but its basically the same concept. I have a function in one of the derived classes and i would like to call it from the base class. This is one my derived classes:

//SwordShop derived cpp
#include "SwordShop.h"
void SwordShop::soldierShop(Hero& hero)
{
  /* some code here*/
}
Eric Z

It's not a good design to select specific sub-class instance in super-class methods, e.g., by dynamic_cast, due to runtime overhead, and future maintenance, etc.

You can offload the burden of such switch-case logic to virtual functions which are designed by the language to call a specific instance via base class pointer/reference.

For example:

class MainShop
{
public:
   virtual void EnterShop(Hero &hero) = 0;
};

class SwordShop: public MainShop
{
   void EnterShop(Hero &hero)
   {
      soldierShop(hero);
   }
};

class SpellBookShop: public MainShop
{
   void EnterShop(Hero &hero)
   {
      MageShop(hero);
   }
};

int main()
{
   ...
   MainShop *shop = new SwordShop;
   // calling soldierShop
   shop->EnterShop(hero);
   ..
   shop = new SpellBookShop;
   // calling MageShop
   shop->EnterShop(hero);
   ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call base() from derived class to execute base class function?

From Dev

Call function in derived class (from interface), from base class

From Dev

Call derived class' function from a base class' instance

From Dev

Call function from Derived class stored in a Map with Base class type

From Dev

Call a function from base class that uses the member functions of derived class

From Dev

How to call an operator from a base class within the derived class?

From Dev

How to call derived class method from base class pointer?

From Java

How to call a parent class function from derived class function?

From Dev

C++ call derived function from base class

From Dev

Moq a base class function from a derived class

From Dev

Printing from a derived class with a base class function

From Dev

How to call derived assignment operator from base class?

From Dev

Call templated function with derived class arguments using base class pointers

From Dev

Calling a derived function from an array of the base class

From Dev

Call derived class method from base class instance

From Dev

Call derived class from base class (VB.NET)

From Dev

Call derived class method from base class instance

From Dev

How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call a base class method from within the same overloaded derived class method in python?

From Dev

C++ how to call Base class method from multiple derived class?

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How to call constructor of a template base class in a template derived class?

From Dev

Base Constructor Call in Derived Class

From Dev

How to implement a derived class and call it from a Button?

From Dev

Calling a member function of a derived class from the base class constructor

From Dev

returning instance of derived class from base class member function

From Dev

Accessing virtual base class function from a derived class

Related Related

  1. 1

    Call base() from derived class to execute base class function?

  2. 2

    Call function in derived class (from interface), from base class

  3. 3

    Call derived class' function from a base class' instance

  4. 4

    Call function from Derived class stored in a Map with Base class type

  5. 5

    Call a function from base class that uses the member functions of derived class

  6. 6

    How to call an operator from a base class within the derived class?

  7. 7

    How to call derived class method from base class pointer?

  8. 8

    How to call a parent class function from derived class function?

  9. 9

    C++ call derived function from base class

  10. 10

    Moq a base class function from a derived class

  11. 11

    Printing from a derived class with a base class function

  12. 12

    How to call derived assignment operator from base class?

  13. 13

    Call templated function with derived class arguments using base class pointers

  14. 14

    Calling a derived function from an array of the base class

  15. 15

    Call derived class method from base class instance

  16. 16

    Call derived class from base class (VB.NET)

  17. 17

    Call derived class method from base class instance

  18. 18

    How can I call virtual function definition of base class that have definitions in both abstract base class and derived class in C++?

  19. 19

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  20. 20

    How do I call a base class method from within the same overloaded derived class method in python?

  21. 21

    C++ how to call Base class method from multiple derived class?

  22. 22

    How do I call a derived class method from the base class constructor in C++?

  23. 23

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  24. 24

    How to call constructor of a template base class in a template derived class?

  25. 25

    Base Constructor Call in Derived Class

  26. 26

    How to implement a derived class and call it from a Button?

  27. 27

    Calling a member function of a derived class from the base class constructor

  28. 28

    returning instance of derived class from base class member function

  29. 29

    Accessing virtual base class function from a derived class

HotTag

Archive