0

I wonder if this is an example of polymorphism in c++. I checked examples in OOP but I didn't find any in pure procedural way of programming. Thank you in advance for your answers!

#include <iostream>
using namespace std;

int return_value(int a) {
return 5;
}
char return_value(char b) {
return 'a';
}
int main() {
    int a = 1;
    char b = 'c';
    cout << return_value(a);
    cout << return_value(b);
    return 0;
}
XraySensei
  • 167
  • 7
  • 1
    it isn't polymorphism. – Joseph D. Jun 01 '18 at 06:11
  • 1
    That isn't polymorphism; it's *overloading*. There is an entire web of examples of polymorphism out there; all it takes is looking. – WhozCraig Jun 01 '18 at 06:11
  • I found a lot of examples of course... But I didn't know if my example is example of polymorphism... Based on the explanation (one of many) that one function can have many shapes... It could be... – XraySensei Jun 01 '18 at 06:14

4 Answers4

2

Polymorphism means one name many forms. In that sense even the function overloading is a kind of polymorphism, as same name provides you different functionalities.

Some categorize polymorphism as Compile-time polymorphism & Run-time polymorphism. The function overloading then is a case of Compile-time polymorphism. The virtual functions are example of runtime polymorphism.

So yes, your code is an example of compile-time polymorphism.

For more details on differences between runtime and compile-time polymorphism, See What is the difference between dynamic and static polymorphism in Java?

Nitesh
  • 2,681
  • 4
  • 27
  • 45
0

That's just an overloaded function, it could be considered polymorphism in some way. But, here you can find something more about polymorphism:

Polymorphism in c++

Federico
  • 743
  • 9
  • 22
0

It's not a polymorphism, here you may found polymorphism and its variations .

  • @JanezMurn Overloading means creating methods with same name but different parameters. Overriding means re-defining body of a method of superclass in a subclass to change behavior of a method. Polymorphism is a wide concept which includes overriding and overloading and much more in it's scope. [see here](https://stackoverflow.com/questions/12893907/are-polymorphism-overloading-and-overriding-similar-concepts) – Kartik Maheshwari Jun 01 '18 at 06:26
0

This could be considered as compile time polymorphic. An example for run time polymorphism is using virtual functions in base class and override those functions in the extended class.

Destructor
  • 523
  • 1
  • 3
  • 13