24 Nov 2019

  • November 24, 2019
  • Amitraj
C++ Virtual Function:-

-> When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword virtual precending its normal declaration. when a function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer.

-> Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual function. Below program illustrates this point.


Rules of Virtual Function

1.Virtual functions must be members of some class.
2.Virtual functions cannot be static members.
3.They are accessed through object pointers.
4.They can be a friend of another class.
5.A virtual function must be defined in the base class, even though it is not used.
6.The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
7.We cannot have a virtual constructor, but we can have a virtual destructor.


// Cpp program to demonstrate virtual function.

#include <iostream>
using namespace std;    
class A
{    
 public:    
        virtual void display()    
 {    
  cout << "Base class is invoked"<<endl;    
 }    
};    
class B:public A    
{    
 public:    
          void display()    
 {    
  cout << "Derived Class is invoked"<<endl;    
 }    
};    
int main()    
{    
 A* a;    //pointer of base class    
 B b;     //object of derived class    
 a = &b;    
 a->display();   //Late Binding occurs    

}    

OUTPUT:

Derived Class is invoked

Related Posts:

  • Difference between procedure Oriented Programming and Object oriented Programming language Difference between procedure Oriented Programming and Object oriented Programming language 1.)  procedure Oriented Programming Or Structured Oriented (POP):- -> Importance is given to logic.(Algorithm) -> … Read More
  • History of C++ History of C++ -> The C++ language is an object-oriented programming language & is a combination of both low-level & high-level language – a Middle-Level Language. -> C++ was developed by a Danish Computer Sc… Read More
  • Constructors in C++ Constructors in C++ A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the… Read More
  • Operators in C and C++ Operators in C and C++ 1.) Arithmetic Operator:-              The type of operators are use to perform general mathematical functions such as Add, Sub, Multiply or Divide. -> The… Read More
  • Friend Class and Friend Function in C++ Friend - Class:-  A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.… Read More

Translate

Popular Posts