Where am I going wrong? Define a bankAccount class that implements some basic pr
ID: 3605069 • Letter: W
Question
Where am I going wrong?
Define a bankAccount class that implements some basic properties of a bank account. An object of this class should store the following data: Account holder's name (string), account number (int), balance (double), account type (string, checking/saving) and interest rate (double). Store interest rate as a decimal number. Add appropriate member functions to manipulate the object. Use a static member in the class to automatically assign account numbers. Also declare an array of 12 components of type bankAccount to process up to 12 customers and write a program to illustrate how to use your class.
#include
using namespace std;
//Define a bankAccount class that implements some basic properties of a bank account.
class bankAccount {
public:
//Add appropriate member functions to manipulate the object.
void setHolder(string fn = "", string ln = "");
void setAccountType(string chck, string save);
void setAccountNum(int num);
void setBalance(double amount);
void setInterest(double rate);
string getHolder() const;
string getAccountType() const;
int getAccountNum() const;
double getBalance() const;
//Store interest rate as a decimal number
double getInterest() const;
void print() const;
private:
string firstName;
string lastName;
string checking;
string saving;
static int account;
double balance;
double newBalance;
double interest;
};
//Main Function
#include
#include
#include
#include "bankAccount.h"
using namespace std;
void bankAccount::setHolder(string fn, string ln) {
cout << "Enter your first name: ";
cin >> fn;
cout << " Enter your last name: ";
cin >> ln;
firstName = fn;
lastName = ln;
}
string bankAccount::getHolder() const {
return firstName, lastName;
}
void bankAccount::setAccountType(string chck, string save) {
char temp;
cout << "Checking/Saving" << "Enter 'c' for checking or 's' for saving: " << endl;
cin >> temp;
switch(temp){
case 'c':
checking = chck;
interest = 0.0;
break;
case 's':
saving = save;
interest = 1.2;
break;
}
}
string bankAccount::getAccountType() const {
return checking, saving;
}
void bankAccount::setAccountNum(int num){
for (int i = 0; i <= 9; i++)
num += rand() % 10;
account = num;
}
void bankAccount::setBalance(double amount) {
int temp;
cout << "How much would you like to deposit? " << endl;
cin >> amount;
balance = amount;
temp = balance * interest;
newBalance = balance + temp;
}
double bankAccount::getBalance() const {
return balance;
return newBalance;
}
int bankAccount::getAccountNum() const {
return account;
}
void bankAccount::setInterest(double rate) {
interest = rate;
}
double bankAccount::getInterest() const {
return interest;
}
void bankAccount::print() const {
cout << "Member: " << lastName << ", " << firstName << " Account Type: " << checking, saving;
cout << " - " << account << "$" << balance << " Interest Rate: " << interest << endl;
cout << "projected balance after 12 months: " << newBalance;
}
int main() {
cout << "Welcome to a Wicked Credit Union. Lets set up a new account." << endl;
//array of 12 components
bankAccount newMember[12];
newMember->print();
cout << endl << endl;
system("pause");
return 0;
Explanation / Answer
Hi,
You are wrote a quite good program. But there are small mistakes that make your code doesn't run.
Let me help you :
1) #inlcude
this should not be empty. Whenever preprocessor see this it expects some file name. So this should not be left empty. Otherwise remove it.
2) rand()
rand() function needs the cstdlib header file so just write a #include at the top to include the header file
3)account number
You are using the account number as static. Static data maintains a single copy of the varibale. So it restricts its usage inside a class object. And that too you are mainitaining a unique number for each account using rand() method. Then there is no need of static.
I think these are the main changes that need to be done.
So after all the changes :
bankAccount.hpp
-------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
//Define a bankAccount class that implements some basic properties of a bank account.
class bankAccount {
public:
//Add appropriate member functions to manipulate the object.
void setHolder(string fn = "", string ln = "");
void setAccountType(string chck, string save);
void setAccountNum(int num);
void setBalance(double amount);
void setInterest(double rate);
string getHolder() const;
string getAccountType() const;
int getAccountNum() const;
double getBalance() const;
//Store interest rate as a decimal number
double getInterest() const;
void print() const;
private:
string firstName;
string lastName;
string checking;
string saving;
int account;
double balance;
double newBalance;
double interest;
};
---------------------------------------------------------------------------------------------------------
bankAccount.cpp
---------------------------------------------------------------------------------------------------------
#include<iostream>
#include<cstdlib>
#include "bankAccount.hpp"
using namespace std;
void bankAccount::setHolder(string fn, string ln) {
cout << "Enter your first name: ";
cin >> fn;
cout << " Enter your last name: ";
cin >> ln;
firstName = fn;
lastName = ln;
}
string bankAccount::getHolder() const {
return firstName, lastName;
}
void bankAccount::setAccountType(string chck, string save) {
char temp;
cout << "Checking/Saving" << "Enter 'c' for checking or 's' for saving: " << endl;
cin >> temp;
switch(temp){
case 'c':
checking = chck;
interest = 0.0;
break;
case 's':
saving = save;
interest = 1.2;
break;
}
}
string bankAccount::getAccountType() const {
return checking, saving;
}
void bankAccount::setAccountNum(int num){
for (int i = 0; i <= 9; i++)
num += rand() % 10;
account = num;
}
void bankAccount::setBalance(double amount) {
int temp;
cout << "How much would you like to deposit? " << endl;
cin >> amount;
balance = amount;
temp = balance * interest;
newBalance = balance + temp;
}
double bankAccount::getBalance() const {
return balance;
return newBalance;
}
int bankAccount::getAccountNum() const {
return account;
}
void bankAccount::setInterest(double rate) {
interest = rate;
}
double bankAccount::getInterest() const {
return interest;
}
void bankAccount::print() const {
cout << "Member: " << lastName << ", " << firstName << " Account Type: " << checking, saving;
cout << " - " << account << "$" << balance << " Interest Rate: " << interest << endl;
cout << "projected balance after 12 months: " << newBalance;
}
------------------------------------------------------------------------------------------------------------------------
Main.cpp
---------------------------------------------------------------------------------------------------------------
#include<iostream>
#include "bankAccount.hpp"
int main() {
cout << "Welcome to a Wicked Credit Union. Lets set up a new account." << endl;
//array of 12 components
bankAccount newMember[12];
newMember->print();
cout << endl << endl;
system("pause");
return 0;
}
----------------------------------------------------------------------------------------------------------------------
/* try to initialize the newMember objects */
/* hope this helps */
/* if any queries please comment */
/* thank you */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.