Topics       Polymorphism             1.Compile Time   Polymorphism          2.Runtime polymorphism       Virtual function        ...

Polymorphism and Virtual function in C++

/
0 Comments

  Topics
      Polymorphism  
         1.Compile Time Polymorphism
         2.Runtime polymorphism
      Virtual function   
        1. Pure virtual function   
        2.Abstract class


Polymorphism 
• Polymorphism means the ability to take more than one form.
• It allows a single name to be used for more than one related purpose.
• It means the ability of operators and functions to act differently in different situations.

Different type of Polymorphism
Compile time: 
• Compile time polymorphism is function and operator overloading. 

Function Overloading: 
• Function overloading is the practice of declaring the same function with different signatures. 
• The same function name will be used with the different number of parameters and parameters of the different type.
• Overloading of functions with different return type is not allowed.
Operator Overloading: 
• Operator overloading is the ability to tell the compiler how to perform a certain operation based on its corresponding operator's data type. 
• Like + performs addition of two integer numbers, concatenation of two string variables and works totally different when used with objects of time class. 

Dynamic Binding (Late Binding): 
• Dynamic binding is the linking of a routine or object at runtime based on the conditions at that moment. 
• It means that the code associated with a given procedure call is not known until the time of the call. 
• At run-time, the code matching the object under current reference will be called. 

What is the pointer?
  • the pointer is one kind of variable which use to store the address of another variable.
  • The pointer is almost same as reference variable because reference variable also stores the address of another variable.
  • The difference between reference variable and pointer is we can use the pointer as a class object but we cannot use the reference variable as a class object.
  • We use *(star symbol) to declare the pointer.And write * in front of the variable name.
  • Syntax of pointer
                      datatype *pointer-name; // pointer as variable
                      class-name *pointer-name; // pointer as a class object
  • We can access class member using pointer as object using ( ->) arrow or point symbol.
  • Syntax 
                 class-name *p;// pointer p declaration asobject
                  p->getdata();// p pointer accessing getdata() function
                  p->a=5;        //p pointer accessing a variable
  • Example is below                

Compile Time  polymorphism :
  • In compile time polymorphism occur on overriding .when we inherite child class from parent class.
  • In compile, time polymorphism compiler just read the declaration of the pointer as the object of class but the compiler is not read the address of which is stored in pointer this is called as compile time polymorphism.
  • In compile time polymorphism we want to run parent class function but it runs child class function.
  • To solve this problem we use runtime polymorphism.
  • Example:
#include<iostream.h>
#include<conio.h>
class a{
public:
void fun() //parent class function
{
cout<<"parent function";
}
};
class b:public a{
public:
void fun() // child class function
{
cout<<"child function";
}
};
void main()
{
a a1;            // creating class a object a1
b *ptr;         //decleraing pointer as class b object
ptr=&a1;       // storing address of  a1 object in pointer
ptr->fun();  // trying to execute fun()  function of parent means a class
getch();
}

Output:
child function

Result :
  • First, we have declared the pointer as class b object.
  • Pointer use to store the address of another variable and object that's why we have stored the address of a1 object.
  • But when we call fun() function of a class using b class pointer at that time fun() function of the b class run.
  • That is compiled time polymorphism
To understand Runtime polymorphism we need to learn about virtual function.

What is the virtual function? 
It is a run time polymorphism. 
• Base class and derived class have same function name and base class pointer is assigned address of 
derived class object then also pointer will execute base class function. 
• To execute function of derived class, we have to declare function of base class as virtual. 
• To declare virtual function just uses keyword virtual preceding its normal function declaration. 
• After making the virtual function, the compiler will determine which function to execute at runtime on the basis of assigned address to the pointer of the base class. 

Rules for virtual function 
1.The virtual functions must be the member of any class. 
2.They cannot be static members. 
3. They are accessed by using object pointers. 
4.A virtual function can be a friend of another class. 
5.A virtual function in a base class must be defined, even though it may not be used. 
6.If two functions with the same name have different prototypes, C++ considers them as overloaded 
    functions and the virtual function mechanism is ignored. 
7.We cannot have virtual constructors, but we can have virtual destructors. 
8.The derived class pointer cannot point to the object of the base class. 
9. When a base pointer points to a derived class, then also it is incremented or decremented only 
    relative to its base type. 
   Therefore we should not use this method to move the pointer to the next object. 
10. If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class. 
Example of virtual function and runtime polymorphism will be same.


What is Run time polymorphism?
• We must access virtual functions through the use of a pointer declared as a pointer to the base class. 
• We can also use object name and dot operator to call virtual functions. 
• But, runtime polymorphism using virtual functions is achieved only when a virtual function is accessed through a pointer to the base class. 

Example of virtual function and  Run time polymorphism
#include<iostream.h>
#include<conio.h>
class a{
public:
virtual void fun()             // Virtual  function decleraed
{
cout<<"parent function";
}
};
class b:public a{
public:
void fun()                  // child class function
{
cout<<"child function";
}
};
void main()
{
a a1;                  // creating class a object a1
b *ptr;              //decleraing pointer as class b object
ptr=&a1;       // storing address of  a1 object in pointer
ptr->fun();    // trying to execute fun()  function of parent means a class
getch();
}

Output:
parent function

Result:
now it will run parent function not child function.

What is pure virtual functions?
• A pure virtual function means 'do nothing' function. 
• We can say empty function. A pure virtual function has no definition relative to the base class. 
• Programmers have to redefine the pure virtual function in the derived class because it has no definition in the base class. 
• A class containing pure virtual function cannot be used to create any direct objects of its own. 
• This type of class is also called as the abstract class. 
Syntax: 
virtual display ( ) =0; OR virtual display(){} 

What is an abstract class?
  • If any class contain pure virtual function or null function then that class is an abstract class.
  • If any class is abstract we cannot create an object of that class and memory will not be allocated to that class.
  • The only way is to access that class member is to inherit that class into subclass. then we can access that class using cub class object.




You may also like

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts