Create An Inheritance Hierarchy For Bank Migration In C Prog

C Programmingcreate An Inheritance Hierarchy That A Bank Might Use

Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (credit) money into their accounts and withdraw (debit) money from their accounts. More specific types of accounts also exist. Savings accounts earn interest on the money they hold, while checking accounts charge a fee per transaction.

The base class Account should include:

  • One data member of type double to represent the account balance.
  • A constructor that receives an initial balance and initializes the data member, validating that the initial balance is greater than or equal to 0. If not, set the balance to 0.0 and display an error message.
  • Member functions Credit to add an amount to the balance.
  • Member functions Debit to withdraw money, ensuring the withdrawal does not exceed the current balance. If it does, leave the balance unchanged and display "Debit amount exceeded account balance."
  • Member function getBalance to return the current balance.

The SavingsAccount class should inherit from Account and include:

  • A data member of type double indicating the interest rate (percentage).
  • A constructor that receives initial balance and initial interest rate.
  • A member function calculateInterest that returns the amount of interest earned, calculated as the interest rate multiplied by the account balance.

The CheckingAccount class should inherit from Account and include:

  • A data member of type double representing the fee charged per transaction.
  • A constructor that receives initial balance and fee amount.
  • Overrides of Credit and Debit that subtract the fee from the balance upon successful transaction. The Debit function should only charge a fee if the debit is successful (i.e., the withdrawal does not exceed the balance). The Debit function of the base class should return a boolean indicating success, which is then used to determine whether to deduct the fee.

After defining these classes, the program should create objects of each class, test their functions, and, for the savings account, add interest by invoking calculateInterest and passing the amount to Credit.

Paper For Above instruction

The design and implementation of an inheritance hierarchy for banking accounts in C++ provides a structured way to model different account types with shared and specific functionalities. This approach exemplifies the principles of object-oriented programming, such as encapsulation, inheritance, and polymorphism, facilitating code reusability, maintainability, and clarity.

The base class Account acts as the fundamental structure, representing common features such as balance management, deposits, and withdrawals. The constructor performs validation to prevent negative initial balances—a vital feature for ensuring data integrity. Member functions Credit and Debit allow for modifying the account balance, with Debit safeguarding against overdraft attempts. The getBalance function provides access to the current balance, encapsulating data access.

Derived classes extend this foundational class, adding specific features pertinent to different account types. The SavingsAccount class introduces an interest rate and the ability to calculate interest, aligning with real-world savings account features. The calculateInterest method computes the interest based on the current balance and interest rate, which can then be credited to the account, thus simulating how savings accounts accrue earnings over time.

The CheckingAccount class models transactional accounts that charge fees per transaction. By overriding the Credit and Debit methods, this class incorporates fee deduction only upon successful transactions, demonstrated by the base class's Debit method returning a success indicator. This implementation ensures that fees are only applied when actual money movements occur, reflecting real-world banking fees' conditional nature.

Implementing this hierarchy in C++ involves defining classes with appropriate access specifiers, constructors, and member functions, ensuring proper inheritance and method overriding. This structure allows creating various account objects, performing transactions, and calculating interest effectively, providing a robust simulation of banking operations.

The practical application of this hierarchy includes creating and testing these classes in a main program, demonstrating deposits, withdrawals, interest calculations, and fee deductions, thereby illustrating how different account types operate and interact within a banking system.

Implementation Code

include <iostream>

include <iomanip>

using namespace std;

// Base class Account

class Account {

protected:

double balance;

public:

Account(double initialBalance) {

if (initialBalance >= 0.0) {

balance = initialBalance;

} else {

balance = 0.0;

cout << "Error: Initial balance is invalid. Setting balance to 0." << endl;

}

}

virtual ~Account() {}

void Credit(double amount) {

if (amount >= 0) {

balance += amount;

} else {

cout << "Error: Credit amount cannot be negative." << endl;

}

}

virtual bool Debit(double amount) {

if (amount

cout << "Error: Debit amount cannot be negative." << endl;

return false;

}

if (amount > balance) {

cout << "Debit amount exceeded account balance." << endl;

return false;

} else {

balance -= amount;

return true;

}

}

double getBalance() const {

return balance;

}

};

// Derived class SavingsAccount

class SavingsAccount : public Account {

private:

double interestRate; // expressed as percentage, e.g., 5 for 5%

public:

SavingsAccount(double initialBalance, double rate)

: Account(initialBalance) {

if (rate >= 0) {

interestRate = rate;

} else {

interestRate = 0;

}

}

double calculateInterest() const {

return (interestRate / 100.0) * getBalance();

}

};

// Derived class CheckingAccount

class CheckingAccount : public Account {

private:

double transactionFee;

public:

CheckingAccount(double initialBalance, double fee)

: Account(initialBalance), transactionFee(fee) {}

void Credit(double amount) override {

if (amount >= 0) {

Account::Credit(amount);

balance -= transactionFee;

} else {

cout << "Error: Credit amount cannot be negative." << endl;

}

}

bool Debit(double amount) override {

if (amount + transactionFee

cout << "Error: Debit amount cannot be negative." << endl;

return false;

}

if (amount > getBalance()) {

cout << "Debit amount exceeded account balance." << endl;

return false;

} else {

bool success = Account::Debit(amount);

if (success) {

balance -= transactionFee;

}

return success;

}

}

};

int main() {

// Create a general account with initial balance

Account generalAccount(500);

cout << fixed << setprecision(2);

cout << "General Account initial balance: $" << generalAccount.getBalance() << endl;

generalAccount.Credit(150.75);

cout << "After crediting $150.75, balance: $" << generalAccount.getBalance() << endl;

generalAccount.Debit(200);

cout << "After debiting $200, balance: $" << generalAccount.getBalance() << endl;

generalAccount.Debit(300); // Should trigger insufficient funds

cout << endl;

// Create a savings account

SavingsAccount savings(1000, 5);

cout << "Savings Account initial balance: $" << savings.getBalance() << endl;

double interest = savings.calculateInterest();

cout << "Interest earned: $" << interest << endl;

savings.Credit(interest);

cout << "Balance after adding interest: $" << savings.getBalance() << endl;

savings.Debit(50);

cout << "Balance after withdrawing $50: $" << savings.getBalance() << endl;

cout << endl;

// Create a checking account

CheckingAccount checking(200, 2);

cout << "Checking Account initial balance: $" << checking.getBalance() << endl;

checking.Credit(100);

cout << "After crediting $100, balance: $" << checking.getBalance() << endl;

checking.Debit(50);

cout << "After debiting $50, balance: $" << checking.getBalance() << endl;

// Try a withdrawal exceeding balance

checking.Debit(300);

cout << "Final balance: $" << checking.getBalance() << endl;

return 0;

}

References

  • Stroustrup, B. (2013). The C++ Programming Language (4th ed.). Addison-Wesley.
  • Lippman, S. B., Lajoie, J., & Moo, B. E. (2012). C++ Primer (5th ed.). Addison-Wesley.
  • Meyers, S. (2005). Effective C++: 55 Specific Ways to Improve Your Programs and Designs. Addison-Wesley.
  • ISO/IEC. (2017). ISO/IEC 14882:2017(E) — Programming language C++.
  • Stroustrup, B. (2020). The Evolution of C++: 40 Years in the Making. Communications of the ACM, 63(7), 14-16.
  • Deitel, P. J., & Deitel, H. M. (2014). C++ How to Program. Pearson.
  • Sutter, H., & Alexandrescu, A. (2004). C++ Coding Standards: 101 Rules, Guidelines, and Best Practices. Addison-Wesley.
  • Gourlay, T. & et al. (2019). Applying OOP Principles in Real-world Banking Systems. International Journal of Software Engineering & Applications, 13(4), 45-58.
  • Heess, G. (2018). Object-Oriented Design Patterns in C++ for Banking Applications. Journal of Software Engineering and Applications, 11(7), 355-370.