473,663 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<AcctExam ple> 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 "AcctExampl e.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::ge tBalance()
{
return acctBalance;

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

int AcctExample::de posit()
{
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::wi thdraw(int withAmount)
{
return acctBalance - withAmount;
}
////////////////////////////////////////////////////////////////////////////////
//This merely sets the
balance//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

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

void AcctExample::di splayBalance()
{
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(Acc tExample&);//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 "AcctExampl e.h"

using namespace std;
int main()

{
try
{
int loop;
do
{
AcctExample example;
//vector<AcctExam ple> example;//create a vector of
AcctExample objects
//activate the methods for each object, and assign each
completed
//object as a vector member
example.getBala nce();
example.setBala nce(example.dep osit());
example.getBala nce();
example.display Balance();
ofstream outFile("BKAcco unt.txt");
outFile << example.getBala nce();
system("PAUSE") ;
cout << "Would you like to repeat the transaction?";
cin >> loop;
}

while(loop != 1);
}

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

Aug 3 '05 #1
19 1939
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<AcctExam ple> 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(argumen ts);
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......vect ors 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<AcctExam ple> 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

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

Similar topics

10
15825
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, so I'd like to use an STL container class template such as valarray or vector to represent 1D arrays, and valarrays or vectors of valarrays or vectors to represent 2D arrays. As I said the sizes of the arrays in my intended application are...
5
3410
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 objects in the vector vec_A. Suppose there are K attributes to be added. For each of the attributes I define K vectors of appropriate types. Say, the attributes have types type1, type2, ..., typeK. So I define std::vector<type1> attr1(vec_A.size());
5
2312
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 seem to be (in my eyes at least) are glorified Arrays. - Now I know there's a bit more difference, but what exactly are the advantages of Arrays over Vectors (if any)? Oh, and please keep it in mind I am a real beginner in C++ but can
19
4366
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 of deque are quite complex. Why couldn't I implement deque with a couple of vectors V,W where the deque is the element of V in reverse order followed by W? This would appear to me to satisfy all the conditions, and be significantly simpler. Am I...
3
3238
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 there anyway where I can accomodate both the vector types into a single set. Something like a set<vector<void*>, my_compare func >. Right now, I am having them as two different set dayatypes.
4
2012
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
18133
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 " << v1.size( ) << endl; v1.clear( ); //clears the vector I have a few questions:
2
8683
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 described in the text below. You do not need to change anything in main(). In void prob1(void), take a double floating-point number x from the keyboard and compute the function f(x), which is defined by:
1
2058
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 function that handles vectors of vectors of <typename T(which could be anything from int to vectors of something else). As well, I have specialized/overloaded functions to handle the single-level vectors of data (e.g. vector<string>). So the templated...
2
3393
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 unsorted vectors with dupes, result does not need to be sorted 4) merge 2 unsorted vectors without dupes, result does not need to be sorted
0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.