vectors

N

nick

The attached program is fine, but I need to create vectors for each
AcctExample object. I know that I can do the following:

vector<AcctExample> example which makes a vector of AcctExample objects
called example. Now, how do I activate the methods for each object?
Also, why are we replacing templates with vectors? (That was what the
assignment required. I don't get why we do this).

ACCTEXAMPLE.CPP
#include <conio.h>
#include <iostream>
#include <fstream>
#include "AcctExample.h"//this file must be included or we get an
undefined
//reference

/*This file has no template functions in it. It's used to demonstrate
some C++
**basics in a multifile program. This file contains the functions that
were
**DECLARED in the .h file of the same name. The .h file has the class
DEFINITION
**with the function DECLARATIONS.*/

////////////////////////////////////////////////////////////////////////////////
//This class is public and used for exception
handling./////////////////////////
////////////////////////////////////////////////////////////////////////////////
// class AnError
//{
//};
////////////////////////////////////////////////////////////////////////////////
//Returns the account balance :: Scoping operator used
here/////////////////////
////////////////////////////////////////////////////////////////////////////////
int AcctExample::getBalance()
{
return acctBalance;

}
////////////////////////////////////////////////////////////////////////////////
//This holds the deposit amount. If a negative number is entered an
exception is
//thrown.///////////////////////////////////////////////////////////////////////

int AcctExample::deposit()
{
int depAmt;
cout << " Please enter deposit amount";
cin >> depAmt;
if (depAmt <= 0)
throw AnError();
return acctBalance + depAmt;

}
////////////////////////////////////////////////////////////////////////////////
//This method takes an int for withAmount and returns it with
acctBalance///////
////////////////////////////////////////////////////////////////////////////////
int AcctExample::withdraw(int withAmount)
{
return acctBalance - withAmount;
}
////////////////////////////////////////////////////////////////////////////////
//This merely sets the
balance//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void AcctExample::setBalance(int bal)
{
acctBalance = bal;
}
////////////////////////////////////////////////////////////////////////////////
//This function is used to display back
information/////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void AcctExample::displayBalance()
{
cout << acctBalance << PIN;
}
////////////////////////////////////////////////////////////////////////////////

ACCTEXAMPLE.H


#include <conio.h>
#include <iostream>
#include <fstream>

using namespace std;
//template <class T>
class AcctExample
{
private:
string fname, lname;
int PIN;
int acctBalance;

public:
//AcctExample();//default constructor
AcctExample(int, int);//constructor
AcctExample(): acctBalance(0), PIN(1234){}//This
intializing
//constructor makes acctBalance 0 and PIN 1234 by default

AcctExample(AcctExample&);//copy constructor
////////////////////////////////////////////////////////////////////////////////
class AnError
{
};
////////////////////////////////////////////////////////////////////////////////
int getBalance();

//returns the account balance
////////////////////////////////////////////////////////////////////////////////

int deposit();

//returns the account balance plus whatever amount was deposited
//if an amount of 0 or less is deposited an error is thrown
////////////////////////////////////////////////////////////////////////////////
int withdraw(int withAmount);

//returns the account balance minus the amount withdrawn
////////////////////////////////////////////////////////////////////////////////

void setBalance(int bal);
//sets the balance to whatever amount you wish
////////////////////////////////////////////////////////////////////////////////

void displayBalance();

//displays the resulting balance and PIN number
////////////////////////////////////////////////////////////////////////////////


};

MAIN.CPP


#include <conio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "AcctExample.h"

using namespace std;


int main()

{
try
{
int loop;
do
{
AcctExample example;
//vector<AcctExample> example;//create a vector of
AcctExample objects
//activate the methods for each object, and assign each
completed
//object as a vector member
example.getBalance();
example.setBalance(example.deposit());
example.getBalance();
example.displayBalance();
ofstream outFile("BKAccount.txt");
outFile << example.getBalance();
system("PAUSE");
cout << "Would you like to repeat the transaction?";
cin >> loop;
}

while(loop != 1);
}

catch(AcctExample::AnError)
{
cout << "There was an error!\n";
cout << "Deposit amounts must be more than 0 dollars";
system("PAUSE");
}
}
 
V

Victor Bazarov

nick said:
The attached program is fine, but I need to create vectors for each
AcctExample object. I know that I can do the following:

vector<AcctExample> example which makes a vector of AcctExample objects
called example. Now, how do I activate the methods for each object?

Probably by using the form

example[index].method(arguments);
Also, why are we replacing templates with vectors? (That was what the
assignment required. I don't get why we do this).

How the hell should _we_ know? Ask the person who gave you the
assignment.
 
J

Josh Mcfarlane

nick said:
Also, why are we replacing templates with vectors? (That was what the
assignment required. I don't get why we do this).

Maybe I'm just missing it, but I fail to see how templates were used /
required in the first place... Did you mean replacing arrays with
vectors? Vectors and templates are two completely seperate things
(Vectors USE templates, but vectors are not a substitute for
templates.)

Nothing in your accounts class seems to require a varying variable type.
 
N

nick

That's probably the point......vectors use templates. I'm just trying
to get all this information into my head.....thanks.
 
N

nick

The compilation of example[0].getBalance() , for instance, goes fine
but the program won't run. It generates a Microsoft error report. What
am I doing wrong?
 
V

Victor Bazarov

nick said:
The compilation of example[0].getBalance() , for instance, goes fine
but the program won't run. It generates a Microsoft error report. What
am I doing wrong?

Does your vector have any elements in it?
 
J

Josh Mcfarlane

nick said:
The compilation of example[0].getBalance() , for instance, goes fine
but the program won't run. It generates a Microsoft error report. What
am I doing wrong?

You can think of vectors as a growable array.

When you initialize a vector with vector<type> foo; it has nothing.
foo[0] points to an invalid member.

In order to use foo[n], the vector must be of size n+1 or more.

You can resize the vector to create members, or add members manually,
but you have to create the data for the vector somehow.

Josh McFarlane
 
N

nick

Thanks, Josh. I'm just a complete newb at this. My reasoning goes like
this:

1. Create an object of type AcctExample.
2. Access the member functions for it.
3. Create a vector of type AcctExample.
4. Assign the first object to the first position.

It doesn't seem necessary, but the purpose of the instruction is to
teach us about simple containers.

Is my reasoning good so far?
 
V

Victor Bazarov

nick said:
Thanks, Josh. I'm just a complete newb at this. My reasoning goes like
this:

1. Create an object of type AcctExample.
2. Access the member functions for it.
3. Create a vector of type AcctExample.
4. Assign the first object to the first position.

It doesn't seem necessary, but the purpose of the instruction is to
teach us about simple containers.

Is my reasoning good so far?

I think you missed the point that standard containers don't come with any
storage _unless_ you tell them to. If you just write

vector<AcctExample> vAccEx;

the vector (vAccEx) is _empty_. You need to either use a different
constructor (the one that takes the size and the value) or populate your
vector with objects as you go. Read about 'push_back' member function or
'std::vector'.

V
 
J

Josh Mcfarlane

nick said:
Thanks, Josh. I'm just a complete newb at this. My reasoning goes like
this:

1. Create an object of type AcctExample.
2. Access the member functions for it.
3. Create a vector of type AcctExample.
4. Assign the first object to the first position.

It doesn't seem necessary, but the purpose of the instruction is to
teach us about simple containers.

Is my reasoning good so far?

That's good reasoning, however, you can't really "assign" the object
you created to the first position in your current example.

You have a few options:
#1. You can add a copy of the current object to the vector using any of
the insertion methods such as push_back, which would create a copy of
the AcctExample example object stored in the vector. I think this is
what you wanted to do when you said assign. After doing this, you can
access the newly created copy of the object via the container.

#2. You can store a vector of pointers, and then add the pointer to the
current object to the vector instead of the object itself. This method
allows you to "assign" positions in the vectors to already defined
objects. However, this method brings up a few other problems (such as
having to make sure the object isn't deconstructed while it is still
listed in the vector).

You might want to look at documentation on vector and it's members in
order to get a few ideas about it's operations. When looking at it in
the beginning, just try to think of it as a variable array when you try
to access the objects inside of it.
 
N

nick

I was looking at an example online, and tried this:

AcctExample account1;//create an AcctExample object
called account1
vector<AcctExample> allAccounts;//create a vector of
type
//AcctExample to store the AcctExample objects
allAccounts[0] = account1;
allAccounts[0].getBalance();
allAccounts[0].setBalance(allAccounts[0].deposit());
allAccounts[0].displayBalance();

This compiles but bombs out. When I try
allAccounts.push_back(account1); instead of allAccounts[0] = account1;
It also bombs out.

I remember making a vector of ints and using push_back with no problem.
It seems that when dealing with objects it's a bit trickier.
 
V

Victor Bazarov

nick said:
I was looking at an example online, and tried this:

AcctExample account1;//create an AcctExample object
called account1
vector<AcctExample> allAccounts;//create a vector of
type
//AcctExample to store the AcctExample objects
allAccounts[0] = account1;
allAccounts[0].getBalance();
allAccounts[0].setBalance(allAccounts[0].deposit());
allAccounts[0].displayBalance();

This compiles but bombs out. When I try
allAccounts.push_back(account1); instead of allAccounts[0] = account1;
It also bombs out.

Bobms out in what way?
I remember making a vector of ints and using push_back with no problem.
It seems that when dealing with objects it's a bit trickier.

It depends on what your 'AcctExample' is. If the object itself does not
try to handle dynamic memory, you should be fine. Can you post a bit more
code to illustrate what you're trying to do?

V
 
J

Josh Mcfarlane

nick said:
I was looking at an example online, and tried this:

AcctExample account1;//create an AcctExample object
called account1
vector<AcctExample> allAccounts;//create a vector of
type
//AcctExample to store the AcctExample objects
allAccounts[0] = account1;
allAccounts[0].getBalance();
allAccounts[0].setBalance(allAccounts[0].deposit());
allAccounts[0].displayBalance();

This compiles but bombs out. When I try
allAccounts.push_back(account1); instead of allAccounts[0] = account1;
It also bombs out.

I remember making a vector of ints and using push_back with no problem.
It seems that when dealing with objects it's a bit trickier.

What message does it give when it bombs out? It appears that you
haven't defined your copy constructor:
AcctExample(AcctExample&);//copy constructor

This is what is called when you execute allAccounts.push_back(account1)
or allAccounts[0] = account1.

HTH,

Josh McFarlane
 
N

nick

Okay, I see what you meant about a copy of it being in the vector. The
copy constructor helps to do this, right?
 
N

nick

It compiles just fine, but when it runs it generates one of those
Microsoft error reports. Is it possible to post the code in here as an
attachment?

The real problem is that I have a difficult time figuring out
programming. I love the stuff, but I'm mentally challenged when it
comes to doing it. So a lot of what is obvious to others means nothing
to me.

Thanks for the help, you guys are awesome!
 
V

Victor Bazarov

nick said:
It compiles just fine, but when it runs it generates one of those
Microsoft error reports. Is it possible to post the code in here as an
attachment?

Please, no attachments. If you want to post your code, post it in
a message, as text (like you did in your first post).

Before you post all the code, consider learning to use a debugger.
You're using one of those IDE things, aren't you? If so, it's gotta
have a debugger. Compile your program with debugging on, then run it
under the debugger and it should tell you where the error happens when
it happens.
The real problem is that I have a difficult time figuring out
programming. I love the stuff, but I'm mentally challenged when it
comes to doing it. So a lot of what is obvious to others means nothing
to me.

We've all been there. It takes practice, just like everything else.
But that's the key part, practice. Unless you figure it out, you won't
learn it. To figure it _out_ you need to keep figuring it, so to speak.
It's worth very little if we just tell you what to fix in your code.

V
 
V

Victor Bazarov

nick said:
Okay, I see what you meant about a copy of it being in the vector. The
copy constructor helps to do this, right?

Right. But if you didn't have it defined, and 'vector' uses it, you would
get a link error. If you do a clean build and can run your program after
that, there were probably no errors, that means you have all functions
needed as defined, and Josh is shooting blanks.
 
N

nick

Here are my files....

..h, .cpp, and main respectively

I have a copy constructor declared in the .h file, but not defined.

#include <conio.h>
#include <iostream>
#include <fstream>

using namespace std;
//template <class T>
class AcctExample
{
private:
string fname, lname;
int PIN;
int acctBalance;

public:
//AcctExample();//default constructor
AcctExample(int, int);//constructor
AcctExample(): acctBalance(0), PIN(1234){}//This
intializing
//constructor makes acctBalance 0 and PIN 1234 by default

AcctExample(AcctExample&);//copy constructor
////////////////////////////////////////////////////////////////////////////////
class AnError
{
};
////////////////////////////////////////////////////////////////////////////////
int getBalance();

//returns the account balance
////////////////////////////////////////////////////////////////////////////////

int deposit();

//returns the account balance plus whatever amount was deposited
//if an amount of 0 or less is deposited an error is thrown
////////////////////////////////////////////////////////////////////////////////
int withdraw(int withAmount);

//returns the account balance minus the amount withdrawn
////////////////////////////////////////////////////////////////////////////////

void setBalance(int bal);
//sets the balance to whatever amount you wish
////////////////////////////////////////////////////////////////////////////////

void displayBalance();

//displays the resulting balance and PIN number
////////////////////////////////////////////////////////////////////////////////


};

#include <conio.h>
#include <iostream>
#include <fstream>
#include "AcctExample.h"//this file must be included or we get an
undefined
//reference

/*This file has no template functions in it. It's used to demonstrate
some C++
**basics in a multifile program. This file contains the functions that
were
**DECLARED in the .h file of the same name. The .h file has the class
DEFINITION
**with the function DECLARATIONS.*/

////////////////////////////////////////////////////////////////////////////////
//This class is public and used for exception
handling./////////////////////////
////////////////////////////////////////////////////////////////////////////////
// class AnError
//{
//};
////////////////////////////////////////////////////////////////////////////////
//Returns the account balance :: Scoping operator used
here/////////////////////
////////////////////////////////////////////////////////////////////////////////


int AcctExample::getBalance()
{
return acctBalance;

}
////////////////////////////////////////////////////////////////////////////////
//This holds the deposit amount. If a negative number is entered an
exception is
//thrown.///////////////////////////////////////////////////////////////////////

int AcctExample::deposit()
{
int depAmt;
cout << " Please enter deposit amount: ";
cin >> depAmt;
if (depAmt <= 0)
throw AnError();
return acctBalance + depAmt;

}
////////////////////////////////////////////////////////////////////////////////
//This method takes an int for withAmount and returns it with
acctBalance///////
////////////////////////////////////////////////////////////////////////////////
int AcctExample::withdraw(int withAmount)
{
return acctBalance - withAmount;
}
////////////////////////////////////////////////////////////////////////////////
//This merely sets the
balance//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void AcctExample::setBalance(int bal)
{
acctBalance = bal;
}
////////////////////////////////////////////////////////////////////////////////
//This function is used to display back
information/////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void AcctExample::displayBalance()
{
cout << "The deposit amount is " << acctBalance;
cout << "\nThe PIN number is " << PIN;
}
////////////////////////////////////////////////////////////////////////////////




#include <conio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "AcctExample.h"

using namespace std;


int main()

{
try
{
int loop;
do
{
AcctExample account1;//create an AcctExample object
called account1
//account1.getBalance();//use all the methods for
account1
//account1.setBalance(account1.deposit());
//account1.getBalance();
//account1.displayBalance();
vector<AcctExample> allAccounts;//create a vector of
type
//AcctExample to store the AcctExample objects
allAccounts[0] = account1;
allAccounts[0].getBalance();//make element 0 hold
account1
allAccounts[0].setBalance(allAccounts[0].deposit());
allAccounts[0].displayBalance();
//ofstream outFile("BKAccount.txt");
//outFile << account1.getBalance();
system("PAUSE");
cout << "Would you like to repeat the transaction?";
cin >> loop;
}

while(loop != 1);
}

catch(AcctExample::AnError)
{
cout << "There was an error!\n";
cout << "Deposit amounts must be more than 0 dollars";
system("PAUSE");
}
}
 
N

nick

I think I figured it out. My copy constructor was written incorrectly.
When I rewrote it the program compiled and ran fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top