Introduction of Polymorphism in C++

  • Polymorphism in C++ is one of the most important features of object-oriented programming.
  • It simply means “one name multiple forms‟.
  • The word polymorphism consists of two Greek words – ‘poly’ means many/several and ‘morphs’ means form/structure, which signifies the many uses of these operators and functions in different situations.

Definition

  • Polymorphism is an operation that may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation.
  • Polymorphism is a mechanism that allows to implementation a function in different ways in different situations or environments.

Characteristics/Features of Polymorphism in C++

  • Polymorphism is the ability to use an operator or method/function in different ways.
  • Polymorphism refers to codes, operations, or objects that behave differently in different contexts.
  • Polymorphism gives different meanings to the operators or methods.
  • Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface i.e., a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ.
  • Condition to occur polymorphism, the following conditions must apply –
    • All different classes must be derived from a single base class.
    • The member function must be declared virtual in the base class. 

Examples

Suppose an expression

2 + 3           //This represents integer addition.
Again the same + operator can be used with different meanings with strings such as :
 “Coders” + “helpline”        //This represents String addition.
Once again, the same + operator can be also used for floating point addition such as :

2.321+5.021    //This represents floating point addition.

Thus, we saw that from the above-given example that a single operator “+‟ behaves differently in different contexts such as integer, string, or float referring to the concept of polymorphism. The above polymorphism concept is said to be an operator overloading type. When the existing operator or function operates on a new data type more than once, it is said to be overloaded.

Types of Polymorphism

  • C++ shows two types of polymorphism – 

(i) Static/Compile-time/Early Binding or linking polymorphism  and

(ii) Run-time/Dynamic Binding polymorphism.

  • Function overloading & operator overloading are examples of compile-time polymorphism whereas run-time polymorphism can be achieved by the use of both derived classes and virtual functions.
  • In polymorphism, a single function or an operator functioning in many ways depends upon the user to function properly.
Static/Compile-time/Early Binding Polymorphism
  • When the same operator (in operator overloading) and function (in function overloading) work differently on different arguments producing different results.
  • In function overloading, the overloaded member functions are selected for
    invoking by matching arguments, in both datatype and number of arguments. This information is known to the compiler in advance/at the compile time and, therefore, the compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early binding/static binding/static linking/compile time polymorphism.
Run-time/Dynamic Binding polymorphism.
  • When the selection of the appropriate function in a program occurs dynamically at run time, it is called as dynamic binding.
  • C++ supports run-time polymorphism by a mechanism called virtual function. It exhibits late binding.

    Benefits/Advantages of Polymorphism

    • The main advantage of polymorphism is the creation of reusable code by the programmer – classes once written, tested, and implemented can be easily reused at other required places without caring about what’s written in the case.
    • By using polymorphic variables memory is defined once, i.e. a single variable can be used to store multiple data types (integers, strings, etc.) rather than declaring a different variable again and again for each data format to be used.
    • A polymorphic program can be easily extended i.e. once an application is written using the concept of polymorphism, it can easily be extended, providing new objects that conform to the original interface. Also, It is unnecessary to recompile original programs again when a new type is added. Only re-linking is necessary to exhibit the new changes along with the old application. It is one of the greatest features of Oops.
    • It provides easier maintenance of applications.
    • It helps in achieving robustness in applications.

    Function Overriding

    • In inheritance, If a derived class defines the same function as defined in its base class, it is known as function overriding in C++.
    • It is used to achieve runtime polymorphism i.e. Overriding is a type of polymorphism.
    • It enables us to provide specific implementation of the function which is already provided by its base class.
    • It is sometimes also called Method overriding which is the ability of the inherited class to rewrite the virtual method of the base class.
    • Overriding is about the same method, and same signature but different classes connected through inheritance.
    • Constructors are not normal methods and they cannot be overridden.

    Virtual Functions

    • The word “virtual‟ means something that does not exist in reality, some sort of imaginary thing.
    • The concept of a virtual function is the same as a member function, but it does not really exist although it appears in needed places in a program.
    • The object-oriented programming language C++ implements the concept of a virtual function as a simple member function, like all member functions of the class.
    • The functionality of virtual functions can be overridden in their derived classes.
    • Polymorphism is used to give different meanings to the same concept. This is the basis for virtual function implementation.
    • When the form/nature of a member function of a class is changed at runtime. Such member functions are called virtual functions and the corresponding class is called a polymorphic class. The objects of the polymorphic class, addressed by pointers, change at runtime and respond differently to the same message.

    Pure Virtual Functions

    • It is also called do-nothing functions/dummy functions.
    • This pure virtual function is always a virtual function.
    • Pure virtual functions are functions with a null body defined in the base class.
    • This makes a derived class able to override them.
    • Any normal function cannot be declared as a pure function.
    • A pure virtual function cannot be used for any operation.
    • It cannot be used to declare an object.

    Pure Abstract/Abstract Class

    • When a class contains a pure virtual function it is called Abstract Class.
    • It cannot be used to declare any object i.e. any attempt to declare an object for an abstract class results in an error.
    • To declare pure virtual functions – 

               virtual void display()=0;

    • Here, the assignment operator is not used to assign zero to this function but rather used to instruct the compiler that the function is pure.
    • It cannot be used to declare an object.

    Demerits/Disadvantages of Polymorphism

    • Implementation of polymorphism is difficult, for the programmer in programming codes.
    • Run time polymorphism leads to performance issues because os with compilers manages all the related resources suddenly at run time.
    • Polymorphism reduces the readability of the program i.e. identifying the runtime behavior of a program is difficult.

    Loading

    Categories: C++

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.