Help needed on Abstract Classes

Hello, I trying to complete a project for my c++ class, but do not know where to start. The project is a banking assignment using derived classes and pure virtual functions, I will need to derive classes (such as checking and savings) off of the abstract class as described below.

bankAccount: Every bank account has an account number, the name of
the owner, and a balance. Therefore, instance variables such as name,
accountNumber, and balance should be declared in the abstract class
bankAccount. Some operations common to all types of accounts are retrieve
account owner’s name, account number, and account balance; make deposits;
withdraw money; and create monthly statement. So include functions to
implement these operations. Some of these functions will be pure virtual.

I have created what I think should be the bankAccount class, but I am unsure how to build it's constructor / initialize it since I can't instantiate an object of this class.

Any help is appreciated. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <string>
using namespace std;

class bankAccount
{
	
	public:

		
		bankAccount(string fname="", string lname="", int acctNum = 0, double bal = 0); //constructor
		 string getAccountHoldersFname() const; // Get account holders first name
		 string getAccountHoldersLname() const; // Get account holders last name
		 int getAccountNumber() const; 		//get account number
		 int getAccountBalance() const;  // get account balance

		virtual void getMonthlyStatement(int month) = 0; // Print a statement
		
		virtual void deposit(double depositAmount) = 0; // Deposit money into the account

		virtual void withdraw(double withdrawAmount) = 0; // Withdraw money


	protected: // these can be used in derived classes


		void loadAccounts();
		string AccountHoldersFname;
		string AccountHoldersLname;
		int accountNumber;
		double balance;
		

};
The constructor would call the base constructor
1
2
Checking(std::string FName, std::string LName, int AccountNumber, double Balance):
   bankAccount(FName, LName, AccountNumber, Balance)

BTW don't divide by zero, it produces undefined behavior.
BTW don't divide by zero, it produces undefined behavior.
For floating point numbers result is defined. It is only integers who suffers from this. EDIT Peter87 is right. Standard doesn't define behavior of division by zero. It works just because x86 processors use IEEE 754 standard to represent those values. And it actually can lead to some unexpected errors should program be run on anothe architecture. Not that it should bother most of programmers
Last edited on
It's not defined (=undefined) by the C++ standard but it might be defined by some other standard such as IEEE 754.
Thank you pogrady. I will try that constructor in my derived class.

BTW don't divide by zero, it produces undefined behavior.
That's why I like it.
I am still having problems with this assignment. Can someone tell how to load values into my abstract data class or point me in the right direction?

This is what I have so far.

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
using namespace std;

class bankAccount 
{
	
	public:

		 bankAccount(string fname="", string lname="", int acctNum = 0, double bal = 0); //constructor
		 string getAccountHoldersFname() const; // Get account holders first name
		 string getAccountHoldersLname() const; // Get account holders last name
		 int getAccountNumber() const; 		//get account number
		 double getAccountBalance() const;  // get account balance

		virtual void getMonthlyStatement(int month) = 0; // Print a statement
		
		virtual void deposit(double depositAmount) = 0; // Deposit money into the account

		virtual void withdraw(double withdrawAmount) = 0; // Withdraw money


	protected: // these can be used in derived classes

		string AccountHoldersFname;
		string AccountHoldersLname;
		int accountNumber;
		double balance;
};


bankAccount Implementation cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "bankAccount.h"
#include <iostream>

using namespace std;

bankAccount::bankAccount(std::string fname, std::string lname, int acctNum, double bal) 
{
	//loadAccounts();
}

string bankAccount::getAccountHoldersFname() const
{
	return AccountHoldersFname;
}
		 
string bankAccount::getAccountHoldersLname() const
{
	return AccountHoldersLname;
}

int bankAccount::getAccountNumber() const
{
	return accountNumber;
}

double bankAccount::getAccountBalance() const
{
	return balance;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <iostream>
#include <fstream>

#include "bankAccount.h"

using namespace std;

int main()
{
	
	bankAccount *myAccounts[10]; //pointer to an array of 10 bank account pointers??
	

	ifstream inFile;                                 
	inFile.open("customerData.txt");                  

    if (!inFile)                                 
    {
        cout << "Bank account file not found! "<< endl; 
		exit;
                                        
    }

	int count = 0;
	while(!inFile.eof())
	{
		string fname, lname;
		int acctNum;
		double bal;
		
		inFile>>fname>>lname>>acctNum>>bal; //read file values into temp variables
		
		myAccounts[count] = // I give up!! help please

		//bankCustomers[count] = anAccount;
		count++;

	}



	return 0;

} 



1
2
3
4
5
class base{};
class derived{};

base *ptr;
ptr = new derived;
Ok, I've created a derived class of bankAccount called certificate of deposit like so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "bankAccount.h"

class certificateOfDeposit : public bankAccount
{
	private:
		int cdMatMonths; // CD maturity months
		double interestRate; 
		int curMonth; // current CD month

	public:

		//certificateOfDeposit(int = 0, double = 0, int = 0);
		certificateOfDeposit();
		void setCDMatMonths(int cdMatMonths);
		void setInterestRate(double interestRate);
		void setCurMonth(int curMonth);


}


implementation is here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "certificateOfDeposit.h"

certificateOfDeposit::certificateOfDeposit()
{

}

void certificateOfDeposit::setCDMatMonths(int c)
{
	    cdMatMonths = c;
		double interestRate; 
		int curMonth; 
}
void certificateOfDeposit::setInterestRate(double i)
{
	interestRate = i;
}
void certificateOfDeposit::setCurMonth(int m)
{
	curMonth = m;
}


When I attempt to build the project I get this message.

'certificateOfDeposit' : cannot instantiate abstract class

Any ideas? Thank you.
If you intend to instantiate it, don't make it abstract.
You must provide implementation for
1
2
3
virtual void getMonthlyStatement(int month);
virtual void deposit(double depositAmount);
virtual void withdraw(double withdrawAmount);
that were pure virtual in the base class.
Thank you ne555. I created definitions for my pure virtual functions in my derived class and it built successfully. Now my only question is how to populate from a file?
The file needs to store type information.
By instance
1
2
3
4
5
6
for(int K=0; K<size and std::cin >> type; ++K){
   switch(type){
   case checking: accounts[K] = new Checking(/**/); break;
   case saving: accounts[K] = new Saving(/**/); break;
   }
}
O.k. I created an input file for processing the accounts like so.

Where column one is the account type, column 2 = first name, column 3 is last name, column 4 is the account number, column 5 is the balance. (yes they are all broke right now lol) Sorry, it's been a long night and I need to keep some humor going to keep myself from going mad.


CD Mary Ann 00001 0.00
CD Ted Smith 00002 0.00
CHECKING Jane Doe 00003 0.00


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <string>
#include <iostream>
#include <fstream>

//#include "bankAccount.h"
#include "certificateOfDeposit.h"


using namespace std;

int main()
{
	
	// Load accounts
	ifstream inFile;                                 
	inFile.open("customerData.txt");                  

    if (!inFile)                                 
    {
        cout << "Bank account file not found! "<< endl; 
		exit;
                                        
    }

	int TypeChecking = 0; //Counters for acctTypes
	int TypeSavings = 0;
	int TypeCD = 0;

	while(!inFile.eof()) // loop to count bank account types
	{
		string AcctType;
		string fname, lname;
		int acctNum;
		double bal;
		
		inFile>>AcctType>>fname>>lname>>acctNum>>bal; //read file values into temp variables
		
			{
				if(AcctType == "CHECKING")
				{
					TypeChecking++;
				}

				if(AcctType == "SAVINGS")
				{

					TypeSavings++;
				}

				if(AcctType == "CD")
				{

					TypeCD++;
				}

			}
	}

	// create account type pointer arrays

	bankAccount *ptrCDAccounts[TypeCD];
	bankAccount *ptrChkAccounts[TypeChecking];
	bankAccount *ptrSavAccounts[TypeSavings];

	
	// This works --> why wont the above??
	bankAccount *myAccounts[5]; //pointer to an array of bank account pointers??

	myAccounts[0] = new certificateOfDeposit;

	myAccounts[0]->deposit(1.00); //set the ammount for the first account

	myAccounts[1] = new certificateOfDeposit;

	myAccounts[1]->deposit(12.00); //set the amoount for the second

	double tempBal;
	tempBal = myAccounts[0]->getAccountBalance();

	cout << "the balance for account 0 is = : " << tempBal << endl;

	tempBal = myAccounts[1]->getAccountBalance();

	cout << "the balance for account 0 is = : " << tempBal << endl;
	
	
	return 0;

} 
> This works --> why wont the above??
doesn't work is not an error message. Be more explicit.

while(!inFile.eof()) // loop to count bank account types
Don't loop on `eof', loop on the reading. while(input>>type)
eof will stop one step too late.
ne555

Thank you for all of your help so far. I have spoken with my instructor and we will not be reading any data from a file.

The issue I was having was creating an array of pointers like so.

// create account type pointer arrays

1
2
3
	bankAccount *ptrCDAccounts[TypeCD];
	bankAccount *ptrChkAccounts[TypeChecking];
	bankAccount *ptrSavAccounts[TypeSavings];


I believe the reason this didn't work has something to do with early binding? The compiler does not trust an undefined variable during compilation.

If I were to do something like this the compiler would be happy.

1
2
const int myArraySize = 5;
bankAccount *ptrCDAccounts[myArraySize ];


At least that is how I understand it. Either way, this programming model is not needed.

Cheers!
Topic archived. No new replies allowed.