You are on page 1of 13

Session Objectives

Define the word polymorphism


Virtual Function virtual Base class Abstract class

It is one of pillars of object oriented programming.In an object oriented language polymorphism allows a programmer to purse a course of action by sending a message to an object without concerning about how the software system is to implement the action (i.e One function works as different in different place)

Without Using Virtual Keyword


#include<iostream.h> class base { public: void getdata(); void display(); };

cin>>name; cout<<"Enter the roll no"<<endl; cin>>rollno; } void derivedB::display() { cout<<"The name is"<<name<<endl; cout<<"The roll no is"<<rollno<<endl; } void main() { clrscr(); base *ptr; derivedB obj; ptr=&obj; ptr->getdata(); ptr->display(); getch(); }

class derivedB: public base { private: long int rollno; char name[20]; public: void getdata(); void display(); };
void base::getdata() { cout<<"base class getdata function"; } void base::display() { cout<<"base class display function"; } void derivedB::getdata() { cout<<"Enter the name"<<endl;

Virtual means existing in effect but not in reality.


It provide a way for a program to decide, when it is running, what function to call. It allows greater flexibility in performing the same kinds of action on different kinds of objects. It actually holds pointers to a variety of derived types It is defined and declared in the base class and is then used by several derived functions by getting up pointers.

IMPORTANT CONCEPTS OF VIRTUAL FUNCTIONS


1.EARLY OR STATIC BINDING - at compile time rather than run time.
2.LATE OR DYNAMIC BINDING - it is the process of determining the addresses of functions at runtime rather than at compile time.

With Using virtual Keyword


#include<iostream.h> #include<conio.h> class base { public: virtual void getdata()=0; virtual void display()=0; }; class derivedB: public base { private: long int rollno; char name[20]; public: void getdata(); void display(); };

void derivedB::display() { cout<<"The name is"<<name<<endl; cout<<"The roll no is"<<rollno<<endl; } void main() { clrscr(); base *ptr; derivedB obj; ptr=&obj; ptr->getdata(); ptr->display(); getch(); }

void derivedB::getdata() { cout<<"Enter the name"<<endl; cin>>name; cout<<"Enter the roll no"<<endl; cin>>rollno; }

This function should be declared in the base class specifications It can be overloaded It should be a member function of a class There can be virtual destructors Constructors cannot be declared as virtual functions This function may or may not have function body It cannot be an ordinary stand alone function These functions are to be accessed by base class pointers If there is no code defined for a virtual function in derived class, then base class function is invoked

A body less virtual function is otherwise called. Simply defined as virtual functions with no body we use the pointer of base class to refer all the derived objects It is a normal practice to have a function in base class just for implementing virtual function but the actual coding will be done in derived class only and also the class cannot be used for creating objects.

Polymorphism Using Array


#include<iostream.h> #include<conio.h> class base { public: virtual void show()=0; }; class base1:public base { public: void show(){ cout<<"\n base1 called";} }; class base2:public base { public: void show(){ cout<<"\n base2 called"; } };

void main() { base *list[2]; base1 b; base2 b1; clrscr(); list[0]=&b; list[1]=&b1; list[0]->show(); list[1]->show(); getch(); }

Abstract classes
A class that contains atleast one pure virtual function is said to be an abstract class.

NOTE : you can use an

abstract class only act as a base class for deriving other classes but does not create objects
#include<iostream.h> #include<conio.h> class book { private : int x; public : book() { x=0; }

virtual void getdata()=0; virtual void putdata()=0; }; void main() { clrscr(); cout<<"end"; getch(); }

Polymorphism is defined as the ability of objects of different classes related by inheritance to respond differently to the same member function or operator. A virtual function is one that does not really exist but never less appears real to some parts of the program. It provides a way to decide , when it is running what function to call.

The function call resolved at compile time is called as static


binding

The function call resolved at runtime is called as dynamic binding

The virtual function that has no body is called as pure


virtual function The virtual function that is equated to zero is called as a null virtual function If a class contains one or more pure virtual functions, it is

an abstract base class.

You might also like