Virtual Functions & Abstract Classes in C++ | C++ Programming Tutorials

Simple Snippets
Youtube
Related Topic
:- programming skills C++ language

Support Simple Snippets by Donations - Google Pay UPI ID - tanmaysakpal11@okicici PayPal - paypal.me/tanmaysakpal11 --------------------------------------------------------------------------------------------- In this video tutorial we will study and understand the concept of Virtual Functions and Abstract Classes and we will also see 2 practical examples of Virtual Functions. Virtual Function in C++ - A virtual function is a member function which is declared within base class and is re-defined (Overridden) by derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. 1) Virtual functions ensure that the correct function is called for an 2) object, regardless of the type of reference (or pointer) used for function call. 3) They are mainly used to achieve Runtime polymorphism 4) Functions are declared with a virtual keyword in base class. 5) The resolving of function call is done at Run-time. Pure Virtual Functions & Abstract Class in C++ - Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration.  Some important facts – 1) A class is abstract if it has at least one pure virtual function. 2) We can have pointers and references of abstract class type. 3) If we do not override the pure virtual function in derived class, then derived class also becomes abstract class. 4) Abstract classes cannot be instantiated. 

Comments