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 Class) Create a class called account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75)called floating- point valuesto represent dollar amounts.] Your 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 \(0 .\) If not, the balance should be set to 0 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 should 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 a message indicating "Debit amount exceeded account balance." Member function getBalance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.

Short Answer

Expert verified
Define the `Account` class with a constructor and methods as specified, handle invalid checks, then test using two accounts.

Step by step solution

01

Define the Account Class

We begin by defining the `Account` class. It will have an integer data member to represent the balance of the account. The class will have a constructor that initializes this balance and member functions for the operations specified.
02

Implement the Constructor

The constructor for the `Account` class should initialize the balance using a given argument. It should check that the initial balance is non-negative. If it is negative, initialize the balance to 0 and print an error message.
03

Implement the Credit Function

Create a `credit` function that takes an integer parameter and adds this amount to the account balance. This function does not need to perform validation beyond adding the credit amount.
04

Implement the Debit Function

Implement the `debit` function which tries to subtract a given amount from the balance. If the debit amount exceeds the current balance, print an error message and leave the balance unchanged.
05

Implement the Get Balance Function

Define the `getBalance` function that simply returns the current balance of the account. No additional logic is necessary for this function.
06

Test the Account Class

Create a program to test the methods of the `Account` class. Instantiate two `Account` objects with different initial balances. Test crediting and debiting each account and confirm the functions work as expected. Print the balance after each operation.

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.

Account class
In object-oriented programming, an `Account` class is a blueprint that models a bank account system. It mainly encapsulates the concept of a customer’s bank account through data members and functions that manipulate the account’s state.
This class includes a data member, typically of type `int`, representing the account balance. By encapsulating this balance, the class can expose methods to interact with it, while controlling how it is changed.
An account class should ideally maintain the integrity of its data. For example, by ensuring a balance cannot be negative or directly manipulated outside of controlled methods. In C++, an `Account` class usually provides member functions that allow adding, withdrawing, and querying the balance. These functions ensure that any interaction with the balance adheres to defined rules, such as limits on withdrawals or setting up initial conditions.
Constructor function
A constructor is a special member function called automatically when an object of a class is created. The primary role of a constructor is to initialize the object’s data members. In the `Account` class, the constructor function serves to set up the account with an initial balance.
The constructor does more than just assign values; it also contains logic to ensure the values are valid. This class specifically initializes the balance only if the provided amount is greater than or equal to zero. Otherwise, it defaults the balance to zero and provides a clear error message indicating the initial balance was invalid.
Constructors in C++ are powerful as they help enforce constraints right when an object is created, ensuring the object starts its lifecycle in a valid state.
Data validation
Data validation is a crucial aspect of programming that involves checking and ensuring that the data being processed meets certain criteria. In the context of the `Account` class, data validation ensures that the account starts with a non-negative balance.
When a constructor receives an invalid initial balance, it doesn't just fail silently. Instead, it rejects the invalid data by setting the balance to zero and informs the user with an error message. This form of validation prevents inconsistent states in the system.
Validation is also applied in other methods like `debit`, which ensures that money cannot be withdrawn unless the account balance is sufficient. Such checks help maintain data integrity and prevent operations that would lead to errors or exceptions.
Member functions
Member functions are integral components of a class, providing the means to interact with an object's data members. The `Account` class employs several key member functions for interaction.
  • `credit`: This function adds a specified amount to the account balance, allowing deposits. It assumes the amount is valid since it doesn't reduce the balance.
  • `debit`: This function attempts to subtract an amount from the balance for withdrawals. It includes logic to ensure the balance doesn’t fall below zero, protecting the account from overdrawing. If the intended debit amount exceeds the balance, it prevents the operation and notifies the user with an error message.
  • `getBalance`: A straightforward method that returns the current balance. It serves as a safe way to check the balance without altering it.
Member functions not only perform operations but help encapsulate the logic necessary to maintain consistent and valid states within an object, thereby following good programming practices.

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

(Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data membersa part number (type string \(),\) a part description (type string), a quantity of the item being purchased (type int) and a price per item (type int). [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75 )called floating- point valuesto represent dollar amounts.] Your class should have a constructor that initializes the four data members. Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int value. If the quantity is not positive, it should be set to \(\theta\). If the price per item is not positive, it should be set to \(\theta .\) Write a test program that demonstrates class Invoice's capabilities.

(Employee Class) Create a class called Employee that includes three pieces of information as data membersa first name (type string), a last name (type string and a monthly salary (type int). [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75 )called floating-point valuesto represent dollar amounts.] Your class should have a constructor that initializes the three data members. Provide a set and a get function for each data member. If the monthly salary is not positive, set it to \(\theta\). Write a test program that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10 percent raise and display each Employee's yearly salary again.

What is a default constructor? How are an object's data members initialized if a class has only an implicitly defined default constructor?

Explain why a class might provide a set function and a get function for a data member (Modifying Class GradeBook) Modify class GradeBook (Figs. 3.113 .12 ) as follows: a. Include a second string data member that represents the course instructor's name. b. Provide a set function to change the instructor's name and a get function to retrieve it. c. Modify the constructor to specify two parametersone for the course name and one for the instructor's name. d. Modify member function displaymessage such that it first outputs the welcome message and course name, then outputs "This course is presented by: " followed by the instructor's name.

Explain the difference between a function prototype and a function definition.

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