Friday, 17 February 2023

Virtual Function and Polymorphism

 

Unit 5: Virtual Function and Polymorphism (8)         

6.1. Concept of Pointer

6.2  Need of virtual Function

6.3 Definition of virtual Function

6.4 Pure virtual function

6.5 Abstract Class

6.6 Container class

Practical Works:

·       Create virtual function

·       Create pure virtual function

·       Create abstract and container class

 

Polymorphism:

The word polymorphism is made up of two words: poly and morphism. The word: poly means many and morph means forms. Thus polymorphism is characteristics of being able to assign a different meaning or usage to something in different contexts. The function overloading, operator overloading, function overriding virtual functions are example of polymorphism.

1.     In function overloading, two or more function having same name but with different signatures are used.

2.     In function overriding two or more functions with same name and same signatures but one defined in base class and other in child class used.

 



                                                          Fig: Types of polymorphism

1.     Compile time polymorphism:

In this type of polymorphism, an object is bound to its function call at compile time i.e. before run time. This is also called early binding or static binding or static linking. Eg. Function overloading, operator overloading.

2.     Run time polymorphism:

The correct form of the function is selected to call on the basis of content of calling object at run time. It is called late binding dynamic binding or dynamic linking. Eg. Virtual function.

 

C++ virtual function

  • A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword.
  • It is used to tell the compiler to perform dynamic linkage or late binding on the function.
  • There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the 'virtual' function.
  • A 'virtual' is a keyword preceding the normal declaration of a function.
  • When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.

Rules of Virtual Function

  • Virtual functions must be members of some class.
  • Virtual functions cannot be static members.
  • They are accessed through object pointers.
  • They can be a friend of another class.
  • A virtual function must be defined in the base class, even though it is not used.
  • The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
  • We cannot have a virtual constructor, but we can have a virtual destructor
  1. #include <iostream.h>  
  2. #include <conio.h>  
  3. class Figure
  4. {  
  5. Protected:
  6.    Float dim1,dim2;
  7. public:  
  8.     Figure(float a, float b)
  9.               {
  10.               dim1=a;
  11.               dim2=b;
  12.                }
  13.      Virtual float Area ()
  14.               {
  15.               return 0.0;
  16.              }
  17.  };
  18. class Rectangle: public Figure
  19. {  
  20. public:  
  21.     Rectangle (float length, float breadth) : Figure (length, breadth)
  22.               {
  23.                }
  24.      float Area ()
  25.               {
  26.               return (dim1*dim2);
  27.              }
  28.  };
  29. class triangle: public Figure
  30. {  
  31. public:  
  32.     Triangle (float height, float base) : Figure (height, base)
  33.               {
  34.                }
  35.      float Area ()
  36.               {
  37.               return (dim1*dim2/2);
  38.              }
  39.  };
  40. void main ()  
  41. {  
  42.     Figure*f;  
  43.     Rectangle rect (100.5, 7.0);
  44.    Triangle  tri (10.5, 5.6);
  45. Float area_rect, area_tri;
  46. Clrscr();
  47. f =&rect;
  48. area_rect=f->Area();
  49. f =&tri;
  50. area_tri=f->Area();
  51. cout<<”Area of Rectangle:”<<area_rect<<endl;
  52. cout<<”Area of triangle:”<<area_tri<<endl;
  53. getch();
  54. }  

Output:

Area of Rectangle:703.5
Area of triangle:29.4

Pure virtual function

 

A pure virtual function in C++, also known as the do-nothing function, is a virtual function that does not perform any task. It is only used as a placeholder and does not contain any function definition (do-nothing function). It is declared in an abstract base class. These types of classes cannot declare any objects of their own. We can drive a pure virtual function in C++ using the following syntax:

 

Virtual void class_name()=0;

 

 

  1. #include <iostream.h>  
  2. #include <conio.h>  
  3. class Polygon
  4. {  
  5. public:  
  6.     virtual void Draw ( )=0;
  7. };
  8. class Rectangle: public Polygon
  9. {  
  10. public:  
  11.     void Draw ( )
  12.               {
  13.               Cou<<”Drawing a rectangle….”<<endl;
  14.                }
  15.   };
  16. class Triangle: public polygon
  17. {  
  18. public:  
  19.     void Draw ( )
  20.               {
  21.               Cou<<”Drawing a Triangle….”<<endl;
  22.                }
  23.   };
  24. class Pentagon: public polygon
  25. {  
  26. public:  
  27.     void Draw ( )
  28.               {
  29.               Cou<<”Drawing a Pentagon….”<<endl;
  30.                }
  31.   };
  32. void main ()  
  33. {  
  34.     Polygon *p[3];
  35. Rectangle rect;
  36. Triangle tri;
  37. Pentagon pent;
  38. Clrscr();
  39. P[0]=&rect;
  40. P[1]=&tri;
  41. P[2]=&pent;
  42. For (int i=0; i<3; i++)
  43. P[i]->Draw();
  44. getch();
  45. }  

Output:

Drawing a rectangle…… 
Drawing a triangle…… 
Drawing a pentagon…… 

 

Abstract class

 

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. We declare a pure virtual function by using a pure specifier (=0) in the declaration of a virtual member function in the class declaration.

Syntax:

Class AB

  1. {  
  2. public:  
  3.     virtual void f( )=0;
  4. };

 

Consider an example of shape base class with sub-classes (triangle and rectangle) that inherits the shape class.

 

  1. #include <iostream.h>  
  2. #include <conio.h>  
  3. class shape
  4. {  
  5. public:  
  6.     virtual int Area ( )=0;
  7. Void setwidth (int w)
  8.     {  
  9.      width=w;
  10.     }
  11.  
  12.     Void setheight (int h)
  13.     {  
  14.      height=h;
  15.     }
  16.    Protected:
  17.        int width;
  18.        int height;
  19. };
  20. class Rectangle: public shape
  21. {  
  22. public:  
  23.     int Area ( )
  24.               {
  25.               Return (width*height);
  26.                }
  27.   };
  28. class Triangle: public shape
  29. {  
  30. public:  
  31.     int Area ( )
  32.               {
  33.               Return (width*height);
  34.                }
  35.   };
  36. int main ()  
  37. {  
  38.  Rectangle R;
  39. Triangle T;
  40. R.setwidth (5);
  41. R.setheight(10);
  42. T.setwidth(20);
  43. T.setheight(8);
  44. Cout<<”the area of the rectangle is :”<<R.area()<<endl;
  45. Cout<<”the area of the triangle is :”<<T.area()<<endl;
  46.  
  47. }  

Container class

 

 A class is said to be a container class which is utilized for the purpose of holding objects in memory or persistent media. The purpose of container class is to hide the topology for the purpose of objects list maintenance in memory. There are two types of container class.

1.     Heterogeneous container:

A container class is known as heterogeneous container, when it contains a set of different objects,

2.     Homogenous container:

A container class is known as homogeneous container when it contains a set of similar objects.

Syntax:

  1. Class first
  2. {  
  3. .
  4. .
  5. }  ;
  6. Class second
  7. {  
  8. First f;
  9. .
  10. }  ;
  11.  

Example:

  1. #include<iostream>
  2. Class first
  3. {  
  4. Public:
  5. Void show f()
  6. {  
  7. Cout<<”hello from first class \n”;
  8. }  
  9. }  ;
  10. Class second
  11. {  
  12. First f;
  13. Public:
  14. Second ( )
  15. {  
  16. f.show();
  17. }  
  18. }  ;
  19. Int main ()
  20. {  
  21. Second s;
  22. }  
  23.  

Output:

hello from first class


No comments:

Post a Comment

Teaching Method in ICT Education | Syllabus | Seven Semester | BICTE

  Course Title: Teaching Method in ICT Education Course No.: ICT. Ed. 478                                            Nature of ...