Return type of class functions

I was given some code that looked like this (I added the default constructor and private declarations) and asked to make a program with it (I was also given a main function that did all the manipulation of money and output, but that didn't seem relevant to include). Seems easy, except the functions that are declared of type PiggyBank (PiggyBank& PiggyBank::addPennies(int p)). What do I return for these???

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
#ifndef PIGGYBANK_H
#define PIGGYBANK_H

class PiggyBank
{
public:
	PiggyBank();
	PiggyBank(int pennies, int nickels, int dimes, int quarters);

	// Return the number of coins in the bank
	int getPenniesCount() const;
	int getNickelsCount() const;
	int getDimesCount() const;
	int getQuartersCount() const;

	// Add coins to the bank
	PiggyBank& addPennies(int p);
	PiggyBank& addNickels(int n);
	PiggyBank& addDimes(int d);
	PiggyBank& addQuarters(int q);

	// Withdraw coins from the bank, return number withdrawn
	int withdrawPennies(int p);
	int withdrawNickels(int n);
	int withdrawDimes(int d);
	int withdrawQuarters(int q);

	void displayBalance() const;
	void breakTheBank(); // Display the balance then cash out (all counts zeroed).

private:
	int pennies;
	int nickels;
	int dimes;
	int quarters;
};

#endif 

I guess I'll attach my other code.

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
#include "PiggyBank.h"
#include <iostream>
using namespace std;

PiggyBank::PiggyBank()
{}
PiggyBank::PiggyBank(int pennies, int nickels, int dimes, int quarters)
{
	pennies = 0;
	nickels = 0;
	dimes = 0;
	quarters = 0;
}
int PiggyBank::getPenniesCount() const
{
	return pennies;
}
int PiggyBank::getNickelsCount() const
{
	return nickels;
}
int PiggyBank::getDimesCount() const
{
	return dimes;
}
int PiggyBank::getQuartersCount() const
{
	return quarters;
}
PiggyBank& PiggyBank::addPennies(int p)
{
	pennies += p;
}
PiggyBank& PiggyBank::addNickels(int n)
{
	nickels += n;
}
PiggyBank& PiggyBank::addDimes(int d)
{
	dimes += d;
}
PiggyBank& PiggyBank::addQuarters(int q)
{
	quarters += q;
}
int PiggyBank::withdrawPennies(int p)
{
	pennies -= p;
	return pennies;
}
int PiggyBank::withdrawNickels(int n)
{
	nickels -= n;
	return nickels;
}
int PiggyBank::withdrawDimes(int d)
{
	dimes -= d;
	return dimes;
}
int PiggyBank::withdrawQuarters(int q)
{
	quarters -= q;
	return quarters;
}
void PiggyBank::displayBalance() const
{
	cout << "blah";
}
void PiggyBank::breakTheBank()
{
	pennies = 0;
	nickels = 0;
	dimes = 0;
	quarters = 0;
}


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
#include <iostream>
using std::cout;
using std::endl;

#include "PiggyBank.h"

int main()
{
	PiggyBank firstPig;
	cout << "(1) firstPig balance is ";
	firstPig.displayBalance();
	cout << endl;

	firstPig.addPennies(17).addNickels(6).addDimes(3).addQuarters(5);
	cout << "(2) firstPig balance is now ";
	firstPig.displayBalance();
	cout << endl;

	cout << "(3) Tried to withdraw 4 nickels, got "
		<< firstPig.withdrawNickels(4) << " nickels, balance is now ";
	firstPig.displayBalance();
	cout << endl
		<< "    with a nickel count of "
		<< firstPig.getNickelsCount()
		<< " in the piggy" << endl;

	cout << "(4) Tried to withdraw 5 dimes, got "
		<< firstPig.withdrawDimes(5) << " dimes, balance is now ";
	firstPig.displayBalance();
	cout << endl
		<< "    with a dime count of "
		<< firstPig.getDimesCount()
		<< " in the piggy" << endl;

	firstPig.addDimes(2).addQuarters(-1);
	cout << "(5) Tried to add 2 dimes and -1 quarters, "
		<< "balance is now ";
	firstPig.displayBalance();
	cout << endl
		<< "    with a dime count of "
		<< firstPig.getDimesCount()
		<< " and a quarter count of "
		<< firstPig.getQuartersCount()
		<< " in the piggy" << endl;

	cout << "(6) ";
	firstPig.breakTheBank();
	cout << "    and the piggy now contains ";
	firstPig.displayBalance();
	cout << endl;

	return 0;
}
What do I return for these???

Return *this. That's what lets this statement from main() work:
firstPig.addPennies(17).addNickels(6).addDimes(3).addQuarters(5);
Topic archived. No new replies allowed.