Arizona State University (ASU) CSE100 Principles of Programming with C++ Midterm 1 Practice Exam

Question: 1 / 400

In C++, what would the keyword 'virtual' signify when used in a class?

It allows a method to be overridden in derived classes

The keyword 'virtual' in C++ is primarily used in the context of inheritance and polymorphism. When a method in a class is marked as virtual, it enables dynamic dispatch of the method. This means that if a derived class overrides this method, the version of the method that is executed will depend on the type of the object being pointed to or referenced, rather than the type of the pointer/reference itself.

By declaring a method as virtual in a base class, you provide the opportunity for derived classes to provide their specific implementation of this method. This is crucial for achieving polymorphism, which allows for more flexible and reusable code, as different objects can respond to the same method call in distinct ways based on their actual class type.

The other options do not adequately capture the purpose of the 'virtual' keyword. Static methods do not relate to dynamic polymorphism, access restrictions are managed through access specifiers like public, private, and protected, and default functions are unrelated to the concept of method overriding in C++. Thus, the option that states it allows a method to be overridden in derived classes accurately reflects the role of the 'virtual' keyword in C++.

Get further explanation with Examzify DeepDiveBeta

It indicates a static method

It restricts method access

It marks a method as a default function

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy