4
class Method {
  public:    
  virtual void Rum();
};
class Euler : public Method {
  virtual void Rum() {
    printf("ahoj\n");
  }    
};
class Kutta : public Method {
  virtual void Rum() {
    printf("ahoj2\n");
  }    
};
class Simulator {
  public:
  Method *pointer;
  Simulator();
  void setmethod(Method m) { pointer = &m; }
};

int main() {
  Simulator s;
  s.setmethod(new Kutta());
  s.pointer->Rum();
  s.setmethod(new Euler());
  s.pointer->Rum();
}

I hope this example understandable enough. I tried to apply the principle of inheritance but I get these errors: (OOP stuff seems to be a little bit messed in my head)

prog.cpp: In function ‘int main()’:
prog.cpp:26: error: no matching function for call to ‘Simulator::setmethod(Kutta*)’
prog.cpp:21: note: candidates are: void Simulator::setmethod(Method)
prog.cpp:28: error: no matching function for call to ‘Simulator::setmethod(Euler*)’
prog.cpp:21: note: candidates are: void Simulator::setmethod(Method)

So what is the correct way to pass child instead of its parent? Thanks!

andre
  • 7,018
  • 4
  • 43
  • 75
milanseitler
  • 765
  • 1
  • 7
  • 21

2 Answers2

6

Signature of your void setmethod(Method m) isn't correct. It must be void setmethod(Method* m) to match your invocation.

As a side note, you need a reference or a pointer in your method for the polymorphism to work - that means that you can't pass the argument to setmethod by value and expect the polymorphism to work.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
4

This code will work:

#include <stdio.h>                                                                                                                                                                                                   

class Method {
  public:    
  virtual void Rum() = 0;
};
class Euler : public Method {
  virtual void Rum() {
    printf("ahoj\n");
  }    
};
class Kutta : public Method {
  virtual void Rum() {
    printf("ahoj2\n");
  }    
  virtual void Rum2() {
    printf("ahoj2blah\n");
  }    
};
class Simulator {
  public:
  Method *pointer;
//  Simulator(); // this was only declaration...
  void setmethod(Method* m) { pointer = m; }
};

int main() {
  Simulator s;
  s.setmethod(new Euler());
  s.pointer->Rum();
  s.setmethod(new Kutta());
  s.pointer->Rum();
//  s.pointer->Rum2(); //you can use only functions from Method
}

First of all you should define Rum function in Method.
Also there was two reasons why void setmethod(Method m) was incorrect:

  1. new always returns pointer to object
  2. and the most important one:
    Compiller expected object with Method type, so it allocated sizeof(Method) bytes for argument.
    Unfortunatelly inherited objects very often has additional variables, so thier size is greater than size thier parents. It's why function cannot accept objects whitch inhereted types.
    Good solution is passing pointer to functions, which always has 8 bytes. Unfortunatelly you can use only method and variables defined in class from which we derived the other one, but when function, which we want to invoke, was virtual, the overridden one will be call.
Jakub Kuszneruk
  • 1,188
  • 1
  • 12
  • 37