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

Hey guys i need help! 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    #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:

1
2
3
4
5
6
    //SwordShop derived cpp
    #include "SwordShop.h"
    void SwordShop::soldierShop(Hero& hero)
    {
      /* some code here*/
    }
Make the function virtual. That's the whole point of virtual functions.

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
#include <iostream>
using namespace std;

struct Base
{
  virtual ~Base() { }  // Doesn't do anything, but you need it anyway
  
  void hello() const  // Base class function that calls derived stuff
  {
    do_hello();
  }
  
  virtual void do_hello() const  
  {
    cout << "Hello from Base!\n";
  }
};

struct Derived: public Base
{
  virtual void do_hello() const
  {
    cout << "Hello from Derived!\n";
  }
};
27
28
29
30
31
32
33
34
35
36
37
38
39
// Version using pointers to Base 

int main()
{
  Base* base    = new Base;
  Base* derived = new Derived;
  
  base->hello();
  derived->hello();
  
  delete derived;
  delete base;
}
27
28
29
30
31
32
33
34
35
36
// Version using local Base object 

int main()
{
  Base    base;
  Derived derived;
  
  base.hello();
  derived.hello();
}

Hope this helps.

[edit] Added more info.
Last edited on
oh i kind of understand now, but what would happen if, lets say that i had a Hero class with a function called gameplay. something like this:
1
2
3
//Main.cpp
Hero hero;
hero.gameplay();

inside this gameplay is where i want to call the different shops. So how can i do that without declaring the shop instances in main?
If you want to be able to call the 'shop' functions without an instance of the shop, there are a few ways to do this.

1) You can create an instance of the shop within your gameplay() function, and then call its functions
1
2
3
4
5
6
7
8
9
void MainShop::EnterShop(Hero& hero) {
    switch(choice) {
        case 1: {
            SwordShop temp;
            temp.soldierShop(hero);
        } break;
        // etc
    }
}


2) You can declare the function as a static, so you can call it like you did above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class SwordShop {
public:
    static void soldierShop(Hero& hero);
    // This func can only access data members declared as static
};

void MainShop::enterShop(Hero& hero) {
    switch (choice) {
        case 1:
            SwordShop::soldierShop(hero);
        break;
        // etc
    }
}


3) You can make each of the shops inherit from MainShop, and have MainShop have a virtual function for each of the shops that can be called from the shop, similar to @Duoas' example above, and all you do is have a seperate, non-member function that is passed an instance of a "shop" and call its "doShop" function (or whatever you call it).
Okay i tried exactly what you said, but i get this error!
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
Ld /Users/Damian/Library/Developer
/Xcode/DerivedData/SampleClass1
-dmwbviaemubkqddiigadavsgeknm/Build/
Products/Debug/SampleClass1
 normal x86_64
    cd /Users/Damian/Documents/Xcode/SampleClass1
    setenv MACOSX_DEPLOYMENT_TARGET 10.9
    /Applications
/Xcode.app/Contents/Developer/Toolchains/
XcodeDefault.xctoolchain/usr/bin/clang++ -arch
 x86_64 -isysroot /Applications/Xcode.app
/Contents/Developer/Platforms/MacOSX.platform/
Developer/SDKs/MacOSX10.9.sdk -L/Users/Damian
/Library
/Developer
/Xcode/DerivedData
/SampleClass1-dmwbviaemubkqddiigadavsgeknm
/Build/Products/
Debug -F/Users/Damian
/Library/Developer/Xcode/DerivedData/
SampleClass1-dmwbviaem
ubkqddiigadavsgeknm/Build/Products/Debug -filelist 
/Users/Damian
/Library/Developer/Xcode/DerivedData
/SampleClass1-dmwbviaemubkqddiigadavsgeknm/
Build/Intermediates/SampleClass1.build/
Debug/SampleClass1.build/Objects-normal/x86_64/
SampleClass1.LinkFileList
 -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker 
-dependency_info
 -Xlinker /Users/Damian/Library/Developer/Xcode/
DerivedData/SampleClass1-dmwbviaemubkqddiigadavsgeknm/Build/Intermediates/
SampleClass1.build/
Debug/SampleClass1.build/Objects-normal/x86_64/SampleClass1_dependency_info.dat -o 
/Users
/Damian/Library/Developer/Xcode/DerivedData/
SampleClass1-dmwbviaemubkqddiigadavsgeknm/
Build/
Products/Debug/
SampleClass1

Undefined symbols for architecture x86_64:
  "vtable for PotionShop", referenced from:
      PotionShop::PotionShop() in PotionShop.o
  NOTE: a missing vtable usually means the first non-inline 
virtual member function has no definition.
  "vtable for SpellBookShop", referenced from:
      SpellBookShop::SpellBookShop() in SpellBookShop.o
  NOTE: a missing vtable usually means the first non-inline 
virtual member function has no definition.
  "vtable for BowShop", referenced from:
      BowShop::BowShop() in BowShop.o
  NOTE: a missing vtable usually means the first non-inline 
virtual member function has no definition.
  "vtable for SwordShop", referenced from:
      SwordShop::SwordShop() in SwordShop.o
  NOTE: a missing vtable usually means the first non-inline 
virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 
(use -v to see invocation)


Please help!
Topic archived. No new replies allowed.