Who heard what? Need infa, thankful in advance.
Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants AK ♦ , pavel , user194374, Kromster , fori1ton Jan 9 '17 at 16:32 .
The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Did you decide to file for coursework / interview or something? - KoVadim
- 2To begin with, it seems to me, we need to decide on the very concept of "polymorphism" - Sublihim
- Question in the ticket for the exam. There are no manuals, Google did not give an answer. - Aleksandr
- 2For example: How can I simulate OO-style polymorphism in C? - Grundy
- oneIf you understand what polymorphism is, then you can turn any structure using pointers (addresses) to objects and functions into an analogue of the ++ class, but this is a complex and fraught with difficult to catch errors - Alexander Muksimov
2 answers
Well, for example :)
#include <stdio.h> typedef struct _foo { union { char c; int i; } data; void (*print)(struct _foo *); } foo; static void print_char(foo *bar) { printf( "data.char: %c\n", bar->data.c ); } static void print_int(foo *bar) { printf( "data.int : %d\n", bar->data.i ); } int main(void) { foo int_foo = { .data.i = 1, .print = print_int }; foo char_foo = { .data.c = 'a', .print = print_char }; foo *foo_ptr; foo_ptr = &char_foo; foo_ptr->print(foo_ptr); foo_ptr = &int_foo; foo_ptr->print(foo_ptr); return 0; } I would venture to give my notes from the comments, as a kind of generalized answer to the question.
The fact is that the concept of polymorphism is a somewhat broader concept than just a call to virtual functions in C ++.
You should familiarize yourself with the concept of "polymorphism". Wikipedia, of course, does not pretend to academic, but still useful to read .
For example, the so-called. Ad hoc polymorphism is just an overload of functions. Subtype polymorphism is another matter.
Those. As far as I can understand, if this is an examination question, then it requires at least some knowledge of the theory of types and a good command of the C language.
It seems to me that you should analyze specific types of polymorphism and try to implement each type using a relatively low-level language. It is quite possible, but will have to work hard.