"No Appropriate Default Constructor Available"

Pages: 12
Well, my instructor sent us a template because a lot of students were having trouble with the assignment. That was awesome of them. I think I am doing alright, but some things confuse me here and there.
Ok, if you can post the new code as is from your instructor, and any questions you have. You can post it over several topics if there is too much, but if it is only an outline it shouldn't be too bad.

HTH

This is the header 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
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

/*

    COP 3014
    Summer 2013
    Project 2

    bankingSystem.h

*/

#ifndef _BANKINGSYSTEM_H
#define _BANKINGSYSTEM_H

#include <iostream>
#include <string>
#include <cstdlib>

//Class Account holds an account ID, passcode,
//first name, last name, and balance.  All variables
//are kept private.  Access/changes are made through
//public set and get functions.

class Account
{

    public:

        void        SetAccountID(size_t accountID);
        void        SetPasscode(size_t passcode);
        void        SetFirstName(const std::string& firstName);
        void        SetLastName(const std::string& lastName);
        void        SetBalance(double balance);

        size_t      GetAccountID();
        size_t      GetPasscode();
        std::string GetFirstName();
        std::string GetLastName();
        double      GetBalance();

    private:

        size_t      accountID_;
        size_t      passcode_;
        std::string firstName_;
        std::string lastName_;
        double      balance_;

};

//Assume a max of 100 accounts.

const size_t MAX_NUM_ACCOUNTS = 100;

//Class BankingSystem uses an array of Account.
//You can create, delete, print, deposit, and withdraw.

class BankingSystem
{

    public:

                BankingSystem();

        void    CreateAccount();
        void    DeleteAccount();
        void    PrintAccounts();
        void    Deposit();
        void    Withdraw();

    private:

        Account accounts_[MAX_NUM_ACCOUNTS];
        size_t  current_num_accounts_;

        //Use these auxiliary functions if you want.
        //See bankingSystem.cpp for more info.

        size_t  FindAccount(size_t accountID);
        bool    MatchPasscode(size_t passcode, size_t index);

};

#endif 










This is the 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*

    COP 3014
    Summer 2013
    Project 2

    bankingSystem.cpp (partial code)

*/

#ifndef _BANKINGSYSTEM_CPP
#define _BANKINGSYSTEM_CPP

#include <iostream>
#include <string>
#include <cstdlib>
#include "bankingSystem.h"

//*** TO DO:  Add your Account function implementations here.
Account::Account() {

}

Account::~Account() {	//destructor - called when object is destroyed

}

//Default constructor just initializes
//current number of accounts to 0;

BankingSystem::BankingSystem() : current_num_accounts_(0)
{}

//Find an account by accountID.
//Return the array index if found, current_num_accounts_ otherwise.

size_t BankingSystem::FindAccount(size_t accountID)
{

    for (size_t i = 0; i < current_num_accounts_; ++i)
        if (accounts_[i].GetAccountID() == accountID)
            return i;

    return current_num_accounts_;

}

//Match the passcode at a given index with the given passcode.
//Return true for a match, false otherwise.

bool BankingSystem::MatchPasscode(size_t passcode, size_t index)
{

    return accounts_[index].GetPasscode() == passcode;

}

//Add an account.

void BankingSystem::CreateAccount()
{

    Account     account;
    size_t      accountID;
    size_t      passcode;
    std::string firstName;
    std::string lastName;
    double      balance;

    //Get the account ID and make sure it doesn't already exist.

    std::cout << "\nEnter the account ID:  ";
    std::cin >> accountID;

    if (FindAccount(accountID) != current_num_accounts_)
    {

        std::cout << "\nAccount already exists!\n";

        return;

    }

    //*** TO DO:  Get the remaining items (passcode, first name, last name, balance).
	void Account::getFirstName()
	{
		return firstName;

	}

	void Account::getLastName()
	{
		return lastName;

	}

	void Account::getAccountID()
	{
		return accountID;

	}

	void Account::getBalance()
	{
		return balance;

	}

	void Account::getPasscode()
	{
		return passcode;

	}




    //Set this info into your Account object.

    account.SetAccountID(accountID);
    account.SetPasscode(passcode);
    account.SetFirstName(firstName);
    account.SetLastName(lastName);
    account.SetBalance(balance);

    //Add the account to your array and increment the count of accounts.

    accounts_[current_num_accounts_] = account;
    ++current_num_accounts_;

    std::cout << "\nAccount added successfully!\n";

}

//Delete an account.

void BankingSystem::DeleteAccount()
{

    size_t index;
    size_t accountID;
    size_t passcode;

    //*** TO DO:  Get the account ID and verify it exists.

    //*** TO DO:  Get the passcode and verify it matches.

    //Remove the account.  First, shift everything over to the left.

    for (size_t i = index; i < current_num_accounts_ - 1; ++i)
        accounts_[i] = accounts_[i + 1];

    //Then, decrement the count of accounts.

    --current_num_accounts_;

    std::cout << "\nAccount erased!\n";
   
}

//*** TO DO:  Add your remaining BankingSystem function implementations here.



#endif



What I am confused about is in the cpp file. When I am suppose to GET the remaining functions...Should I use getLastName.... or should I use a more practical approach like how you mentioned above.

I still need to create a driver file on my own. I am pretty sure I can get that. Just create a switch statement and call everything.

The account implementations confuse me. I guess the formatting is just confusing me.
Last edited on
I am a bit dismayed that your instructor has given you this code.

As I said earlier, I don't think that any of the get or set functions are necessary.

CreateAccount is a class function, so it has direct access to the class variables -> no need for set functions.

BankingSystem::PrintAccounts() would call Account::PrintThisAccount() which is also a class function with direct access to it's member variables - so no need for any get functions.

Update functions (not trivial set functions) are only necessary when the data changes after the object has been created, this is the case with the Deposit & Withdraw functions which will have checking and validation code in them.

Get functions might only be needed when another class needs access to the data, even then there are ways around that - see the discussion about Send & Receive functions in the getters / setters Topic.

The .cpp file is confusing in it's layout, IMO there should be these files : BankingSystem.h, BankingSystem.cpp, Account.h and Account.cpp. The class declarations go in the relevant .h file, while the function definitions go in the relevant .cpp file. The header file is #include in which ever file needs it (that is, involves an object of that type), namely the .cpp files and in main.cpp.

Now you could get away with not having any get / set functions, it's not cheating: you just need to explain all your good reasons to your instructor. I would be honest and show s/he the advice you have received on this forum.

When I am suppose to GET the remaining functions...Should I use getLastName.... or should I use a more practical approach like what I am trying to do with cout and cin.


There is a slight difference in the meanings. A get function returns a value, which is a different thing to asking the user for input for the account number say. The code could go in a private function which is called by whatever function that needs it. Naming is important, try AskAccountNumber for example. Checking whether an account exists is another candidate for a private function, along with ones that ask for filenames, and asking for the Pass Code.

With the file processing, the BankingSystem class should have 2 functions : that load data from a file, in turn calling the Account constructor - populating the accounts array ; and one that save the data to a file, by iterating through the array, calling the Account::PrintAcountToFile() function say.

main() should only have code that creates the necessary objects, and calls their member functions with nothing else. A ShowMainMenu() function could go in the BankingSystem class, seeing that it only deals with this class and not multiple other classes. This is easy because it can call the necessary functions directly. More advanced would be to put it in a class of it's own, but that is a tiny bit trickier to call the functions, and maybe not worth it for this example. A justification for doing it any way would be that it makes it easier to extend later on.

Did I show this link for how to use a while loop & switch for a menu?

http://www.cplusplus.com/forum/beginner/104553/2/#msg564228


Any way see how you go with all that :+)
Topic archived. No new replies allowed.
Pages: 12