Help with Array of Objects

Hi all.

I’m supposed to be writing a code that involves arrays of objects and I’m becoming a bit overwhelmed with a certain part of the code. I feel I might be over thinking it though, so any pointers in the right direction would be appreciated.

I’m writing a code that displays a Bank Statement. It involves two classes, one called Transaction and another called Statement. Transaction basically details the transaction itself (the amount, whether it is a withdrawal or deposit, and a note on what the transaction was for). Statement collects the data and compiles it together to display all the transactions that have been done and the end results (end balance, number of withdrawals/deposits, transaction history, etc).

My problem is with a function void EnterTransactions. It’s supposed to place the transaction input into a transaction log array, update a running balance array and adjust the Number of entries, number of deposits/withdrawal and the end balance.

I’m not sure exactly how to fill the arrays since the transactions are an object of the Transaction class? On top of that, we were provided a non-interactive driver to fill the arrays with and I’m a little lost on how to fill the arrays with it (I am use to using user input to fill the arrays.)

Right now my code is broken because of identifier issues (which I am hunting down and trying to fix) so for now I'm just looking to see if I'm even going in the right direction with filling these arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void BankStatement::EnterTransaction() //Transaction Input?
{
        for (NumEntries=0; NumEntries < MaxTransactions; NumEntries++)
	{
		TransactionLog[NumEntries] = Transaction.Transaction(); //Not sure if this is legal.
		//RunningBal[NumEntries] = ??? //Array updated for each transaction entry.
        }

	if(Code == 'D' || Code == 'd')
	{NumDeposits++;}
	else if(Code == 'W' || Code == 'w')
	{NumWithdrawals++;}
	else 
	{cout << "No transaction.";}

}


If there's any other part of the code I need to post, please let me know.
Is that what you've already tried to compile, verbatim? Because the variable "NumEntries" and the two arrays in the for loop don't have a data-type/class-type. Also, the variable "Code" hasn't been declared (not passed as a parameter, or declared as a local variable). If this isn't the case, then ignore this paragraph ^.

I think the syntax you want, in order to explicitly call the Transaction constructor from within another class, is "Transaction::Transaction()".
Last edited on
Currently it won’t compile because of identifier errors and because of errors (I think) associated with the classes.

I have Code identified under the Transaction class, so I'm not sure why it isn't carrying over.

It'd probably help to post all my code, as I’m not really sure how to draw from one class to another (we didn’t have as much focus on classes as I would’ve liked and the last Class related program had fewer things going on at once.)

Transaction.h
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
#include <iostream>
#include <string>

using namespace std;

#ifndef TRANSACTION_H
#define TRANSACTION_H

class Transaction
{
public:
	Transaction();
	Transaction(float InAmount, char InCode, string InNote);
	void SetAmount(float NewAmount);
	float GetAmount();
	void SetCode(char NewCode);
	char GetCode();
	void SetNote(string NewNote);
	string GetNote();

private:
	float Amount;
	char Code;
	string Note;
};

#endif 


Transaction.cpp (Implementation)
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
#include "Transaction.h"
#include <iostream>

using namespace std;

	Transaction::Transaction()
	{
		Transaction();
	};

	Transaction::Transaction(float Amount, char Code, string Note)
	{
		Amount = 0.0f;
		Code = 'D';
		Note = "No Note Included.";
	};

	void Transaction::SetAmount(float NewAmount)
	{
		Amount = NewAmount;
	};


	float Transaction::GetAmount()
	{
		return Amount;
	};

	void Transaction::SetCode(char NewCode)
	{
		Code = NewCode;
	};

	char Transaction::GetCode()
	{
		return Code;
	};

	void Transaction::SetNote(string NewNote)
	{
		Note = NewNote;
	};

	string Transaction::GetNote()
	{
		return Note;
	};


BankStatement.h
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
#include "Transaction.h"

using namespace std;

#ifndef BANKSTATEMENT_H
#define BANKSTATEMENT_H

const int MaxTransactions = 10;

class BankStatement
{
public:
	BankStatement();
	void SetBegBal(float Balance);
	float GetBegBal();
	float GetEndBal();
	int GetNumEnteries();
	void EnterTransaction();//Edit the input for this.
	void DisplayResults();
	void ArrangeTransactions();
	void PrintArranged();
	
private:
	Transaction TransactionLog[MaxTransactions];
	Transaction Arranged[MaxTransactions];
	int NumEntries;
	int NumDeposits;
	int NumWithdrawals;
	float RunningBal[MaxTransactions];
	float BegBal;
	float EndBal;
};

#endif 


Statement.cpp
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
#include "BankStatement.h"
#include "Transaction.h"
#include <iostream>

using namespace std;


BankStatement::BankStatement()

void BankStatement::SetBegBal(float Balance)
{
	BegBal = Balance;
}

float BankStatement::GetBegBal()
{
	return BegBal;
}

float BankStatement::GetEndBal()
{

	return EndBal;
}

int BankStatement::GetNumEnteries()
{
	return NumEntries;
}

void BankStatement::EnterTransaction() //Transaction Input?
{
	for (NumEntries=0; NumEntries < MaxTransactions; NumEntries++)
	{
		TransactionLog[NumEntries] = Transaction::Transaction(); //Not sure if this is legal.
		RunningBal[NumEntries]; //Array updated for each transaction entry.
	}

	if(Code == 'D' || Code == 'd')
	{NumDeposits++;}
	else if(Code == 'W' || Code == 'w')
	{NumWithdrawals++;}
	else 
	{cout << "No transaction.";}

	EndBal = BegBal - RunningBal[NumEntries];
};

void BankStatement::DisplayResults()
{
	cout << "Beginning Balance:" << BegBal << endl;

	for (NumEntries=0; NumEntries < MaxTransactions; NumEntries++)
	{
		cout << "Transaction:" << TransactionLog[NumEntries] << "was a" << Transaction.GetCode << "in the amount of:" << Transaction.GetAmount << "for" << Transaction.GetNote << endl;
	cout << "Running Balance:" << EndBal;
	}
}


//void BankStatement::ArrangeTransactions(); // Haven't figured this out yet.

void BankStatement::PrintArranged()
{
	cout << "Printing Deposits and Withdrawals as a group:" << endl;
	for (NumEntries=0; NumEntries < MaxTransactions; NumEntries++)
	{
		cout << "Transaction was a" << Code << "amount: $" << Amount << "for" << Note << endl;
	}
}


The implementations in BankStatement are what I'm currently working on and what are currently tripping me up.
I'm also curious if it would do me any good to initialize the arrays under the BankStatement::BankStatement() constructor or if that should be done somewhere else.
TransactionLog[NumEntries] = Transaction::Transaction(); //Not sure if this is legal.

You're right, this is not legal. First off, you can't explicitly call a constructor. Second, if you want to use a function of a class, you need to make an object of it.

RunningBal[NumEntries]; //Array updated for each transaction entry.

And this does nothing.

I'm also curious if it would do me any good to initialize the arrays under the BankStatement::BankStatement() constructor or if that should be done somewhere else.


Usually a good idea to initialize your variables in the constructor.

EndBal = BegBal - RunningBal[NumEntries];

This will also have undefined behavior, since you never initialized RunningBal.
Last edited on
Ya, that’s my bad. RunningBal[NumEntries] should’ve been commented out since at the moment I can’t figure out how to add the data collected from the Transaction class into the arrays found in class BankStatement.

I've edited my BankStatement constructor statement to initialize variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
BankStatement::BankStatement()
{
	for (int i=0; i < MaxTransactions; i++)
	{TransactionLog[i] = 0;} //Error: no operator "=" matches these operands

	for (int i=0; i < MaxTransactions; i++)
	{RunningBal[i] = 0;}

	NumEntries = 0;
	NumDeposits = 0;
	NumWithdrawals = 0;

};


I figure the error with TransactionLog is relate to what what you mentioned, but how would I go about making a function of a class into an object? I'm kind of confused. Should I be passing EnterTransaction something so that it in turn can fill the arrays?

My variables Code, Amount and Note are also being called unidentified in the BankStatement.cpp file. But since I've #include <"Transaction.h"> shouldn't it be able to see and use them?
{TransactionLog[i] = 0;} //Error: no operator "=" matches these operands

This doesn't do what you think it's doing, which is why you're getting this error. TransactionLog is an array of Transaction objects. So, do you have an operator= defined anywhere your class? Not likely. What you probably meant to do was set one of the data members of Transaction to 0.

TransationLog[i].setterFunction(0); is an example of what I think you're trying for.

My variables Code, Amount and Note are also being called unidentified in the BankStatement.cpp file. But since I've #include <"Transaction.h"> shouldn't it be able to see and use them?


No, because those are private. Only Transaction objects can see them. You've made a bunch of getter/setters, so you'll use those.
Ah okay. I edited and used the getter/setters and now they're not red-lining (at least.) So that's awesome, thanks. I also think I've gotten the array initialized now.

I'm having an issue understanding the syntax to fill the array up though. I imagine I'll be using a for loop, but would I be using the syntax as you've written it in order to fill TransactionLog?

Would it be along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void BankStatement::EnterTransaction() 
{
	for (NumEntries < MaxTransactions; NumEntries++;)
	{	
		TransactionLog[NumEntries].GetAmount();
		TransactionLog[NumEntries].GetCode();
		TransactionLog[NumEntries].GetNote();
	}

	if (TransactionLog[NumEntries].GetCode() == 'D')
		{NumDeposits++;}

	if (TransactionLog[NumEntries].GetCode() == 'W')
		{NumWithdrawals++;}
	}
}


I'm sorry if it's obvious and I'm missing it. This code is beginning to frustrate me. :/
Well, I just typed out a nice long explanation of this, but accidentally hit cancel instead of submit. So I just lost all of that.

Anyway, here's the short version. A getter function simply retrieves a value so the calling function can use it in some way. It doesn't have any effect on the value store in the object. What you're looking to use is a setter function. A setter function does what it sounds like, its sets a value equal to whatever is passed into the function.
I think I follow you. So I'll use the setter functions I've created to fill the array and any getter functions if I need to use a value for a function.

I made the change (swapped the Get to Set for the three) but get the error of "to few arguments in function call".

Are there parameters I should actually be passing? From what I understood, classes usually don't require parameters to be passed (while in normal global functions they do). Is my syntax still off?

I have to head out here now, so I won't be able to respond until later.
You're thinking of classes wrong. A function inside a class follows the same rules as a function outside a class.

If it has parameters, you need to pass in corresponding arguments. Otherwise, what are you setting to? If you don't give a value, then there is no point.
Topic archived. No new replies allowed.