Argumentos predeterminados en función y polimorfismo [cerrado]

  1. Why can default arguments in C++ functions be considered polymorphism?

  2. Why is it consider ad-hoc polymorphism and universal polymorphism?

I think that it can be considered as an ad-hoc polymorphism as its like overloading functions(?), but in what way is it universal polymorphism?

preguntado el 09 de enero de 11 a las 11:01

lots of words... for one of the C/C++ feature I use the less. -

¿Qué es? polimorfismo ad-hoc y universal polymorphism? Never heard of them! -

@Nawaz: I Googled for it, and I found things like this:…... But no, I'd never heard of it either! -

Smells like homework to me, that question is way too buzzword-heavy to be a real question. -

@Oli: Well that's why i said it's teorético. I know both terms but still can't see why it can be considered as universal polymorphism. -

2 Respuestas

If something is polymorphic, it has many shapes.

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.

As este link explains, there are different types of polymorphism. Ad-hoc polymorphism is what we call it when an interface defines different implementations depending on a limited range of individually specified types and combinations. Function overloading and default parameters is a great example of this.

Universal polymorphism is split into parametric polymorphism (when an interface is type independent - templates is a good example of this) and inclusion polymorphism (when one data type can be treated as another, i.e. the typical OOP polymorphism where a derived class may be treated as a base class).

When it comes to the default function arguments in C++, the ad-hoc polymorphism is quite obvious. The universal polymorphism might come from the fact that you can use C++'s inclusion polymorphic features with the function arguments:

void a_function (const char* print_me, Car* pCar = NULL)
    std::cout << print_me << std::endl;

Now, we could call the function like this:

a_function("Hello World!");

and it would simply print "Hello World!", or we can call it like this:

Car mycar;
a_function("Hello World!", &mycar);

and it would also drive the car. That is ad-hoc polymorphism. Due to C++'s inclusion polymorphism you can also do this:

class DieselTruck : public Car
     // ...

y llámalo así:

DieselTruck mytruck;
a_function("Hello Earth!", &mytruck);

and it would drive the truck (if the classes were correctly set up, of course - virtual functions and all that). So you can mix ad-hoc and universal polymorphism in functions that way. Whether this is what your professor is thinking about or not, I do not know. It is not directly connected to default arguments - i.e. you can do this perfectly fine without them, but then again, I can't see how you could accomplish universal polymorphism in C++ using SOLO default arguments.

Respondido el 09 de enero de 11 a las 16:01

Thanks a lot! I also had a feeling it has something to do with the inclusion polymorphism. Again, many thanks - well explained. - user550413

1) It can be considered polymorphism because you're effectively providing different ways of calling the same function. Consider:

int func(int a = 1, int b = 2, int c = 3, int d = 4);

I can call this the following ways:

func(5, 6);
func(5, 6, 7);
func(5, 6, 7, 8);

This way, it's similar to function overloading if we had provided these 5 different methods without any default arguments:

int func();
int func(int a);
int func(int a, int b);
int func(int a, int b, int c);
int func(int a, int b, int c, int d);

2) I, too, had not come across the term polimorfismo ad-hoc until today, but my opinion is that it's ad-hoc because the number of types we can use, and the number of ways you can use it are all pre-determined.

Respondido el 09 de enero de 11 a las 14:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.