Question:
You are a software engineer, and you are assigned with a task to create a simplified banking system that handles different types of bank accounts. Your system should allow for the management of different accounts such as Savings Accounts, and Credit Card Accounts. You will be using the concepts of inheritance, constructors, and function overriding to achieve this.
Requirements:
- Create a base class BankAccount with the following attributes:
• accountNumber (integer)
• accountHolderName (string)
• balance (double)
• Create a virtual function displayAccountInfo() in the base class that displays the account information. - Implement the following constructors in the BankAccount class:
• An explicit default constructor that initializes accountNumber to 0, accountHolderName to an empty string, and balance to 0.
• A Parameterized constructor that takes accountNumber and accountHolderName as parameters and initializes balance to 0. - Create two derived classes from BankAccount:
• SavingsAccount
• CreditCardAccount - SavingsAccount derived class should:
• Has its own unique attributes (interest rate for savings) and appropriate accessor methods.
• Implement constructors:
• An explicit default constructor that initializes unique attributes appropriately.
• A Parameterized constructor that takes accountNumber, accountHolderName, and unique attributes as parameters.
• Implement function overriding: Override displayAccountInfo() function in each derived class to display account-specific information. - CreditCardAccount derived class should:
• Has its own unique attributes (credit limit for credit card) and appropriate accessor methods.
• Implement constructors:
• An explicit default constructor that initializes unique attributes appropriately.
• A Parameterized constructor that takes accountNumber, accountHolderName, and unique attributes as parameters.
• Implement function overriding: Override displayAccountInfo() function in each derived class to display account-specific information. - Implement function overriding:
• Create a virtual function displayAccountInfo() in the base class that displays the account information.
• Override this function in each derived class to display account-specific information.
Solution:
#include <iostream>
#include <string>
// https://universitydistancelearning.com/
class BankAccount {
protected:
int accountNumber;
std::string accountHolderName;
double balance;
public:
// Default constructor
BankAccount() : accountNumber(0), accountHolderName(""), balance(0.0) {}
// Parameterized constructor
BankAccount(int accNumber, const std::string& accHolderName)
: accountNumber(accNumber), accountHolderName(accHolderName), balance(0.0) {}
// Virtual function to display account information
virtual void displayAccountInfo() {
std::cout << "Account Number: " << accountNumber << std::endl;
std::cout << "Account Holder Name: " << accountHolderName << std::endl;
std::cout << "Balance: $" << balance << std::endl;
}
};
class SavingsAccount : public BankAccount {
private:
double interestRate;
public:
// Default constructor
SavingsAccount() : interestRate(0.0) {}
// Parameterized constructor
SavingsAccount(int accNumber, const std::string& accHolderName, double interest)
: BankAccount(accNumber, accHolderName), interestRate(interest) {}
// Accessor method for interest rate
double getInterestRate() const {
return interestRate;
}
// Override displayAccountInfo() for SavingsAccount
void displayAccountInfo() override {
BankAccount::displayAccountInfo();
std::cout << "Interest Rate: " << interestRate << "%" << std::endl;
}
};
class CreditCardAccount : public BankAccount {
private:
double creditLimit;
public:
// Default constructor
CreditCardAccount() : creditLimit(0.0) {}
// Parameterized constructor
CreditCardAccount(int accNumber, const std::string& accHolderName, double limit)
: BankAccount(accNumber, accHolderName), creditLimit(limit) {}
// Accessor method for credit limit
double getCreditLimit() const {
return creditLimit;
}
// Override displayAccountInfo() for CreditCardAccount
void displayAccountInfo() override {
BankAccount::displayAccountInfo();
std::cout << "Credit Limit: $" << creditLimit << std::endl;
}
};
int main() {
// Example usage
SavingsAccount savings(12345, "John Doe", 2.5);
CreditCardAccount creditCard(54321, "Jane Smith", 5000.0);
std::cout << "Savings Account Information:" << std::endl;
savings.displayAccountInfo();
std::cout << "\nCredit Card Account Information:" << std::endl;
creditCard.displayAccountInfo();
return 0;
}