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

(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.

Short Answer

Expert verified
Create the 'Account' class with balance handling, extend for 'SavingsAccount' with interest, and 'CheckingAccount' with fees, then test functionalities.

Step by step solution

01

Define the Base Class 'Account'

Start by defining a class named 'Account'. This class should encapsulate a single member variable for the account balance of type double. You will define a constructor to initialize the balance, ensuring the initial balance is non-negative. If the balance is negative, initialize it to 0.0 and print an error message. Additionally, implement member functions 'credit' to add money, 'debit' to withdraw money, ensuring no overdraft, and 'getBalance' to return the current balance.
02

Define the Derived Class 'SavingsAccount'

Inherit 'SavingsAccount' from the base class 'Account'. It should add a new member variable for the interest rate. Its constructor should accept an initial balance and interest rate, initializing the base class's balance and its own interest rate. Implement the 'calculateInterest' function, which calculates interest by multiplying the balance by the interest rate and returns it as a double.
03

Define the Derived Class 'CheckingAccount'

Define 'CheckingAccount' inheriting from 'Account'. Add a member variable to represent the transaction fee. In its constructor, accept parameters for the initial balance and transaction fee, passing the balance to the base class constructor. Override the 'credit' and 'debit' functions to deduct the transaction fee from the balance after a successful transaction. Ensure that 'debit' invokes the base class method and only applies the fee if the withdrawal was successful, using the bool approach.
04

Create and Test Class Objects

Using the defined classes, create objects for 'Account', 'SavingsAccount', and 'CheckingAccount'. Test each class by applying credits, debits, checking balances, and for 'SavingsAccount', calculating and applying interest. Validate that 'CheckingAccount' properly applies the transaction fee where intended, ensuring the correct functionality of all class methods.

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 Hierarchy
Inheritance in Object-Oriented Programming allows new classes, called derived classes, to take on properties and behaviors of existing classes, known as base classes. In this exercise, we create an inheritance hierarchy starting with the base class `Account`. This base class contains common features for different types of accounts, such as balance handling.
Derived classes, `SavingsAccount` and `CheckingAccount`, inherit from `Account`. This means that they share the account balance management functionality, while also providing their unique behaviors.
  • Inheritance enables code reusability, reducing repetition.
  • It allows us to define common functionality in a single place, preventing errors.
  • Yet, derived classes can extend and modify behaviors based on their specific requirements.
Understanding inheritance is essential, as it models real-world relationships efficiently while keeping your code clean.
Class Design
Class design is the art of creating a blueprint for objects. In this exercise, you create three classes: `Account`, `SavingsAccount`, and `CheckingAccount`. This design differentiates between general and specific behaviors.
The base class `Account` is designed to handle common functionalities like balance operations. It includes methods for crediting and debiting amounts and a constructor for setting the initial balance.
  • This design keeps the core logic centralized, aiding maintainability and extensibility.
  • Derived classes add special functionalities without altering the base class directly.
  • Divide concerns by letting each class handle one aspect of the required functionality.
Good class design ensures that your code is efficient, easy to update, and aligned with real-world relationships.
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. In this exercise, we see polymorphic behavior when both `SavingsAccount` and `CheckingAccount` are treated as `Account` objects.
Polymorphism is powerful because it allows for flexible code that can interact with different object types through a consistent interface.
  • Even though `SavingsAccount` and `CheckingAccount` differ in functionality, they share the base interface inherited from `Account`.
  • This means you can invoke methods like `credit` and `debit` on them uniformly.
  • It also enables the use of dynamic binding, allowing derived classes to override base class methods and introduce specific functionality.
Through polymorphism, you can write more generic and reusable code pieces, reducing dependencies and increasing flexibility.
Member Functions
Member functions are methods that operate on the data contained within a class. For `Account`, `SavingsAccount`, and `CheckingAccount`, key member functions bring specific banking functionalities to life.
The base class `Account` provides essential member functions:
  • `credit(double amount)`: Adds a specified amount to the account balance.
  • `debit(double amount)`: Withdraws a specified amount if the balance allows; otherwise, prints a warning.
  • `getBalance()`: Returns the current account balance.
Each derived class can enhance or override these functions. For example:
  • `SavingsAccount` introduces `calculateInterest()`, which computes interest based on the current balance and interest rate.
  • `CheckingAccount` modifies `credit` and `debit` to include transaction fees.
The use of member functions allows for organized and encapsulated handling of account operations, making the system robust and clear in its functioning.

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

The world of shapes is much richer than the shapes included in the inheritance hierarchy of Fig, \(12.3 .\) Write down all the shapes you can think ofboth two- dimensional and three-dimensionaland form them into a more complete Shape hierarchy with as many levels as possible. Your hierarchy should have base class shape from which class Two imensionalshape and class ThreeDimensionalshape are derived. [Note: You do not need to write any code for this exercise.] We will use this hierarchy in the exercises of Chapter 13 to process a set of distinct shapes as objects of base-class shape. (This technique, called polymorphism, is the subject of Chapter \(13 .\).

Draw an inheritance hierarchy for classes ouadrilateral, TRapezoid, Parallelogram, Rectangle and Square. Use quadrilateral as the base class of the hierarchy. Make the hierarchy as deep as possible.

( 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.

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?

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.

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