Q:

Runtime polymorphism in C++

I have an interface, and I was trying an example on dynamic polymorphism as follows:
#include <iostream>

using namespace std;

class foo{
    public:     
        virtual void set();
        virtual void printValue();
};

class fooInt : public foo{
        private:
            int i;
        public:
            int get(){ 
                return i;
            }   
            void set(int val){ //override the set
                i = val;
            }
            void printValue(){
                cout << i << endl;
            }           
};

int main(){
    foo *dt;         //Create a base class pointer
    dt = new fooInt; //Assign a sub class reference
    dt->set(9);
}

However when I compile this, I get no matching function for call to ‘foo::set(int)’. Where am I going wrong? I tried to read this article, and I still couldn't figure out the mistake.

A:

class foo has no method set(int). It has a method set(), but no method set(int).
If you intend to override an inherited method, the superclass method and your method must have the same signature:
class foo {
  ...
  // If you really want an abstract class, the `= 0`
  //  ensures no instances can be created (makes it "pure virtual")
  virtual void set(int) = 0;
  ...
}

