-1

I know there are a few different ways to achieve polymorphism in c++.

I know of 3 ways to do this:

  • by using inheritance (through the use of a pointer to a base class)
  • by using virtual function
  • by using abstract classes

During a technical discussion on the topic I was told I am missing something and was left hanging...hence I asked the question here.

Is there another way in c++ to to this or is something I said wrong?

Stephan
  • 4,147
  • 1
  • 19
  • 33
Pandrei
  • 4,759
  • 3
  • 23
  • 42

2 Answers2

2

Your three ways are really just one: whether the base class is abstract is an implementation detail; you need virtual functions, which can be overridden in a derived class.

Other than that: both function overloading and templates provide a form of polymorphism as well, although it is resolved at compile time, and not run time. For that matter, you can define a class in a header file, and provide several different implementations for it, depending on compile time switches; that's also a form of polymorphism. (This is often done for system dependent code. The polymorphism is resolved as a function of the system you're compiling for.)

James Kanze
  • 146,674
  • 16
  • 175
  • 326
0

I think your discussion was related to different types of polymorphism.

  1. Compile time polymorphism - Ex: Function Overloading, Operator Overloading.
  2. Run time polymorphism - Ex: Inheritance + virtual functions + base class pointer.
rockoder
  • 747
  • 10
  • 22