Department of Computer Science Faculty of Mathematics and Computing Banasthali Vidyapith, Rajasthan Agenda • Virtual Function • Virtual Base Class • Pure Virtual Function • Abstract Class Virtual Function • A 'virtual' is a keyword preceding the normal declaration of a function. • A C++ virtual function is a member function in the base class that you redefine in a derived class. • There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the 'virtual' function. • When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer. • It is used to tell the compiler to perform dynamic linkage or late binding on the function. Rules for Virtual Function • Virtual functions must be members of some class. • Virtual functions cannot be static members. • They are accessed through object pointers. • They can be a friend of another class. • A virtual function must be defined in the base class, even though it is not used. • The prototypes of a virtual function of the base class and all the derived classes must be identical. • We cannot have a virtual constructor, but we can have a virtual destructor. Virtual Base Class • Virtual base classes in C++ are used to prevent multiple instances of a given class from appearing in an inheritance hierarchy when using multiple inheritances. • Base classes are the classes from which other classes are derived. The derived(child) classes have access to the variables and methods/functions of a base(parent) class. The entire structure is known as the inheritance hierarchy. • Virtual Class is defined by writing a keyword “virtual” in the derived classes, allowing only one copy of data to be copied to derived class. • It prevents multiple instances of a class appearing as a parent class in the inheritance hierarchy when multiple inheritances are used. Need for virtual base class • To prevent the error and let the compiler work efficiently, we’ve to use a virtual base class when multiple inheritances occur. It saves space and avoids ambiguity. • When a class is specified as a virtual base class, it prevents duplication of its data members. Only one copy of its data members is shared by all the base classes that use the virtual base class. • If a virtual base class is not used, all the derived classes will get duplicated data members. In this case, the compiler cannot decide which one to execute. Pure Virtual Function • A pure virtual function in C++ is a virtual function for which we do not have an implementation. • We do not write any functionality in it. Instead, we only declare this function. • A pure virtual function does not carry any definition related to its base class. • A pure virtual function is declared by assigning a zero (0) in its declaration. • Any class containing one or more pure virtual functions can not be used to define any object. For this reason, these classes are known as abstract classes. Classes derived from abstract classes need to Syntax
virtual <function_type> <function_name>() = 0;
Characteristics of Pure Virtual Function • A pure virtual function does not do anything, which means it is used to resemble the template, and the derived classes implement the function. • It is an empty function because it does not contain any definition of the functionality of its base class in it. • Derived class can call a member or pure virtual function in C++. • The user in the derived class redefines a pure virtual function. • Any class in C++ that contains a pure virtual function does not allow the user to create the object of that class.