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
Give example of a pure virtual function in c++?
What is c++ runtime?
What are advantages of using friend classes?
Can there be at least some solution to determine the number of arguments passed to a variable argument list function?
What is the last index number in an array of 100 characters a) 100 b) 99 c) 101
What is the need of a destructor? Explain with the help of an example.
What is array in c++ pdf?
Explain function overloading
What is a tree in c++?
Which programming language's unsatisfactory performance led to the discovery of c++?
What does n mean in c++?
How can you quickly find the number of elements stored in a static array? Why is it difficult to store linked list in an array?
How a modifier is similar to mutator?
Can c++ do everything c can?
What is the use of cmath in c++?