|
|
|
C++ |
|
Home >
Interview Questions >
C++
|
|
C++
Interview Questions Page 4 |
|
<<Previous Next>> |
What is virtual function?
When derived class overrides the base class method by
redefining the same function, then if client wants to access
redefined the method from derived class through a pointer from
base class object, then you must define this function in base
class as virtual function.
class parent
{
void Show()
{
cout << "i'm parent" << endl;
}
};
class child: public parent
{
void Show()
{
cout << "i'm child" << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls parent->show() i
now we goto virtual world...
class parent
{
virtual void Show()
{
cout << "i'm parent" << endl;
}
};
class child: public parent
{
void Show()
{
cout << "i'm child" << endl;
}
};
parent * parent_object_ptr = new child;
parent_object_ptr->show() // calls child->show()
What is pure virtual function? or what is abstract class?
When you define only function prototype in a base class
without and do the complete implementation in derived class.
This base class is called abstract class and client won't able
to instantiate an object using this base class.
You can make a pure virtual function or abstract class this
way..
class Boo
{
void foo() = 0;
}
Boo MyBoo; // compilation error |
|
<<Previous Next>> |
|
Confined Topics
to C++ |
|
|
|
Related Topics to
C++ |
|
|
|
Other Sites for C++ |
|
Submit your link
|
|
Send more about C++ |
|
Send us your comments on C++,
if you want to share your knowledge on C++. We will
publish it on this site |
|