Sie sind auf Seite 1von 18

Virtual Functions & Polymorphism

Polymorphism

Combination of poly & morphism


many forms

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.

Rules for virtual functions P. No. 281 (Title Book)

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

understand the contrast with overloading

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(); }

Outputs: Class A Class C

Pure Virtual Functions & Abstract Classes


A virtual function without a body pure virtual function or do nothing function. A class with at least one pure virtual function abstract class. Syntax:- Pure Virtual Function

class sampleclass { private: ::::::::::: //data members of the class public: ::::::::::: //function members of the class virtual ReturnType FunctionName (arg) = 0; };

Pure Virtual Functions & Abstract Classes


Some classes exist logically but not physically. Example : Shape

Shape s; // Legal but silly..!! : Shapeless shape

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; }

Shape s; // error : variable of an abstract class

Example
Shape virtual void draw()

Circle

Triangle public void draw()

public void draw()

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

We can have virtual destructors but not virtual constructors. Problem:

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!!!

Das könnte Ihnen auch gefallen