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