WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Basic · question 15 of 100

What is a virtual function, and why would you use one in C++?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

In C++, a virtual function is a member function of a class that can be overridden in a derived class. When a virtual function is called through a pointer or reference to a base class object, the actual function that is called depends on the type of the object that the pointer or reference points to. This is known as dynamic binding or late binding.

To declare a virtual function in C++, you use the ’virtual’ keyword before the function declaration in the base class. For example:

    class MyBaseClass {
        public:
        virtual void myVirtualFunction() {
            // function code
        }
    };
    
    class MyDerivedClass : public MyBaseClass {
        public:
        void myVirtualFunction() override {
            // overridden function code
        }
    };

In this example, we have a base class called MyBaseClass with a virtual function called myVirtualFunction(). We also have a derived class called MyDerivedClass that overrides the myVirtualFunction() function. When we call the myVirtualFunction() function through a pointer or reference to a MyBaseClass object, the actual function that is called depends on the type of the object that the pointer or reference points to.

Virtual functions are important in C++ because they allow you to achieve polymorphism, which is the ability to use objects of different classes in a uniform way. Polymorphism is a fundamental concept in object-oriented programming and is useful in many situations, such as when you want to write code that can handle objects of different classes in a generic way.

For example, consider the following code:

    void doSomething(MyBaseClass* obj) {
        obj->myVirtualFunction();
    }
    
    int main() {
        MyBaseClass* obj1 = new MyBaseClass();
        MyDerivedClass* obj2 = new MyDerivedClass();
        doSomething(obj1); // calls MyBaseClass::myVirtualFunction()
        doSomething(obj2); // calls MyDerivedClass::myVirtualFunction()
        return 0;
    }

In this example, we have a function called doSomething() that takes a pointer to a MyBaseClass object and calls its myVirtualFunction() function. We also have two objects, obj1 and obj2, of different classes that inherit from MyBaseClass. When we call the doSomething() function with these objects, the actual function that is called depends on the type of the object. This allows us to use objects of different classes in a uniform way and achieve polymorphism.

In summary, a virtual function in C++ is a member function of a class that can be overridden in a derived class. Virtual functions are important because they allow you to achieve polymorphism, which is the ability to use objects of different classes in a uniform way. Virtual functions are useful in many situations, such as when you want to write code that can handle objects of different classes in a generic way.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics