Hi Kristen, you have quite a few things wrong with this, so I am going to list the changes here and then show you a working example.
1) To start, lets look at your header file. You defined a class, but you failed to put in the actual function implementation so there is no way for you to call bank account details and get any information back. You only did one side of the coin here by defining the class "blueprint" but you forgot to tell the compiler how the functions actually work before you started using them. So I put those in and hooked them to your class' private member variables.
2) Now over on your cpp file, you are not creating instances of your classes and then calling them correctly. When you create a class you have to use the "new" keyword to create an instance of your classes and assign them to variables of your class type.
bankAccount *a = new bankAccount("name",balance);The above example is saying declare a variable pointer of type bankAccount called "a". So the variable "a" points to an address to where a bankAccount class is stored.
3) Since you now have pointers to two classes you created, you have to pass these pointers to the display function. Now I noticed you tried a reference here, but being that you have classes and pointers to the classes, you have to dereference those variables. Which means you have to ask the pointer to lookup the object it points to and return a reference to it. We do that using the
-> operator. Which you also may know is the same as
(*anAcct) and then using the dot notation.
4) So once we are in the display function we need to get at the functions you have defined. So we use the -> to call its member variables... which is why you needed to define them back in your header file so they will actually return a value.
5) You defined a "space" variable and then defined it again in the other two functions. You just needed to use it once it was defined.
6) Your call to find the last name had two parameters, where you needed just one. The first argument is where to start in the string and the second is the ending index. I want to make you aware that this will break if the name doesn't have a space. So make sure your data is valid when you get to this point in your program. Like if I put my name as "Martyr2" there is no space so this breaks. You also need to set your firstname and lastname variables to the values returned by the substr call. You also forgot your semicolon on the end of your lastname line.
7) Fetch the name, balance and then print right in display(). That is what it is there for, to display the info. You had a return statement here but the function was set as void (not to return a function). So that was another error. Since you are displaying here, no need to display back in the main function.
Those are the main changes. I took out a few of your operator definitions and your preprocessor declarations just to help simplify things. This is how it all should look.
BACCOUNT.H
CODE
#include <string> // for class string
using namespace std; // avoid having to write std:: as in std::string
///////////////////////////////////////////
/////// class bankAccount defintion ///////
///////////////////////////////////////////
class bankAccount {
public: // class member functions
//--constructors
bankAccount();
bankAccount(string initName, double initBalance);
// post: A bankAccount with two arguments when called like this:
// bankAccount anAcct("Hall", 100.00);
//--modifiers
void deposit(double depositAmount);
// post: depositAmount is credited to this object's balance
void withdraw(double withdrawalAmount);
// post: withdrawalAmount is debited from this object's balance
//--accessors
double balance() const;
// post: return this account's current balance
string name() const;
// post return the account name
private:
string my_name; // Uniquely identify an object
double my_balance; // Store the current balance (non-persistent)
};
// Definition for your parameterless constructor
bankAccount::bankAccount() {
}
// Definition for your constructor that takes parameters.
// Notice how we set the member variables using "this" which refers to the current instance
bankAccount::bankAccount(std::string initName, double initBalance) {
this->my_name = initName;
this->my_balance = initBalance;
}
// Some empty definitions for your other functions
void bankAccount::deposit(double depositAmount) {
}
void bankAccount::withdraw(double withdrawalAmount) {
}
// Definition for returning the customer's balance
double bankAccount::balance() const {
return this->my_balance;
}
// Definition for returning the customer's name
string bankAccount::name() const {
return this->my_name;
}
So as you can see I added definitions with the same signatures as defined by your class. Some of the implementation was filled in for you because it was needed for the display portion of your program. Below is the main file...
CODE
#include <iostream>
#include "BACCOUNT.H"
// Display the account information
void display(const bankAccount *anAcct)
{
// Save the name of the account holder
string name = anAcct->name();
// Break the name on the space
int space = (int) name.find(" ");
// Store the firstname and lastname
string firstname = name.substr(0, space);
string lastname = name.substr(space);
// Get the balance
double balance = anAcct->balance();
// Show all the information for this account
cout << "{bank values:" << lastname << ", " << firstname << ", $" << balance << "}" << endl;
}
int main()
{
// You forgot the "c" in activity
cout << "Kristen Hoeve, CO127, Activity 5.4 "<<endl;
// Define two instances of bank accounts, passing in their initializing details
bankAccount *a = new bankAccount("Annie Hill", 123.00);
bankAccount *b = new bankAccount("Bob Becker", 45.60);
// Pass the pointers to these accounts to our display function
display(a);
display(b);
system ("pause");
return 0;
}
So read through the comments and see how I did everything. If you have any questions, just throw it on this thread and either I or someone else can help you out.
Enjoy!
"At DIC we be coding ninjas!"