Polymorphism

What is Polymorphism?

Virtual function

Pure Virtual function

What is Polymorphism?

Polymorphism is another important OOP’s concept. Polymorphism a greek term means the ability to take more than one form. An operation may exhibit different behaviors in different instances. So polymorphism enables us to “program in the general” rather than “program in the specific”.In particular, polymorphism enables us to write programs that process objects of classes that are part of the same class hierarchy as if they are all objects of the hierarchy’s base class. With polymorphism, it is possible to design and implement systems that are easily extensible. New classes can be added with little or no modification to the generic portions of the program, as long as those classes are part of the inheritance hierarchy that the program processes generically. The only parts of a program that must be altered to accommodate new classes are those program components that require direct knowledge of the new classes that the programmer adds to the hierarchy

1.Example for Polymorphism:

#include <iostream>
using namespace std;
class Add {
public:
  int sum(int num1, int num2){
     return num1+num2;
  }
  int sum(int num1, int num2, int num3){
     return num1+num2+num3;
  }
};
int main() {
  Add obj;
  //This will call the first function
  cout<<"Output: "<<obj.sum(10, 20)<<endl;
  //This will call the second function
  cout<<"Output: "<<obj.sum(11, 22, 33);
  return 0;
}

OUTPUT:

Output: 30
Output: 66

Virtual Functions

First, we have to consider why virtual functions are useful. Suppose that a set of shape classes such as Circle, Triangle, Rectangle, Square, etc. are all derived from the base class Shape. In object-oriented programming, each of these classes might be endowed with the ability to draw itself via a function draw. Although each class has its own draw function, the function for each shape is quite different. When drawing a shape, whatever that shape may be, it would be nice to be able to treat all these shapes generically as objects of the base class Shape. Then to draw any shape, we could use a base-class Shape pointer to invoke a function draw and let the program determine dynamically which derived -class draw function to use, based on the type of the object to which the base-class Shape pointer points at any given time. An overridden function in a derived class has the same signature as the function it overrides in its base class. If we do not declare the base-class function as virtual, we can redefine that function. By contrast, if we declare the base-class function as virtual, we can override that function. We declare a virtual function by preceding the function’s prototype with the keyword virtual in the base class. For example,’

 virtual void draw() const;

would appear in base class Shape. The preceding prototype declares that function draw is a constant virtual function that takes no arguments and returns nothing.

Once a function is declared virtual, it remains virtual all the way down the inheritance hierarchy from that point, even if that function is not explicitly declared virtual when a class overrides it.

Even though certain functions are implicitly virtual because of a declaration made higher in the class hierarchy, explicitly declare these functions virtual at every level of the hierarchy to promote program clarity.

When a programmer browses a class hierarchy to locate a class to reuse, it is possible that a function in that class will exhibit virtual function behavior even though it is not explicitly declared virtual. This happens when the class inherits a virtual function from its base class, and it can lead to subtle logic errors. Such errors can be avoided by explicitly declaring all virtual functions virtual.

Pure virtual functions

Pure virtual functions normally do not provide implementations. Every concrete derived a class must override all base-class pure virtual functions and provide concrete implementations of those functions. The difference between a virtual function and a pure virtual function is that a virtual function has implementation and gives the derived class option of overriding the function; by contrast, a pure virtual function does not provide an implementation and requires the derived class to override the function

An abstract class defines a common public interface for the various classes in a class hierarchy. An abstract class typically contains one or more pure virtual functions that derived classes must override.

Attempting to instantiate an object of an abstract class causes a compilation error.

Failure to override a pure virtual function in a derived class then attempting to instantiate objects of that derived class is a compilation error.

An abstract class must have at least one pure virtual function. An abstract class also can have data members and concrete functions(including constructors and destructors), which are subject to the normal rules of inheritance by derived classes.

Translate »