Friday, July 9, 2021

virtual memory, virtual base class and virtual function

 Virtual Memory

Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of main memory.

This enables a computer to be able to compensate shortages of physical memory by transferring pages of data from random access memory to disk storage.

More at https://www.geeksforgeeks.org/virtual-memory-in-operating-system/

Virtual Base class

When a class is made a virtual base class, C++ takes care to see that only one copy of that class is inherited regardless of how many inheritance path exits between the virtual base class and derived class.

Virtual function

This is example of runtime polymorphism


(Function overloading & Operator overloading are example of compile time polymorphism)

In c++ we can point any derived object via its base pointer.
But that pointer can not be used directly to access all members of derived class. by default that base  pointer will points to base class members only.

when we have same function name used in base class & derived class then if we mark base class function as virtual function then c++ decides which function to call at runtime based on the type of object pointed by base pointer. 

Pure Virtual functions

Its normal that we mark one function as virtual in base class and redefine it in derived class.

Many times virtual function in base class are just place holder and do nothing.

virtual void display() = 0;

such function called pure virtual function.

Such base class can not declare their own object.

so these classes are also called abstract base class.

No comments:

Post a Comment