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
Explain how a pointer to function can be declared in C++?
What is c++ 11 and c++ 14?
Define Virtual function in C++.
Explain deep copy?
Should you pass exceptions by value or by reference?
What is setw manipulator in c++?
If a base class declares a function to be virtual, and a derived class does not use the term virtual when overriding that class, is it still virtual when inherited by a third-generation class?
Can non-public members of another instance of the class be retrieved by the method of the same class?
Can a constructor return a value?
What are the rules about using an underscore in a c++ identifier?
Array base access faster or pointer base access is faster?
What are the uses of typedef in a program?
Explain unexpected() function?
Is python written in c or c++?
Explain function overloading