Virtual functions and class inheritance

Hello,I am making a project that has 5 different classes (given they are pretty small classes for the most part), one of which called Transaction that has a virtual function included in it. By the advice I was given, the function is supposed to display data gathered by other inherited functions. I was also told the default constructor function has no code necessary for this project as well.

I haven't worked with a virtual function and after reading up on it and trying to apply it to here, I still couldn't seem to wrap my head around it.. There is a pretty decent chunk of code underneath, and I'll try to comment on the parts that are of concern to me.. Thank you all for any help or insight you can bring to this, it means a lot!

-Cooper

and for code..

Deposit.h

1
2
3
4
5
6
7
8
9
10
#pragma once
#include "Account.h"
#include "Transaction.h"

class Deposit : Transaction
{
public:
	Deposit();
	void execute(Account *acct, double amt);
};

Deposit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include "Deposit.h"
#include "Account.h"

Deposit::Deposit()
{

}
void execute(Account *acct, double amt)
{
	acct->setAcctbalance(acct->getAcctBalance() + amt);
}

Transaction.h
1
2
3
4
5
6
7
#pragma once
class Transaction
{
public:
	Transaction();
	virtual void execute();
};


Transaction.cpp (The one with the problem)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "Transaction.h"
#include <iostream>

using namespace std; 

Transaction::Transaction()
{
	
}
void execute() 
{
// No idea what to put here to display.	
}

Account.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
 
#include "stdafx.h"
#include "Account.h"
#include <iostream>
#include <string>

using namespace std;

Account::Account()
{
	acctNumber = 100000;
	acctBalance = 0;
	acctOwner = "No name";
}
Account::Account(long acctNumber, double acctBalance, std::string acctOwner)
{
	acctNumber = acctNumber;
	acctBalance = acctBalance;
	acctOwner = acctOwner;
}
void Account::setAcctNumber(long acctN)
{
	acctNumber = acctN;
}
void Account::setAcctbalance(double balance)
{
	acctBalance = balance;
}
void Account::setAcctOwner(std::string name)
{
	acctOwner = name;
}
void Account::display()
{
	getAcctNumber();
	cout << "     " << "Current balance:  $ " << getAcctBalance() << "     " << getAcctOwner();
}
long Account::getAcctNumber()
{
	return acctNumber;
}
double Account::getAcctBalance()
{
	return acctBalance;
}
std::string Account::getAcctOwner()
{
	return acctOwner;
}

AccountTransactoin.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
#include "stdafx.h"
#include <iostream>
#include "Account.h"
#include "Transaction.h"
#include "Deposit.h"
#include "Withdrawal.h"
#include "BalanceInquiry.h"
#include <iostream>

using namespace std;

int main()
{
	Deposit *dpTrans[5];
	Withdrawal *wdTrans[5];
	Account *acct[5];

	acct[0] = new Account(100001, 6350.00, "James L. Hetherington");
	acct[1] = new Account(100006, 0.00, "Eleanor M. Hastings");
	acct[2] = new Account(100007, 55.90, "Caleb J. Roth");
	acct[3] = new Account(100012, 354.19, "Rhonda Marie Sims");
	acct[4] = new Account(100023, 8842.67, "Arthur Crawford Broan");

	for (int i = 0; i < 5; i++)
	{
		acct[i]->display();
	}
	for (int i = 0; i < 5; i++)
	{
		dpTrans[i] = new Deposit;
		wdTrans[i] = new Withdrawal;
	}
	dpTrans[0]->execute(acct[0], 550);
	dpTrans[1]->execute(acct[1], 259.67);
	dpTrans[2]->execute(acct[2], 411.19);
	dpTrans[3]->execute(acct[3], 245.81);
	dpTrans[4]->execute(acct[4], 1157.33);

	BalanceInquiry	inquiry;
	inquiry.execute(acct[02]);
	inquiry.execute(acct[04]);

	wdTrans[0]->execute(acct[0], 200);
	wdTrans[1]->execute(acct[1], 100);
	wdTrans[2]->execute(acct[2], 350);
	wdTrans[3]->execute(acct[3], 1);
	wdTrans[4]->execute(acct[4], 2500);

	cout << endl;

	for (int i = 0; i < 5; i++)
	{
		acct[i]->display();
	}
	cout << endl;
	system("pause");
	return 0;
}


The other classes are similar to the nature of Deposit, just different actions and I figured the giant wall of code posted is probably a lot to begin with. Thank you again for any help.
Last edited on
Transaction should be an interface for Withdrawal and Deposit classes. Hence:

1
2
3
4
5
6
class Transaction
{
public:
	Transaction();
	virtual void execute() = 0;
};


Then you will not need Transaction.cpp.

Also, by looking at the main code I see that all you need is one object per Withdrawal and Deposit classes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 Deposit dpTrans;
Withdrawal wdTrans;

// ...

for (int i=0; i<5; ++i)
{
    dpTrans.execute(acct[i], random_amt());
}

// ...

for (int i=0; i<5; ++i)
{
    wdTrans.execute(acct[i], random_amt());
}


Of course you must write your random_amt() function.
Topic archived. No new replies allowed.