Please help.... composition!!!

I need to write a program with 9 files: 4 header files with 4 implementation files and one Driver. There will be one base class (Account) and 2 subclasses (Checking,Saving). The last of the four classes is Customer; Account has a Customer data member and uses it in some of its functions. The process of what is supposed to happen is self-explanatory based on the header file.
I didn't include my saving and chekcing class files, because my inheritance parts of this code is working perfectly.
I have a problem with composition which are account class and customer class.
my output does not display name and accountId for customer class member variable.
my output looks like:

Savings Account:
Name:
Account #:
Balance: $ 1540

Account class constructor has to call overloaded assignment operator from customer class.
and Account class view() funtion calls customer view() function to display customer member variable name and accountID. (Which is my problem here)
The test.cpp file is already given so I can't change anything in test.cpp.

Please help me out where I did wrong.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>

class Customer
{
	private:
		char accountID[7];
		char name[20];

	public:
		Customer();
		Customer(char [], char []);
		void view();
		Customer operator= (const Customer &);
};
#endif


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
//customer.cpp
#include <iostream>
#include <string>
#include "customer.h"
using namespace std;

Customer::Customer()
{
	for (int x = 0; x < 20; x++)
		name[x] = NULL;

	for (int x = 0; x < 7; x++)
		accountID[x]= NULL;
}

Customer::	Customer(char n[], char id[]) 
{
	cout << "customer constructor is working" << endl;
	strcpy_s (name, n);
	strcpy_s (accountID, id);
}
	
void Customer::view()
{
	cout << "Name: ";
	for(int x = 0; name[x] != NULL; x++)
		cout << name[x];

	cout << "\nAccount #: ";
	for(int x = 0; accountID[x] != NULL; x++)
		cout << accountID[x];
}

Customer Customer::operator= (const Customer &obj)
{
	cout << "ASSINGMENT OPERATOR = IS WORKING NOW!!!! " << endl;

	for(int x = 0; accountID[x] != '\0'; x++)
		accountID[x] = obj.accountID[x];
	
	for(int x = 0; name[x] != '\0'; x++)
		name[x] = obj.name[x];

	return *this;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "customer.h"

class Account
{
	protected:
		float balance;
		Customer cust;

	public:
		Account();
		Account(float, Customer);
		void makeDeposit(float);
		float getBalance();
		virtual bool makeWithdrawal(float);
		virtual void adjustBalance();
		virtual void view();
};
#endif 


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
//account.cpp
#include <iostream>
#include "customer.h"
#include "account.h"
using namespace std;


Account::Account()
{
	balance = 0.0;
}

Account::Account(float Bal, Customer pCust) : balance(Bal)
{
	cust = pCust;
}

void Account::makeDeposit(float depos)
{
	balance += depos;
}

bool Account::makeWithdrawal(float withd)
{
	balance -= withd;
	return true;
}

void Account::view()
{
//	Customer theCustomer;
//	theCustomer.view();
	cust.view();
	cout << "\nBalance: $ " << balance << endl;
}

float Account::getBalance()
{
	return balance;
}

void Account::adjustBalance()
{}


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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//test.cpp
#include <iostream>
#include "account.h"
#include "checking.h"
#include "savings.h"
using namespace std;

const int MAX = 4;
void doTransactions (Account*);
const int NAME_SIZE = 20;
const int ID_SIZE = 6;

int main()
{
	Account* acctsPtr[MAX];
	char acctType;
	bool validType = true;
	char custName[NAME_SIZE];
	char acctID[ID_SIZE];
	double startBal;
	int aNum;

	for (aNum = 0; aNum < MAX && validType; aNum++)
	{
		cout << "Enter c for checking; s for savings; any other character to quit: ";
		cin >> acctType;
		acctType = tolower (acctType);

		if (acctType == 'c' || acctType == 's')
		{
			cout << "Enter customer name: ";
			cin >> custName;
			cout << "Enter account number: ";
			cin >> acctID;
			cout << "Enter account beginning balance: ";
			cin >> startBal;
		}

		switch (acctType)
		{
			case 'c':
				{
					char response;
					bool overdraftOk;

					cout << "Does this account have overdraft protection? ";
					cin >> response;
					overdraftOk = (tolower(response) == 'y')? true : false;
					Customer c (custName, acctID);
					acctsPtr[aNum] = new Checking (c, startBal, overdraftOk);
					doTransactions (acctsPtr[aNum]);
					acctsPtr[aNum]->adjustBalance();

					cout << "Balance after service charge (if any): " 
                                        << acctsPtr[aNum]->getBalance() << endl;
					break;
				}

			case 's':
				{
					double intRate;

					cout << "Enter current monthly interest rate: ";
					cin >> intRate;
					Customer c (custName, acctID);
					acctsPtr[aNum] = new Savings (c, startBal, intRate);
					doTransactions (acctsPtr[aNum]);
					acctsPtr[aNum]->adjustBalance();

					cout << "Balance after interest: " <<
                                          acctsPtr[aNum]->getBalance() << endl;
					break;
				}

			default:
				validType = false;
				aNum--;
				break;
		}
	}

	int totalAccts = aNum;
	cout << "\nAccounts: \n";

	for (int i = 0; i < totalAccts; i++)
	{
		acctsPtr[i]->view();
		cout << "\n";
	}

	for (int i = 0; i < totalAccts; i++)
		delete acctsPtr[i];

	system("pause");
	return 0;
}

void doTransactions (Account* aPtr)
{
	double depAmt, withdAmt;

	cout << "Enter total deposite: ";
	cin >> depAmt;
	aPtr->makeDeposit (depAmt);
	cout << "Balance after deposite: " << aPtr->getBalance() << endl;

	cout << "Enter total withdrawals: ";
	cin >> withdAmt;
	if (aPtr->makeWithdrawal (withdAmt))
		cout << "Balance after withdrawals: " 
                          << aPtr->getBalance() << endl;
	else
		cout << "Withdrawal not made -- balance too low\n" 
                      << " and no overdraft protection\n";
}
Last edited on
Without seeing what the constructors for Saving and Checking are doing, it's hard to tell. What are they doing with the Customer objects that are passed in? Which constructors are they calling on Account?

Do you really need the default constructor on Account? Is it ever used? Will you ever need to create an Account object without knowing the customer etc?
@MikeyBoy

The constructor calling on account class is this one.
1
2
3
4
5
6
Customer::	Customer(char n[], char id[]) 
{
	cout << "customer constructor is working" << endl;
	strcpy_s (name, n);
	strcpy_s (accountID, id);
}


Account class default constructor is not ever used. But the direction of this assignment wants to write that (initialize the balance (member variable of account)).

and here is my savings constructors
1
2
3
4
5
6
7
8
9
Savings::Savings()
{
	interestRate = 0;
}

Savings::Savings(Customer pCust, float Bal, float rate) : Account (Bal, pCust)
{
	interestRate = rate;
}


and also, checking constructors
1
2
3
4
5
6
7
8
9
Checking::Checking()
{
	overdraftProtection = false;
}

Checking::Checking(float Bal, Customer pCust, bool overP) : Account (Bal, pCust)
{
	overdraftProtection = overP;
}
Last edited on
 In function ‘int main()’:
211:61: error: no matching function for call to ‘Checking::Checking(Customer&, double&, bool&)’
211:61: note: candidates are:
160:1: note: Checking::Checking(float, Customer, bool)
155:1: note: Checking::Checking()
131:7: note: Checking::Checking(const Checking&)
Liar.

Your Customer assignment operator is wrong. It should be equivalent to `strcpy()'
However, you don't need to define it (as you didn't need a copy constructor or a destructor)

Also, #include <string> is for `std::string', you need #include <cstring>
Topic archived. No new replies allowed.