16virtual Functions & Polymorphism
16virtual Functions & Polymorphism
Polymorphism
Allows defining various forms of a single function that can be shared by various objects to perform the identical operations. Implemented at compile & run time.
function overloading virtual functions & operator overloading
Contd
Binding is the process of tying the function call to function definition. When binding happens at compile time early binding. When binding happens at run time late binding.
Static Binding
When the type of a formal parameter is a parent class, the argument used can be: the same type as the formal parameter, or, any derived class type.
Static binding is the compile-time determination of which function to call for a particular object based on the type of the formal parameter When pass-by-value is used, static binding occurs
Can we do better?
void Print (Time someTime ) //pass an object by value { cout << Time is ; someTime.Write ( ) ; // Time cout << endl ; }
:: write()
CLIENT CODE
Time startTime ( 8, 30, 0 ) ; ExtTime endTime (10, 45, 0, CST) ; Print ( startTime ) ; Print ( endTime ) ;
5
OUTPUT
Time is 08:30:00 Time is 10:45:00
Dynamic Binding
Is the run-time determination of which function to call for a particular object of a derived class based on the type of the argument Declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding Dynamic binding requires pass-by-reference
Polymorphism Summary:
When you use virtual functions, compiler store additional information about the types of object available and created Polymorphism is supported at this additional overhead Important :
virtual functions work only with pointers/references Not with objects even if the function is virtual If a class declares any virtual methods, the destructor of the class should be declared as virtual as well.
Contd
To implement run time polymorphism, you need to make a function virtual & access the virtual function through pointer to the base class. A function in a class is preceded by virtual becomes virtual function.
Virtual Functions
A member function whose function declaration is preceded by virtual keyword virtual function. These functions are defined in a base class in the public section & they provide a mechanism by which the derived classes can override it. virtual member function in any derived class this is called overriding
Syntax
class sampleclass { private: ::::::::::: //data members of the class public: ::::::::::: //function members of the class virtual ReturnType FunctionName (arg) { :::::::::::: //body of the virtual function } };
Example
class A { public: virtual void f() { cout << "Class A" << endl; } }; class B: public A { public: void f(int) { cout << "Class B" << endl; } }; class C: public B { public: void f() { cout << "Class C" << endl; } };
Output
int main() { B b; C c; A* pa1 = &b; A* pa2 = &c; // b.f(); pa1->f(); pa2->f(); }
class sampleclass { private: ::::::::::: //data members of the class public: ::::::::::: //function members of the class virtual ReturnType FunctionName (arg) = 0; };
Shape makes sense only as a base of some classes derived from it. Serves as a category Hence instantiation of such a class must be prevented
A class with one or more pure virtual functions is an Abstract Class. Objects of abstract class cant be created
class Shape //Abstract { public : //Pure virtual Function virtual void draw() = 0; }
Example
Shape virtual void draw()
Circle
A pure virtual function not defined in the derived class remains a pure virtual function. Hence derived class also becomes abstract
class Circle : public Shape { //No draw() - Abstract public : void print(){ cout << I am a circle << endl; } class Rectangle : public Shape { public : void draw(){ // Override Shape::draw() cout << Drawing Rectangle << endl; } Rectangle r; // Valid Circle c; // error : variable of an abstract class
Virtual Destructors
If base-class pointer to a derived object is deleted, the base-class destructor will act on the object
Solution:
Declare a virtual base-class destructor Now, the appropriate destructor will be called
Thank You!!!