Polymorphism – C++

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.

Before getting into this section, it is recommended that you have a proper understanding of pointers and class inheritance.

There are two types of polymorphism:

  • Static Polymorphism
  • Dynamic Polymorphism
Static Polymorphism: Any entity in which can exist in different physical forms at the same time. As the different types of parameters are specified in declaration of function so calls can be made at compile time. Number, type, and sequence of parameters declared for each form of the function will decide which function will get called. This is also called as Early Binding.
For example: add(int, int);  and add(float , float);
In the above example if we pass 2 integers then the first function will get called and if we pass 2 float values then the second function will get called.
Dynamic Polymorphism: When any entity change its form depending upon circumstances then that kind of polymorphism is called as Dynamic polymorphism.A function is said to exhibit dynamic polymorphism when it exists in more than one form, and calls to its various forms are resolved dynamically when the program is executed. This is also called as Late Binding.
Advertisements

Inheritance

 

As the name suggests that it is taking or acquiring some properties from somewhere if we go into its actual definition then INHERITANCE means to get some properties, characteristics or qualities from the predecessor by succession. In this post the inheritance in C++ is discussed.

The definition according to the programming is that the process by which the one class acquire the properties of objects of another class. In other words Inheritance is the mechanism deriving new class from an old one is called inheritance which is also called as derivation. This concept provides the idea of Reusability. This allows user to tailor almost the complete class without any side effects.

The different types of Inheritance are:

1)      Single

2)      Multiple

3)      Hierarchical

4)      Multilevel

5)      Hybrid

The Inheritance adds some more concepts derived class and base class. The syntax of derived class is

Class <derived-class-name> : visibility mode <base-class-name>

{

//body of derived class

};

The visibility mode part is optional here it can be private or public. If its not provided then its private.

Now when the class is publically inherited then the public part of base class becomes the public part of the derived class (in short the public part of base class is inherited to the public part of the derived class) and when the base class is privately inherited then public members of the base class become private members of the derived class.

The private members of the base class are not derived In any of the conditions given above.

Sometimes when we want in our program to inherit the private members of the base class then the C++ provides another visibility modifier, protected. Whenever the member is declared as protected, then that member is accessible by member functions to the class which is derived from that base class.

 

C++

This new topic was asked by one of blog reader and friend of mine.

So starting with C++: It was developed by Bjarne Stroustrup in 1979 at Bell Labs as an upgrade to the C language and originally named C with Classes. It name was changed to C++ in 1983. Its the middle level language as it has the features of high level language and a low level language too. As we all agree to the point that the C++ (used the increment operator in C as its an enhancement of C) is the starting of the Object Oriented Programming Language. So object Orientation can be described as “The Software modelling and development disciplines that make it easy to construct complex systems from individual components”

The first computers were programmed in binary along with mechanical switches. Earlier programming languages were inadequate for developing complex computer programs. There was no facility to reuse the code in the linear programming languages the code was duplicated and used in programs wherever it was necessary. The solution to this was structured programming in which the program is broken in number of procedures which are known as functions and different tasks are performed.

Now shifting towards the Object oriented paradigm. This is built on the foundation of Structured programming and data abstraction. The main change is that the program is designed around the data being operated on rather than the operation. As an upgrade to C the language first adding classes, then virtual functions, operator overloading, multiple inheritance, templates, and exception handling.

The main concepts introduced in Object Oriented Programming:

  1. Class: A class is a construct that is used as a template to create objects of that class. In other words its the extension of the structures in C.
  2. Objects: These are the instances of the Classes created runtime
  3. Data Abstraction and Encapsulation:  Abstraction is the concept of structured programming and used in C++ too whereas encapsulation means the wrapping up of data into single unit.
  4. Inheritance: When the derived classes are created from the classes called as base classes then it is called as inheritance
  5. Virtual Function: The concept of virtual function is the same as a function, but it does not really exist although it appears in needed places in a program.
  6. Templates: This allows us to create a function template which can be adapted for more than one type or class with no need to retype the entire code again for another type.
  7. Polymorphism: It is the ability to use function or operator in different ways.

There are some other features such as dynamic binding , constructors, easy way to handle files are also there.

The Key advantages are :

  • we can extend the use of existing classes.
  • Helps to build secure code.
  • Message passing techniques to communicate between objects.
  • Multiple instances of object can coexist.
  • Software complexity can be easily managed.
  • iostream makes it faster than stdio

Thats it for this time will explain few more things(concepts) related to C++.

Till then Good Bye.. 🙂