0

I'm pretty sure this is OOP 101 (maybe 102?) but I'm having some trouble understanding how to go about this.

I'm trying to use one function in my project to produce different results based on what objet is passed to it. From what I've read today I believe I have the answer but I'm hoping someone here could sure me up a little.

//Base Class "A"
class A
{
    virtual void DoThis() = 0; //derived classes have their own version
};

//Derived Class "B"
class B : public A
{
    void DoThis() //Meant to perform differently based on which
                  //derived class it comes from
};

void DoStuff(A *ref) //in game function that calls the DoThis function of
{ref->DoThis();}     //which even object is passed to it.
                     //Should be a reference to the base class

int main()
{
    B b;

    DoStuff(&b);     //passing a reference to a derived class to call
                     //b's DoThis function
}

With this, if I have multiple classes derived from the Base will I be able to pass any Derived class to the DoStuff(A *ref) function and utilize the virtuals from the base?

Am I doing this correctly or am I way off base here?

Prototype958
  • 179
  • 8
  • Why don't you run the code and see what you can get? – taocp Mar 13 '14 at 20:00
  • @taocp Would love to but I'm at work for the night. Strangely I get most of my programming done without an IDE available. – Prototype958 Mar 13 '14 at 20:03
  • I'd suggest that you take a look at virtual functions and inheritance in C++ at first. There are some errors in your current code. – taocp Mar 13 '14 at 20:06
  • 2
    Shows that IDEs are mostly baggage... use a CLI ;-) – vonbrand Mar 13 '14 at 20:07
  • The whole point of virtual function members is that the right one is called (the one of the real object's type, even if you've got it as a (pointer/reference to a) object of the base class). OOP 101, rule 2 says "All function members are virtual". Rule 3 says "Yes, C++ allows otherwise. OOP 101's rule 2 overrules C++." – vonbrand Mar 13 '14 at 20:11
  • @taocp Well... If you could point out those errors that's kind of what I'm hoping for here. Unless you're talking about syntax errors because I just threw that together as a general idea of what I'm trying to do. I wouldn't expect what I wrote to compile. – Prototype958 Mar 13 '14 at 20:23
  • 1
    If you have internet access, try using an online IDE, something like http://ideone.com/ – Maxim Egorushkin Mar 13 '14 at 21:03
  • @MaximYegorushkin That's... Awesome... Thank you for that. I had been looking for something like that but so many other options are blocked by the company here. – Prototype958 Mar 13 '14 at 21:33

1 Answers1

0

So, utilizing IDEOne which was shared with me by Maxim (Thank you very much), I was able to confirm that I was doing this correctly

#include <iostream>
using namespace std;

class Character 
{
public:
    virtual void DrawCard() = 0;    
};

class Player: public Character
{
public:
    void DrawCard(){cout<<"Hello"<<endl;}
};

class Enemy: public Character
{
public:
    void DrawCard(){cout<<"World"<<endl;}
};

void Print(Character *ref){
    ref->DrawCard();
}

int main() {

    Player player;
    Enemy enemy;

    Print(&player);

    return 0;
}

Print(&player) and Print(&enemy) do call their respective DrawCard() functions as I hoped they would. This has definitely opened some doors to me. Thank you to those who helped.

Prototype958
  • 179
  • 8