Monday, April 5, 2021

virtual function in c++ (OOP) (35)

   /*virtual function in c++

   *************************/

/*virtual function is a function in base class which is overrided in derived class.and which tell

the compiler to perform late binding on this function.virtual function is used  to make the member

function of the class virtual.virtual function is the type of run_time polymorphism.A virtual 

function is a member function of the base class that is decleared in the base class and re defined

in the derived class.another use of virtual keyword is that we can call private function of derieved

from the base class pointers with the help of virtual keyword.compiler check the access specifier

only at compile time.at run time compiler does not the the whether the specifier is public or 

private.*/

#include<iostream>

using namespace std;

class A

{

public:

    virtual void display()

{

cout<<"this is the class A"<<endl;

}

};

class B:public A

{

private:

void display()

{

cout<<"this is the class B"<<endl;

}

};

class C:public A

{

private:

void display()

{

cout<<"this is the class C"<<endl;

}

};


int main()

{

A *p;

B obj;

C objc;

p=&obj;

p->display();

p=&objc;

p->display();

}

No comments:

Post a Comment