I'm currently working on a project for an online banking service and I keep running into the same problem here no matter what I try. The line in question I keep getting an error for is commented in the Account.cpp file
Customer.h:
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
class Customer {
public:
Customer();
Customer(const std::string& firstName, const std::string& lastName, const std::string& address, const std::string& email);
void displayCustomer();
private:
std::string firstName;
std::string lastName;
std::string address;
std::string email;
};
#endif // CUSTOMER_H
Customer.cpp:
#include "Customer.h"
#include <iostream>
#include <iomanip>
Customer::Customer() {
// Default constructor
}
Customer::Customer(const std::string& firstName, const std::string& lastName, const std::string& address, const std::string& email)
: firstName(firstName), lastName(lastName), address(address), email(email) {
// Overloaded constructor
}
void Customer::displayCustomer() {
std::cout << "Name: " << firstName << " " << lastName << std::endl;
std::cout << "Address: " << address << std::endl;
std::cout << "Email: " << email << std::endl;
}
Account.h:
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "Customer.h"
class Account {
public:
Account();
Account(int accountNumber, double balance, const Customer& user);
double getBalance() const;
void setBalance(double balance);
virtual bool deposit(double amount) = 0;
virtual bool withdraw(double amount) = 0;
virtual void displayAccount() const;
protected:
int accountNumber;
double balance;
Customer user;
};
#endif // ACCOUNT_H
Account.cpp:
#include "Account.h"
#include <iostream>
#include <iomanip>
Account::Account() {
// Default constructor
}
Account::Account(int accountNumber, double balance, const Customer& user)
: accountNumber(accountNumber), balance(balance), user(user) {
// Overloaded constructor
}
double Account::getBalance() const {
return balance;
}
void Account::setBalance(double balance) {
this->balance = balance;
}
void Account::displayAccount() const {
std::cout << "Account number: " << accountNumber << std::endl;
user.displayCustomer();
std::cout << "Balance: $" << std::fixed << std::setprecision(2) << balance << std::endl;
}
CheckingAccount.h:
#ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H
#include "Account.h"
class CheckingAccount : public Account {
public:
CheckingAccount();
CheckingAccount(int accountNumber, double balance, const Customer& user, double transactionFee);
double getTransactionFee() const;
bool deposit(double amount) override;
bool withdraw(double amount) override;
void displayAccount() const override;
private:
double transactionFee;
};
#endif // CHECKINGACCOUNT_H
CheckingAccount.cpp:
#include "CheckingAccount.h"
#include <iostream>
#include <iomanip>
CheckingAccount::CheckingAccount()
: transactionFee(5.0) {
// Default constructor
}
CheckingAccount::CheckingAccount(int accountNumber, double balance, const Customer& user, double transactionFee)
: Account(accountNumber, balance, user), transactionFee(transactionFee) {
// Overloaded constructor
}
double CheckingAccount::getTransactionFee() const {
return transactionFee;
}
bool CheckingAccount::deposit(double amount) {
if (amount >= 0) {
double newBalance = getBalance() + amount - transactionFee;
setBalance(newBalance);
return true;
}
return false;
}
bool CheckingAccount::withdraw(double amount) {
if (amount >= 0 && amount <= getBalance()) {
double newBalance = getBalance() - amount - transactionFee;
setBalance(newBalance);
return true;
}
return false;
}
void CheckingAccount::displayAccount() const {
std::cout << "Checking Account ----" << std::endl;
Account::displayAccount();
std::cout << "Transaction fee: $" << std::fixed << std::setprecision(2) << transactionFee << std::endl;
}
SavingsAccount.h:
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include "Account.h"
class SavingsAccount : public Account {
public:
SavingsAccount();
SavingsAccount(int accountNumber, double balance, const Customer& user, double interestRate);
double getInterestRate() const;
double calcInterest();
void displayAccount() const override;
private:
double interestRate;
};
#endif // SAVINGSACCOUNT_H
SavingsAccount.cpp:
#include "SavingsAccount.h"
#include <iostream>
#include <iomanip>
SavingsAccount::SavingsAccount()
: interestRate(0.02) {
// Default constructor
}
SavingsAccount::SavingsAccount(int accountNumber, double balance, const Customer& user, double interestRate)
: Account(accountNumber, balance, user), interestRate(interestRate) {
// Overloaded constructor
}
double SavingsAccount::getInterestRate() const {
return interestRate;
}
double SavingsAccount::calcInterest() {
double interest = getBalance() * interestRate;
setBalance(getBalance() + interest);
return interest;
}
void SavingsAccount::displayAccount() const {
std::cout << "Savings Account ----" << std::endl;
Account::displayAccount();
std::cout << "Interest rate: " << interestRate * 100 << "%" << std::endl;
}
Tester.cpp:
#include <iostream>
#include <iomanip>
#include "Account.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
using namespace std;
int main() {
int selection = 0;
string firstName, lastName, address, email;
double amount;
// Input user's information
cout << "Enter your first name: ";
cin >> firstName;
cout << "Enter your last name: ";
cin >> lastName;
cout << "Enter your address: ";
cin.ignore();
getline(cin, address);
cout << "Enter your email: ";
cin >> email;
// Create a Customer object
Customer customer(firstName, lastName, address, email);
// Create CheckingAccount and SavingsAccount objects
CheckingAccount checkingAccount(1111, 0.0, customer, 5.0);
SavingsAccount savingsAccount(2222, 0.0, customer, 0.02); // Also running into error here "object of abstract class type "SavingsAccount" is not allowed
cout << "Your checking account and savings account are created successfully." << endl << endl;
while (selection != 8) {
cout << "Please select a service from the menu:" << endl;
cout << "1. Display accounts" << endl;
cout << "2. Deposit to checking account" << endl;
cout << "3. Deposit to savings account" << endl;
cout << "4. Withdraw from checking account" << endl;
cout << "5. Withdraw from savings account" << endl;
cout << "6. Transfer money from checking to savings" << endl;
cout << "7. Transfer money from savings to checking" << endl;
cout << "8. Exit" << endl;
cin >> selection;
switch (selection) {
case 1:
checkingAccount.displayAccount();
savingsAccount.displayAccount();
break;
case 2:
cout << "Enter the amount to deposit to checking account: ";
cin >> amount;
if (checkingAccount.deposit(amount)) {
cout << "Deposit is successful." << endl;
} else {
cout << "Invalid deposit amount." << endl;
}
break;
case 3:
cout << "Enter the amount to deposit to savings account: ";
cin >> amount;
if (savingsAccount.deposit(amount)) {
cout << "Deposit is successful." << endl;
} else {
cout << "Invalid deposit amount." << endl;
}
break;
case 4:
cout << "Enter the amount to withdraw from checking account: ";
cin >> amount;
if (checkingAccount.withdraw(amount)) {
cout << "Withdraw is successful." << endl;
} else {
cout << "Invalid withdrawal amount or insufficient balance." << endl;
}
break;
case 5:
cout << "Enter the amount to withdraw from savings account: ";
cin >> amount;
if (savingsAccount.withdraw(amount)) {
cout << "Withdraw is successful." << endl;
} else {
cout << "Invalid withdrawal amount or insufficient balance." << endl;
}
break;
case 6:
cout << "Enter the amount to transfer from checking to savings: ";
cin >> amount;
if (checkingAccount.withdraw(amount) && savingsAccount.deposit(amount)) {
cout << "Transfer is successful." << endl;
} else {
cout << "Invalid transfer amount or insufficient balance in checking account." << endl;
}
break;
case 7:
cout << "Enter the amount to transfer from savings to checking: ";
cin >> amount;
if (savingsAccount.withdraw(amount) && checkingAccount.deposit(amount)) {
cout << "Transfer is successful." << endl;
} else {
cout << "Invalid transfer amount or insufficient balance in savings account." << endl;
}
break;
case 8:
cout << "Thanks for using Bank of Wake!" << endl;
break;
default:
cout << "Invalid selection." << endl;
break;
}
}
return 0;
}
You may not call a non-constant member function from a constant member function.
Within the class
Customerthe member functiondisplayCustomeris a non-constant member finction:But it is called from constant member functions as for example from the function
displayAccountof the classAccountWithin a constant member function the object for which the function is called is considered as a constant object. That is members of such an object are accessed using the pointer
thisof the typeconst class_object *. There is no implicit conversion from a pointer to a constant object to a pointer to a non-constant object.Declare the member function
displayCustomeras a constant function