Run Time Polymorphism in C
What is polymorphism?
Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have multiple traits at once is a real-world example of polymorphism. Being a father, a spouse, and an employee all at once makes one feel like a man. As a result, the same person behaves differently depending on the circumstance. Polymorphism is the term for this. One of the key components of Object-Oriented Programming is polymorphism.
Types of polymorphism
- Compile time polymorphism
- Run time polymorphism
Does c support polymorphism?
Generally, polymorphism is not supported by the C language. A characteristic of object-oriented programming (OOP) languages is polymorphism. Polymorphism is only supported by object-oriented languages. Because OOP is not a concept in C programming, polymorphism is also not a concept in C programming.
Although C doesn't naturally support polymorphism, there are design patterns that can simulate dynamic dispatch by leveraging function pointers, base 'class' (structure) casts, etc. A nice illustration is the GTK library.
Run time polymorphism in c
A binding method that can decide which function should be called at runtime based on the object type making the call would be preferable to the static binding.
Since the suitable function is chosen dynamically during runtime, this sort of binding is often referred to the as late or dynamic binding.
Oops (polymorphism) is a programming technique, not a language characteristic. Following this, you may use C to implement inheritance and polymorphism. Therefore, while C lacks classes, it does contain structures. By first establishing an instance of the base structure in the derived structure, we can create two structures and call a method from one to the other. This is simply inheritance: convert the derived struct instance to the base class (upcasting), utilize the base class's methods, or access data members. Using a function pointer, we may implement polymorphism in the same way.
Use of run time polymorphism
In order to interact with any class descended from that base without being aware of or caring about the precise type, it is necessary to design a common base class (typically one that just specifies an abstract interface). So you may create a function called furtle(A&) that works with class A, class B, or any other class descended from class A. Regardless of the actual class type, it can call the virtual functions that are overridden to do the proper action.
Even so, we can use the Add() method of Class B directly using either its object or its pointer.
Example programs in c using run-time polymorphism
So is possible even though the language does not give you as many tools to achieve it as OO-based languages do.
For instance:
Example 1
#include <stdio.h>
#include <stdlib.h>
typedef struct {
void (*eat)();
void (*say_something)();
} Animal;
void dog_eat()
{
puts("I eat cats");
}
void dog_say_something()
{
puts("Gau");
}
void cat_eat()
{
puts("I eat mice");
}
void cat_say_something()
{
puts("Meow");
}
Animal* new_dog()
{
Animal* a = malloc(sizeof(Animal));
a->eat = dog_eat;
a->say_something = dog_say_something;
return a;
}
Animal* new_cat()
{
Animal* a = malloc(sizeof(Animal));
a->eat = cat_eat;
a->say_something = cat_say_something;
return a;
}
void do_something(const Animal* a)
{
a->eat();
a->say_something();
}
int main()
{
Animal* a = new_cat();
Animal* b = new_dog();
do_something(a);
puts("*********");
do_something(b);
free(a);
free(b);
return 0;
}
Output
I eat mice
Meow
*********
I eat cats
Gau
Example 2
#include <stdio.h>
#include <stdlib.h>
struct foo;
typedef void(*member_function_t)(struct foo*);
typedef struct foo
{
member_function_t bar;
void *data;
} foo;
void bar_impl_1(foo *this)
{
//
}
void bar_impl_2(foo *this)
{
//
}
int main()
{
foo f1 = {bar_impl_1};
foo f2 = {bar_impl_2};
f1.bar(&f1); // Equivalent to bar_impl_1(&f1)
f2.bar(&f2); // Equivalent to bar_impl_2(&f2)
}
Advantages of run time polymorphism in c
- The ability to reuse codes, i.e., classes that have already been created, tested, and implemented, is helpful to programmers. greatly reduces time.
- Multiple data kinds can be stored in a single variable.
- The code is simple to debug.
Conclusion
Method overriding is used to implement runtime polymorphism. When we call the methods with their appropriate objects, everything goes smoothly. However, unexpected outcomes happen from static linking when we have a base class pointer and we call overridden methods using the base class pointer pointing to the derived class objects. We employ the idea of virtual function to get around this.