Tell me can a pure virtual function have an implementation?
Submitted by: MuhammadThe quick answer to that question is yes! A pure virtual function can have an implementation in C++ - which is something that even many veteran C++ developers do not know. So, using the SomeClass class from our example above, we can have the following code:
class SomeClass {
public:
virtual void pure_virtual() = 0; // a pure virtual function
// note that there is no function body
};
/*This is an implementation of the pure_virtual function
which is declared as a pure virtual function.
This is perfectly legal:
*/
void SomeClass::pure_virtual() {
cout <<"This is a test"<< endl;
}< /endl;
Submitted by: Muhammad
class SomeClass {
public:
virtual void pure_virtual() = 0; // a pure virtual function
// note that there is no function body
};
/*This is an implementation of the pure_virtual function
which is declared as a pure virtual function.
This is perfectly legal:
*/
void SomeClass::pure_virtual() {
cout <<"This is a test"<< endl;
}< /endl;
Submitted by: Muhammad
Read Online C++ Virtual Functions Job Interview Questions And Answers
Top C++ Virtual Functions Questions
☺ | Can you please explain the difference between using macro and inline functions? |
☺ | Explain polymorphism? |
☺ | Explain object slicing in C++? |
☺ | Explain data encapsulation? |
☺ | What is Object slicing? |
Top C Plus Plus Language Categories
☺ | C++ Pointers & Functions Interview Questions. |
☺ | C++ Operator Overloading Interview Questions. |
☺ | C++ Exception Handling Interview Questions. |
☺ | C++ Template Interview Questions. |
☺ | C++ Virtual Functions Interview Questions. |