473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

vectors

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");
}
}

Aug 3 '05 #1
19 1919
nick wrote:
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.
[..]

Aug 3 '05 #2
nick wrote:
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.

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

Aug 3 '05 #4
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?

Aug 3 '05 #5
nick wrote:
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?
Aug 3 '05 #6
nick wrote:
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

Aug 3 '05 #7
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?

Aug 3 '05 #8
nick wrote:
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
Aug 3 '05 #9
nick wrote:
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.

Aug 3 '05 #10
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.

Aug 3 '05 #11
Thanks, Victor.

Aug 3 '05 #12
nick wrote:
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

Aug 3 '05 #13
nick wrote:
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
Aug 3 '05 #14
Okay, I see what you meant about a copy of it being in the vector. The
copy constructor helps to do this, right?

Aug 3 '05 #15
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!

Aug 3 '05 #16
nick wrote:
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
Aug 3 '05 #17
nick wrote:
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.
Aug 3 '05 #18
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");
}
}

Aug 3 '05 #19
I think I figured it out. My copy constructor was written incorrectly.
When I rewrote it the program compiled and ran fine.

Aug 4 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.