What is a virtual function in C++?
A
A function that can be overloaded
B
A function with default arguments
C
A member function that can be overridden in derived classes
D
A function that cannot be inherited
Analysis & Theory
A virtual function allows derived classes to override it and supports runtime polymorphism.
Which keyword is used to declare a virtual function?
Analysis & Theory
The `virtual` keyword is used to declare a virtual function in a base class.
What happens if a virtual function is called through a base class pointer to a derived class object?
A
Base class version is always called
B
Derived class version is called if overridden
Analysis & Theory
If the function is virtual and overridden in the derived class, the derived version is called.
What is the purpose of a virtual function?
A
To achieve compile-time polymorphism
C
To allow runtime binding
D
To access private data members
Analysis & Theory
Virtual functions allow runtime polymorphism, also known as dynamic binding.
What is a pure virtual function?
A
A function with no parameters
B
A function that must be overridden in derived classes
C
A function declared outside the class
D
A function that cannot be accessed
Analysis & Theory
A pure virtual function has no definition in the base class and must be overridden in derived classes.
How do you declare a pure virtual function?
B
virtual void show() = 0;
C
pure virtual void show();
Analysis & Theory
A pure virtual function is declared with `= 0` in its declaration.
What is an abstract class in C++?
A
A class with only private members
B
A class with at least one pure virtual function
C
A class with no constructors
D
A class that cannot inherit
Analysis & Theory
A class with at least one pure virtual function is called an abstract class.
Can an abstract class be instantiated?
B
Only if it has a constructor
Analysis & Theory
Abstract classes cannot be instantiated directly because they contain incomplete definitions.
Which of the following is true about virtual destructors?
A
They are required for function overloading
C
They ensure proper destruction of derived objects
D
They prevent object creation
Analysis & Theory
Virtual destructors ensure that derived class destructors are called when deleting via base class pointer.
What is the default behavior if a virtual function is not overridden in the derived class?
B
Derived class version is called
C
Base class version is used
Analysis & Theory
If not overridden, the base class version of the virtual function is called.