0% found this document useful (0 votes)
84 views

Unit - III (Virtual Functions) : Static Binding

The document discusses virtual functions in C++. It defines static binding as function binding during compilation, while dynamic binding refers to function binding during runtime. Virtual functions allow for dynamic binding by using late binding techniques. Making a function virtual involves adding the virtual keyword. This allows polymorphism, where a derived class can override a base class function. Pure virtual functions only have a declaration and no definition, making a class abstract. Virtual destructors are also discussed to ensure proper memory cleanup.

Uploaded by

anshulgarg171186
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Unit - III (Virtual Functions) : Static Binding

The document discusses virtual functions in C++. It defines static binding as function binding during compilation, while dynamic binding refers to function binding during runtime. Virtual functions allow for dynamic binding by using late binding techniques. Making a function virtual involves adding the virtual keyword. This allows polymorphism, where a derived class can override a base class function. Pure virtual functions only have a declaration and no definition, making a class abstract. Virtual destructors are also discussed to ensure proper memory cleanup.

Uploaded by

anshulgarg171186
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

C++ Programming

Virtual Functions

Unit III (Virtual Functions)


Static Binding

In a C++ program, choosing a function in a normal way during its compilation time is called static binding. The static binding is also referred as early binding or static linkage. During compilation time of any program, C++ compiler determines which function is to be used based on the parameters passed to the function or function's return type. After determining the correct function, the compiler substitute it for each invocation (function call). Such compiler based substitutions are called static binding. For e.g. Program 1 : WAP to demonstrate operation of static binding of the member function of the class. In this program, pointers are used to access the objects of base and derived classes. #include<iostream.h> #include<conio.h> class base { int x,y; public: void getdata(); void display(); }; class derived : public base { char a,b; public: void getdata(); void display(); }; void base :: getdata() { cout<<"Enter any two integers : "; cin>>x>>y; } void base :: display() { cout<<"\nThe entered integers are : " <<x<<"\t"<<y; }

B. Tech. - IVth Semester

Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

void derived :: getdata() { cout<<"Enter any two characters : "; cin>>a>>b; } void derived :: display() { cout<<"\nThe entered characters are : " <<a<<"\t"<<b; } void main() { clrscr(); base *ptr; derived obj2; ptr = &obj2; ptr -> getdata(); ptr -> display(); getch(); } OUTPUT : Enter any two integers : 5 4 The entered integers are : 5 4 Program 2 : Nonvirtual function (Demonstration of compile time binding using array of pointers ) #include<iostream.h> #include<conio.h> class baseA { public : void display() { cout<<"One \n"; } }; class derivedB : public baseA { public: void display() {

B. Tech. - IVth Semester

Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

cout<<"Two \n"; } }; class derivedC : public derivedB { public : void display() { cout<<"Three \n"; } }; void main() { clrscr(); // define three objects baseA obja; derivedB objb; derivedC objc; baseA *ptr[3]; // define an array of pointers to baseA ptr[0] = &obja; ptr[1] = &objb; ptr[2] = &objc; for(int i=0;i<=2;i++) ptr[i]->display(); //same message for all objects getch(); } OUTPUT : One One One

Late Binding (Dynamic Binding) :

Late binding is the binding/linking of functions during the run time of the program. In other words, we can say that choosing the function during execution time is called late binding. It is also referred as Dynamic binding or Dynamic linkage. It is very much dependent on virtual functions. In this technique, pointers are used for accessing member functions. This is done by assigning the address of derived class object to the pointer. It supports the concept of polymorphism.

B. Tech. - IVth Semester

Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

Program 3 : To demonstrate the operation of late binding of member function of the class. #include<iostream.h> #include<conio.h> class base { int x,y; public: virtual void getdata(); virtual void display(); }; class derived : public base { char a,b; public: void getdata(); void display(); }; void base :: getdata() { cout<<"Enter any two integers : "; cin>>x>>y; } void base :: display() { cout<<"\nThe entered integers are : " <<x<<"\t"<<y; } void derived :: getdata() { cout<<"Enter any two characters : "; cin>>a>>b; } void derived :: display() { cout<<"\nThe entered characters are : " <<a<<"\t"<<b; } void main() { clrscr();

B. Tech. - IVth Semester

Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

class base obj1; class derived obj2; class base *ptr; ptr = &obj1; ptr = &obj2; ptr -> getdata(); ptr -> display(); getch(); }

Virtual Functions :

In static binding, when the functions with the same name in base and derived class are accessed by a base class pointer, the functions of only base class are accessed, even if we pass the address of the derived class to the base class pointer. If we want to access the member functions of derived class, where the name of both base and derived class member functions are same, the concept of virtual function come into use. Virtual function play an important role in object oriented programming. Ordinary functions used the concept of static(early) binding. However, virtual functions help in late (dynamic) binding. A virtual function is one that does not really exist but it appears in some parts of a program. The concept of pointers play a vital role in virtual functions. To make a member function virtual, the keyword virtual is used before the function definition. The general syntax of the virtual function declaration is given below : class user_defined_name { private : ________________________ public : virtual return_type function1(arguments); virtual return_type function2(arguments); virtual return_type function3(arguments); ________________________ _________________________ }; In the above given syntax, function1,function2 and function3 are virtual functions. The compiler differentiate the ordinary and virtual functions by the keyword virtual which is written before the virtual functions. For e.g. class demo { private : int a,b;
B. Tech. - IVth Semester Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

public : virtual void input(); virtual int sum(); virtual void output(); }; The virtual functions employ late binding by allocating memory space during execution time and not during compilation time. In case of virtual functions, when a pointer of base class is defined in main() function and derived class object's address is passed on to the base class pointer, then calling the overridden function will invoke the derived class member function and not the base class member function. The virtual functions are the base of late binding and are closely related to the concept of polymorphism. Program 4 : WAP to demonstrate the use of virtual function with the help of pointers #include<iostream.h> #include<conio.h> class base { public : virtual void display(); }; class derived : public base { public : void display(); }; void base :: display() { cout<<"VIRTUAL FUNCTION OF BASE CLASS"; } void derived :: display() { cout<<"ORDINARY FUNCTION OF DERIVED CLASS"; } void main() { clrscr(); base obj; base *ptr; derived obj;
B. Tech. - IVth Semester Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

ptr=&obj; ptr->display(); getch(); }

Polymorphism :

Base class member functions can be overridden by defining a derived class member function with the same name as that of the base class member function. This concept is known as Polymorphism. The concept of polymorphism is very much dependent on the late binding of function technique. In other words, polymorphism is implemented using Virtual functions. The word poly is a Greek word meaning many and morphism means forms. Thus, polymorphism means many forms. Polymorphism is a process of defining a number of objects from different classes into a group and call the member function to carry out the operations of the objects using different function calls. The keyword virtual is used to perform the polymorphism. The keyword virtual tells the compiler which function to choose during the execution of the program. So we can define polymorphism as the Late or Run time binding of member functions of the classes using virtual functions. The pointers are used in this concept for passing the address of derived class object and access its functions. Therefore, polymorphism refers to the run time binding to a pointer to a method/ member function. Actually polymorphism is nothing but late binding of member functions.

Pure Virtual Functions :

A pure virtual function is a type of function which has only a function declaration. It does not have the function definition. Consider the following example : Program 5 : WAP to demonstrate the concept of Pure virtual function. #include<iostream.h> #include<conio.h> class base { int x,y; public: virtual void getdata(); virtual void display(); }; class derived : public base
B. Tech. - IVth Semester Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

{ char a,b; public: void getdata(); void display(); }; void base :: getdata() { } void base :: display() { } void derived :: getdata() { cout<<"Enter any two characters : "; cin>>a>>b; } void derived :: display() { cout<<"\nThe entered characters are : " <<a<<"\t"<<b; } void main() { clrscr(); class base obj1; class derived obj2; class base *ptr; ptr = &obj1; ptr = &obj2; ptr -> getdata(); ptr -> display(); getch(); } A pure virtual function can also have the following format, when a virtual function is declared within the class definition itself. The virtual function may be equated to zero if it does not have a function definition. For example : #include<iostream.h>

B. Tech. - IVth Semester

Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

#include<conio.h> class base { private : int x; float y; public : virtual void getdata( ) = 0; virtual void display ( ) = 0; }; class derived : public base { _________________ _________________ }

Abstract Base Classes :

A class which consists of pure virtual functions is called an Abstract Base class. In a pure virtual function, A function may be defined without any statement or the function declaration may be equated to zero if does not have a function definition part.

Virtual Destructors :

The destructor member function is invoked to free the memory storage by the C++ compiler automatically. But the destructor member function of the derived class is not invoked to free the memory storage which was allocated by the constructor member function of the derived class. It is because the destructor member functions are nonvirtual and the message will not reach the destructor member functions under Late binding (Dynamic binding). So it is better to have a destructor member function as virtual and the virtual destructors are essential in a program to free the memory space effectively under late binding method. Program 6 : Virtual Destructors #include<iostream.h> #include<conio.h> class base { public : base() { cout<<"Base class constructor"<<endl; } virtual ~base() {
B. Tech. - IVth Semester Teacher : Sandeep Sangwan (9417565720)

C++ Programming

Virtual Functions

10

cout<<"Base class destructor"<<endl; } }; class derived : public base { public : derived() { cout<<"Derived class constructor"<<endl; } ~derived() { cout<<"Derived class destructor"<<endl; } }; void main() { clrscr(); base *ptr; derived obj1; ptr=&obj1; delete ptr; getch(); } OUTPUT : Base class allocates 5 bytes Derived class allocated 100 bytes. Derived class free 100 bytes Base class frees 5 bytes In the above program, whenever instances are created at run time on the memory through the new operator, constructor member functions are called automatically. When the delete operator is used, the destructors are automatically called to release the space occupied by the instance itself. As a derived class instance always contains a base class instance, it is necessary to invoke destructors of both the classes in order to ensure that all the space on the memory is released.

B. Tech. - IVth Semester

Teacher : Sandeep Sangwan (9417565720)

You might also like