So, I need to define a \"Static Polymorphic Subprogram\" but in the book this is
ID: 3605173 • Letter: S
Question
So, I need to define a "Static Polymorphic Subprogram" but in the book this is all I find about this topic:
I thought about definying it as: "Takes parameters of static types on different applications" but I am not sure that the addition of the word "static" will lead to this definition... Help please!
Software reuse can be an important contributor to software productivitv. One wav to increase the reusability of software is to lessen the need to create dif ferent subprograms that implement the same algorithm on different types of data. For example, a programmer should not need to write four different sort subprograms to sort four arrays that differ only in element type. A polymorphic subprogram takes parameters of different types on dif- ferent activations. Overloaded subprograms provide a particular kind of poly- morphism called ad hoc polymorphism. Overloaded subprograms need not behave similarl v. Languages that support object-oriented programming usually support sub- type polymorphism. Subtype polymorphism means that a variable of type T can access any object of type T or any type derived from T. A more general kind of polymorphism is provided by the methods of Python and Ruby. Recall that variables in these languages do not have types, so formal parameters do not have types. Therefore, a method will work for any type of actual parameter, as long as the operators used on the formal parameters in the method are defined Parametric polymorphism is provided by a subprogram that takes generic parameters that are used in type expressions that describe the types of the parameters of the subprogram. Different instantiations of such subprograms can be given different generic parameters, producing subprograms that take different types of parameters. Parametric definitions of subprograms all behave the same. Parametrically polymorphic subprograms are often called generic subprograms. C++, Java 5.0+, CH 2005 +, and F# provide a kind of compile-time parametric polymorphismExplanation / Answer
Polymorphism :
The general meaning of polymorphism is the ability to take many forms based on the context of usage. It is one of the main Object Oriented Programming Concept( OOP ).
Now coming to the types of polymorphism :
1) Static polymorphism.
2) Dynamic Polymorphism
Staic Polymorphism:
This kind of polymorphism is identified by the compiler at the time of compiling. So the word static is used ( before the execution og program ). This is called static polymorphism (compile time polymorphism).
For example : overloading in C++.
take two method prototypes :
void sum(int a, int b, int c);
and
void sum (int a ,int b);
Here both do the same functionality, both have the same name and same return type except for the number of input arguments.
The first one calculates the sum of three numbers, second one calculates sum of two numbers.
Compiler detects these at the compile time itself and binds the function definition for function call at the compile time.
This is called static polymorphism.
Dynamic Polymorphism:
This is the opposite of Static polymorphism. The polymorphism is reflected at the time of execution. So it is called Dynamic polymorphism or Run time polymorphism.
For example: take virtual functions in C++.
let's assume a method print() in Base class declared as virtual which prints "In Base class" ( base classname : Base ).
Now there is a child class ( child class name : Child ) which extends the base class. And it has the same method print() which prints "In Child class".
Now if the function call is like below then base class method is called.
Base *obj = new Base();
obj -> print(); /* prints "In Base class"
Now if you call like this, child class method is called,
Base *obj = new Child();
obj->print(); /* prints "In Child class" */
Here the method is binded to the object at the run time based on the reference.
This is called dynamic polymorphism.
This is the difference between the static and dynamic polymorphism.
-------------------------------------------------------------------------------------------
Now let's discuus the terms in the image.
Ad hoc polymorphism : It is same as the overloading concept discussed above.
Sub Type Polymorphism: It is same as the dynamic polymorphism discussed above.
Parametric polymorphism : It is the type that we are going to discuus now :
This is called generic programming:
generic programming in C++ is implemented using templates.
example :
template<class T>
void sum(T a, T b);
Here sum is to calculate the sum of two variables that are of type T. T may be user defined or primitive data type.
Now the type of parameter is known at the time of compile time. This is called static polymorphism.
------------------------------------------------------------------------------------------------------------------------------------------
/* hope this helps */
/* if any queries please comment */
/* thank you */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.