Why is polymorphism useful c




















There are generally two ways by which a program can be constructed to exhibit polymorphic behavior. Namely, through composition or inheritance. Polymorphism via composition relies on 1 well-defined and narrow interfaces and 2 other objects or types containing references to things that implement those interfaces. To give a modern example, consider the following Go program. We have a generic animal interface and then two structs that implement this interface: dog and cat. Each animal has a description which is able to produce a simple description of the animal.

When animals are asked to identify themselves they invoke ask their specific description implementation for a description. This is the fundamental idea behind polymorphism behind composition. Since the description fields embedded in the animal structs implements some well-known interface in this case, just the description one , the specific instance of that field determines the behavior of the animal at runtime.

Thus, we say that polymorphic behavior is achieved by delegating to the implementation of an object. Inheritance, on the other hand, achieves polymorphic behavior through the interface of a class. Or, put another way, composition allows reuse at the object level whereas inheritance allows for reuse at the class level.

I'm trying to better understand the idea of polymorphism with examples from languages I know; is there polymorphism in C? This is Nekuromento's second example , factored in the way I consider idiomatic for object-oriented C:. C1x added generic selections, which make compile-time polymorphism via macros possible. The following example is taken from the C1x April draft, section 6. Type-generic macros for math functions were already available in C99 via the header tgmath.

Almost all implementations of runtime polymorphism in C will use function pointers, so this is the basic building block. Using function pointers you can create virtual tables and use it to create "objects" that will be treated uniformly, but behave differently at runtime. There's no intrinsic support for polymorphism in C, but there are design patterns, using function pointers, base 'class' structure casts, etc. The GTK library is good example.

I guess, you already checked Wikipedia article on polymorphism. In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. According to that definition, no, C doesn't natively support polymorphism.

For instance, there is no general function for acquiring absolute value of a number abs and fabs are for integers and doubles respectively. A real-life example of polymorphism, a person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee.

So the same person posses different behavior in different situations. This is called polymorphism. Polymorphism is considered as one of the important features of Object Oriented Programming.

All those who say programming isn't for kids, just haven't met the right mentors yet. The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future. Compile time Polymorphism Runtime Polymorphism Compile time polymorphism : This type of polymorphism is achieved by function overloading or operator overloading.

We know that this is the addition operator whose task is to add two operands. To learn operator overloading in details visit this link. In function overriding, we have two definitions of the same function, one in the superclass and one in the derived class. The decision about which function definition requires calling happens at runtime.

A virtual function is declared by keyword virtual. The return type of virtual function may be int, float, void. A virtual function is a member function in the base class. We can redefine it in a derived class. It is part of run time polymorphism. The declaration of the virtual function must be in the base class by using the keyword virtual. A virtual function is not static. The virtual function helps to tell the compiler to perform dynamic binding or late binding on the function.

This is because we will have to create a pointer to the base class that refers to all the derived objects. But, when the base class pointer contains the derived class address, the object always executes the base class function.

For resolving this problem, we use the virtual function.



0コメント

  • 1000 / 1000