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
- #include <iostream.h>
- #include <conio.h>
- class Figure
- {
- Protected:
- Float dim1,dim2;
- public:
- Figure(float a, float
b)
- {
- dim1=a;
- dim2=b;
- }
- Virtual float Area
()
- {
- return 0.0;
- }
- };
- class Rectangle:
public Figure
- {
- public:
- Rectangle (float
length, float breadth) : Figure (length, breadth)
- {
- }
- float Area ()
- {
- return
(dim1*dim2);
- }
- };
- class triangle:
public Figure
- {
- public:
- Triangle (float
height, float base) : Figure (height, base)
- {
- }
- float Area ()
- {
- return
(dim1*dim2/2);
- }
- };
- void main ()
- {
- Figure*f;
- Rectangle rect (100.5, 7.0);
- Triangle tri
(10.5, 5.6);
- Float area_rect, area_tri;
- Clrscr();
- f =▭
- area_rect=f->Area();
- f =&tri;
- area_tri=f->Area();
- cout<<”Area of Rectangle:”<<area_rect<<endl;
- cout<<”Area of triangle:”<<area_tri<<endl;
- getch();
- }
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;
- #include <iostream.h>
- #include <conio.h>
- class Polygon
- {
- public:
- virtual void Draw (
)=0;
- };
- class Rectangle:
public Polygon
- {
- public:
- void Draw ( )
- {
-
Cou<<”Drawing a rectangle….”<<endl;
- }
- };
- class Triangle:
public polygon
- {
- public:
- void Draw ( )
- {
-
Cou<<”Drawing a Triangle….”<<endl;
- }
- };
- class Pentagon:
public polygon
- {
- public:
- void Draw ( )
- {
-
Cou<<”Drawing a Pentagon….”<<endl;
- }
- };
- void main ()
- {
- Polygon *p[3];
- Rectangle rect;
- Triangle tri;
- Pentagon pent;
- Clrscr();
- P[0]=▭
- P[1]=&tri;
- P[2]=&pent;
- For (int i=0; i<3; i++)
- P[i]->Draw();
- getch();
- }
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
- {
- public:
- virtual void f( )=0;
- };
Consider
an example of shape base class with sub-classes (triangle and
rectangle) that inherits the shape class.
- #include <iostream.h>
- #include <conio.h>
- class shape
- {
- public:
- virtual int Area (
)=0;
- Void setwidth (int w)
- {
- width=w;
- }
- Void setheight (int
h)
- {
- height=h;
- }
- Protected:
- int width;
- int height;
- };
- class Rectangle:
public shape
- {
- public:
- int Area ( )
- {
- Return
(width*height);
- }
- };
- class Triangle:
public shape
- {
- public:
- int Area ( )
- {
- Return
(width*height);
- }
- };
- int main ()
- {
- Rectangle R;
- Triangle T;
- R.setwidth (5);
- R.setheight(10);
- T.setwidth(20);
- T.setheight(8);
- Cout<<”the area of the rectangle is
:”<<R.area()<<endl;
- Cout<<”the area of the triangle is
:”<<T.area()<<endl;
- }
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:
- Class first
- {
- .
- .
- } ;
- Class second
- {
- First f;
- .
- } ;
Example:
- #include<iostream>
- Class first
- {
- Public:
- Void show f()
- {
- Cout<<”hello from first class \n”;
- }
- } ;
- Class second
- {
- First f;
- Public:
- Second ( )
- {
- f.show();
- }
- } ;
- Int main ()
- {
- Second s;
- }
Output:
hello
from first class
No comments:
Post a Comment