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

Fill in the blanks in each of the following statements: a. _______ is a form of software reuse in which new classes absorb the data and behaviors of existing classes and embellish these classes with new capabilities. b. A base class's _______ members can be accessed only in the base- class definition or in derived-class definitions. c. \(\ln a(n)\) _______ relationship, an object of a derived class also can be treated as an object of its base class. d. \(\ln a(n)\) _______relationship, a class object has one or more objects of other classes as members. e. In single inheritance, a class exists in a(n) _______ relationship with its derived classes. f. A base class's _______ members are accessible within that base class and anywhere that the program has a handle to an object of that base class or to an object of one of its derived classes. g. \(A\) base class's protected access members have a level of protection between those of public and _______ access. h. \(C++\) provides for _______ which allows a derived class to inherit from many base classes, even if these base classes are unrelated. i. When an object of a derived class is instantiated, the base class's _______ is called implicitly or explicitly to do any necessary initialization of the base-class data members in the derived-class object. J. When deriving a class from a base class with public inheritance, public members of the base class become _______ members of the derived class, and protected members of the base class become _______ members of the derived class. k. When deriving a class from a base class with protected inheritance, public members of the base class become _______ members of the derived class, and protected members of the base class become _______ members of the derived class.

Short Answer

Expert verified
a. inheritance, b. protected, c. is-a, d. has-a, e. is-a, f. public, g. private, h. multiple inheritance, i. constructor, j. public, protected, k. protected, protected.

Step by step solution

01

Understanding the Context for Software Reuse

For the first blank, we need to identify the concept where new classes extend or embellish existing classes. In object-oriented programming, this is known as 'inheritance', whereby new classes (derived classes) absorb data and behaviors from existing classes (base classes), adding new functionalities.
02

Access Level of Base Class Members

The second blank concerns members of a base class that have restricted accessibility. These members are known as 'protected' members, which can be accessed within the base class and its derived classes.
03

Derived-Class Object Treated as Base-Class

For the third blank, we describe a relationship where a derived class object is recognized as an instance of its base class. This scenario fits the 'is-a' relationship commonly associated with inheritance hierarchies.
04

Components in Class Composition

The fourth blank describes a relationship wherein a class contains other class objects as members, known as an 'has-a' relationship, often referring to 'composition'.
05

Relationship in Single Inheritance

In the fifth blank, single inheritance creates an 'is-a' relationship between classes and their derived classes, meaning derived classes inherit properties from a single base class.
06

Accessibility with Base Class Objects

The sixth blank refers to 'public' members of the base class, which are accessible both within the base class and wherever an object of the base or derived class is accessible in the program.
07

Intermediate Level of Protection

In the seventh blank, the protection level between public and private is 'protected', providing a moderate level of access control to class members.
08

Multiple Inheritance Mechanism

The eighth blank refers to 'multiple inheritance', which lets a derived class inherit from more than one base class.
09

Base Class Initialization When Derived Object Created

For the ninth blank, 'constructor' is the method called implicitly or explicitly to initialize base class data within a newly created derived object.
10

Public Inheritance and Member Visibility

In the tenth blank "J", public members of a base class remain 'public' in derived classes, and protected members remain 'protected'.
11

Protected Inheritance and Member Visibility

In the eleventh blank "K", under protected inheritance, both public and protected members of a base class become 'protected' members of the derived class.

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.

Inheritance
Inheritance is one of the core principles of object-oriented programming that allows new classes, called derived classes, to absorb the attributes and behaviors of existing classes, known as base classes. This process is often summarized with the term "is-a" relationship, where the derived class "is a" specialized version of the base class.

This enables code reuse, reducing redundancy in your programs. Imagine having a generic class `Animal`, and from this, you can inherit to create more specific classes like `Dog` or `Cat`. These new classes automatically possess the fields and methods of `Animal`, and you can also extend them with more specific features or override existing ones.
  • **Single Inheritance**: A derived class inherits from one base class. This straightforward structure maintains a clean hierarchy.
  • **Multiple Inheritance**: A derived class inherits from multiple base classes. This allows for a greater combination of features but can also lead to complexity.
Access Specifiers
Access specifiers in object-oriented programming control the visibility of class members, determining where they can be accessed from.

There are three main levels of access specifiers, each providing progressively stricter access control:
  • **Public**: Members declared as public can be accessed anywhere in the program. This is useful for functions or variables that need to be widely accessible.
  • **Protected**: Protected members can be accessed within their own class and by derived classes. This sets an intermediate level of visibility, ensuring internal class structures are guarded yet extendable.
  • **Private**: These members are accessible only within the class they are declared. They ensure that sensitive class data or functions are hidden from outside manipulation.
Using these specifiers strategically, developers can ensure that class internals remain safe from inadvertent misuse, and at the same time, expose the necessary parts for interaction.
Class Relationships
Class relationships describe how classes in object-oriented programming interact and relate to one another. The primary types of relationships help define how objects communicate within the system, ensuring cohesion and decoupling where necessary.

  • **Inheritance (Is-a Relationship)**: Explained previously, this relationship allows a derived class to inherit the features of a base class, enabling polymorphism where a derived class object can be used in place of a base class object.
  • **Composition (Has-a Relationship)**: A class can contain objects of other classes, indicating that it "has" certain features. For example, a `Car` class can "have" a `Engine` class as a part of its components. This emphasizes a part-whole relationship, where parts don't exist independently of the whole.
Understanding and properly implementing these relationships help create a well-structured and manageable codebase. They allow developers to build modular and scalable software systems that can evolve over time.

One App. One Place for Learning.

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

Get started for free

Most popular questions from this chapter

State whether each of the following is true or false. If \(f a / s e,\) explain why. a. Base-class constructors are not inherited by derived classes. b. A has-a relationship is implemented via inheritance. c. \(A\) car class has an \(i s\) -a relationship with the steeringwheel and Brakes classes. d. Inheritance encourages the reuse of proven high-quality software. e. When a derived-class object is destroyed, the destructors are called in the reverse order of the constructors.

Discuss the ways in which inheritance promotes software reuse, saves time during program development and helps prevent errors.

(Account Inheritance Hierarchy) Create an inheritance hierarchy that a bank might use to represent customers' bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e.. credit or debit). Create an inheritance hierarchy containing base class account and derived classes savingsAccount and checkingAccount that inherit from class Account. Base class Account should include one data member of type double to represent the account balance. The class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to \(\theta . \theta .\) If not, the balance should be set to \(\theta . \theta\) and the constructor should display an error message, indicating that the initial balance was invalid. The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the account and ensure that the debit amount does not exceed the account's balance. If it does, the balance should be left unchanged and the function should print the message "Debit amount exceeded account balance." Member function getBalance should return the current balance. Derived class savingsAccount should inherit the functionality of an Account, but also include a data member of type double indicating the interest rate (percentage) assigned to the Account. SavingsAccount's constructor should receive the initial balance, as well as an initial value for the SavingsAccount's interest rate. SavingsAccount should provide a public member function calculateInterest that returns a double indicating the amount of interest earned by an account. Member function calculateInterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit member functions credit and debit as is without redefining them.] Derived class checkingAccount should inherit from base class Account and include an additional data member of type double that represents the fee charged per transaction. CheckingAccount's constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class CheckingAccount should redefine member functions credit and debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount's versions of these functions should invoke the base-class account version to perform the updates to an account balance. CheckingAccount's debit function should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance). [Hint: Define Account's debit function so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether a fee should be charged.] After defining the classes in this hierarchy, write a program that creates objects of each class and tests their member functions. Add interest to the SavingsAccount object by first invoking its calculateInterest function, then passing the returned interest amount to the object's credit function.

Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee of the CommissionEmployeeBasePlusCommissionEmployee hierarchy to use composition rather than inheritance. After you do this, assess the relative merits of the two approaches for designing classes commissionEmployee and BasePlusCommissionEmployee, as well as for object-oriented programs in general. Which approach is more natural? Why?

( Package Inheritance Hierarchy) Package-delivery services, such as \(\mathrm{FedEx}^{\mathbb{Q}}\), \(\mathrm{DHL}^{@}\) and \(\mathrm{UPS}^{@}\), offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use Package as the base class of the hierarchy, then include classes TwoDayPackage and overnight Package that derive from Package. Base class Package should include data members representing the name, address, city, state and ZIP code for both the sender and the recipient of the package, in addition to data members that store the weight (in ounces) and cost per ounce to ship the package. Package's constructor should initialize these data members. Ensure that the weight and cost per ounce contain positive values. Package should provide a public member function calculatecost that returns a double indicating the cost associated with shipping the package. Package's calculatecost function should determine the cost by multiplying the weight by the cost per ounce. Derived class Two DayPackage should inherit the functionality of base class Package, but also include a data member that represents a flat fee that the shipping company charges for two-day-delivery service. TwoDayPackage's constructor should receive a value to initialize this data member. Two ouppackage should redefine member function calculatecost so that it computes the shipping cost by adding the flat fee to the weight-based cost calculated by base class Package's calculatecost function. Class overnightPackage should inherit directly from class Package and contain an additional data member representing an additional fee per ounce charged for overnight-delivery service. overnightPackage should redefine member function calculatecost so that it adds the additional fee per ounce to the standard cost per ounce before calculating the shipping cost. Write a test program that creates objects of each type of Package and tests member function calculatecost.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free