VIRTUAL FUNCTIONS BY PROF ARTI KALE OBJECTIVES On
VIRTUAL FUNCTIONS BY: PROF ARTI KALE
OBJECTIVES On completion of this lecture, you will be able to: �Explore the importance of Virtual Functions. �Explain the concept of Pointers to object and more about pointers. �Define Abstract classes. �Create Virtual Destructor. �Explain Early & Late binding. �Create Container classes.
INTRODUCTION � Virtual functions is used to implement run time polymorphism in C++ and its declared and defined in the base class and redefined in a derived class. � Object pointers are used for creating objects at the run time and it is also used to acess public members of an object. � The class which contains atleast one virtual function is called as an abstract class. � Virtual Destructor is used to ensure that the derived class destructor is called before base class destructor. � Early binding occurs at the time of compilation and so its also known as compile time binding � Late binding as it occurs at the run time so it is also called run time binding.
Virtual Functions � Virtual functions is used to implement run time polymorphism in C++. � It is declared and defined in the base class and redefined in a derived class. � Its is declared using keyword virtual i following manner: Virtual return-type function-name(argument list); � Derived class redefines virtual funtions to suit its own requirement. � Virtual functions are acessed via base class pointerto achieve run time polymorphism. � It can also be acessed via objects just like other member functions.
#inlcude <iostream> Using namespace std; Class Base { public : void display() { cout<<“n Display Base”; } virtual void show() { cout<<“nshow base”; }; class Derived : public Base { public: void display() { cout<<“n Display Derived”; }; Void show() { cout<<“n show deived”; };
int main() { Base B; Derived D; Base *bptr; cout<<“n bptr points to base n” bptr=&B; bptr->diplay(); //calls base version Bptr->show(); //calls base version cout<<“nn bptr points to Derivedn”; bptr=&D; bptr->display(); //calls base version bptr()->show(); //calls derived version return 0; }
Pointers to Objects Object pointers are used for creating objects ay run time. � It is also used to access public members of an object � We can cre ate an object as follows : class employee { int id ; float salary ; Public : void accept () { cout <<“Enter the details “; Cin>> id >> salary ; } void display () { Cout<<“Employee id ”<<id; cout <<“Employee salary ”<<salary; } }; �
“this” Pointer �“this pointer” points to address of an object that invokes member function. �Every object has access to its own address through his pointer. �It is automatically paused to a member function when its called. �It is shown in program below.
class Student { int rollno ; double marks; public : Studen (int x, double y) //Step 1 { rollno=x; marks=y; } cout<<“Name”<<name; cout<<“Rollno”<<rollno; } }; void main() { Student S(1, 70); //Step 2 S. display(); //Step 3 } ptr=8 p; cout<<p. x; //access data member with object cout<<ptr. x; } //acess data member with pointer
Pointers and Classes �In C ++ we can have pointer to class members. �pointer of class type is used to point to objects. �pointer of class type is declared as follows : �class -name *pointer name;
class example { public: int x; }; Void main() { example p; example *ptr; ptr=8 p; cout<<p. x; cout<<ptr->x; } //Creates object of Class //Creates pointer of class type //Access data member with object //Access data member with pointer
Abstract Class �A class that contains atleast one pure virtual function is called as abstract class Base { public : virtual void data () = 0 ; } �Here class " Base " is abstract class as it contains one pure virtual function. �We cannot create objects of abstract class
class Base { public : virtual void data () = 0 }; : : void main () { Base b ; / / Error , objects of abstract class cannot b e created. : }
Virtual Destructor �Virtual destructor is used to ensure that the derive class destructor is called before base class destructor. �it is written as follows : �Example virtual ~ destructor~name();
class Base { public : Base () } cout<< " Base class constructor " ; } ~ Base () } }; class Derived : public Base { public: Derived() { Cout<<“Derived class constructor”: } ~Derived() { cout <<“Derived class destructor “ } }; void main() { Base *b= new Derived(); Delete *b; }
Early and Late Binding Early Binding (Static Binding): • Early binding occurs at th etime of compile time and is also known as compile time binding. • It refers to event that occurs at compile time. • Early Binding includes events such as normal function call, overloaded function calls and overloaded operators. • As all the information required for a function call is known at compile time early binding is very fast. Late Binding (Dynamic binding): • It occurs at run time and is also known as Run Time Binding. • It reffers to events that occur at run time. • These events include virtual function. • As all the informationrequired for function call is not known until run time so late binding is slow process.
- Slides: 16