What is Pure Virtual Function? Why and when it is used ?

Answer Posted / talha bilal

Pure Virtual Function

class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
class Derived:public Base
{
public:
void show()
{
cout << "Implementation of Virtual Function in Derived class";
}
};

int main()
{
Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}

Virtual Function

class Base
{
public:
virtual void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
private:
void show()
{
cout << "Derived Class";
}
};

int main()
{
Base *b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Occurs
}

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can non-public members of another instance of the class be retrieved by the method of the same class?

608


Explain data encapsulation?

601


What are virtual functions in c++?

685


What is c++ in english?

575


What is difference between array and vector in c++?

555






What problems might the following macro bring to the application?

618


Which sort does c++ use?

576


What is isdigit c++?

569


an integer constant must have atleast one a) character b) digit c) decimal point

554


What is the main purpose of overloading operators?

586


What do you mean by const correctness?

627


How long it will take to learn c++?

612


How do you sort a sort function in c++ to sort in descending order?

554


What is atoi in c++?

564


What is the difference between the indirection operator and the address of oper-ator?

604