Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

State whether each of the following is true or false. If false, explain why. a. All virtual functions in an abstract base class must be declared as pure virtual functions. b. Referring to a derived-class object with a base-class handle is dangerous. c. A class is made abstract by declaring that class virtual. d. If a base class declares a pure virtual function, a derived class must implement that function to become a concrete class. e. Polymorphic programming can eliminate the need for switch logic.

Short Answer

Expert verified
a. False, b. False, c. False, d. True, e. True.

Step by step solution

01

Analyze Statement (a)

The statement claims that all virtual functions in an abstract base class must be declared as pure virtual functions. This is false. An abstract base class only needs at least one pure virtual function to be considered abstract. It may have other virtual functions that are not pure.
02

Analyze Statement (b)

This statement suggests that it is dangerous to refer to a derived-class object with a base-class pointer or reference. This is false. Using a base-class handle to access derived-class objects is a common practice and safe when implemented correctly, as it allows polymorphism; however, one must ensure that the correct types are being used to avoid runtime errors.
03

Analyze Statement (c)

The claim is that a class becomes abstract by declaring it virtual. This is false. A class is made abstract by having at least one pure virtual function, not by declaring it as virtual. The keyword 'virtual' is used for functions, not entire classes.
04

Analyze Statement (d)

This statement asserts that if a base class has a pure virtual function, a derived class must implement it to become a concrete class. This is true. A derived class must provide an implementation for all inherited pure virtual functions to become non-abstract (or concrete).
05

Analyze Statement (e)

The statement suggests that polymorphic programming can eliminate the need for switch logic. This is true. Polymorphism allows behavior to be selected at runtime, often replacing complex switch or if-else logic with more maintainable class hierarchies.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Virtual Functions
In C++, virtual functions allow a class to be capable of dynamic dispatch, meaning that the function that gets executed is determined at runtime. This is achieved using pointer or reference references to base class types.
Virtual functions become important when dealing with inheritance because they enable polymorphism. To declare a virtual function, use the `virtual` keyword in the base class function declaration.
A critical thing to remember is that once a function is declared virtual in a base class, it remains virtual in all derived classes.
  • Virtual functions allow you to call functions of derived classes through base class pointers.
  • The virtual function in the base class must be overridden by any derived class for it to work correctly.
This mechanism is what allows for dynamic typing in C++.
Polymorphism
Polymorphism in C++ is a fundamental concept that enables one interface to control access to a general class of actions. The most common form of polymorphism is via function overriding.
Through polymorphism, C++ provides the ability to redefine or "+overload" functions or operators within derived classes, enhancing code reusability and maintainability.
There are two types of polymorphism: compile-time and runtime.
  • Compile-time polymorphism: Achieved with function overloading or operator overloading, where the call to a function is resolved at the compile time.
  • Runtime polymorphism: Achieved via virtual functions, where the call to an overridden method is resolved at runtime.
Polymorphism allows objects to be treated as instances of their base class, which simplifies and centralizes the control logic.
Abstract Base Class
An abstract base class in C++ is one that cannot be instantiated and usually serves as a blueprint for other classes. It is achieved by having at least one pure virtual function.
A pure virtual function is a function without a body, declared by appending `=0` to the function declaration. This requirement indicates that derived classes must provide an implementation of this function.
Abstract base classes serve as a template for derived classes, guiding the structure without defining exact implementation details.
  • Ensures common interface while allowing specialized behaviors in derived classes.
  • Makes it possible to declare pointers and references to the base class, which actually refer to derived class objects.
Abstract base classes are essential in the design of complex class hierarchies, facilitating scalable and maintainable software.
Derived Class
A derived class in C++ is a class that inherits attributes and behaviors (methods) from another class, known as the base class. Derived classes can add new member variables and functions or override existing ones from the base class.
Inheritance in C++ allows the derived class to extend or modify the functionality from the base class, promoting code reuse and reducing redundancy.
  • Provides a specialized version of the base class.
  • Can implement abstract base class methods to become a concrete class.
  • Allows the use of polymorphism to switch between derived types at runtime.
Derived classes represent an "is-a" relationship in object-oriented programming, where the derived class is a type of the base class, inheriting its interface and behavior while adding its own specific features.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free