Operator overloading and unique generation

I'd like for the code to have operator overloading and the ability to generate unique random account numbers but I do not know how to do that.

This is the original question:

Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors.

Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part a). This class inherits members to store the account number and the balance from the base class. A customer with a checking account typically receives interest, maintains a minimum balance, and pays service charges if the balance falls below the minimum balance. Add member variables to store this additional information. In addition to the operations inherited from the base class, this class should provide the following operations: set interest rate, retrieve interest rate, set minimum balance, retrieve minimum balance, set service charges, retrieve service charges, post interest, verify if the balance is less than the minimum balance, write a check, withdraw (override the method of the base class), and print account information. Add appropriate constructors.

Every bank offers a savings account. Derive the class savingsAccount from the class bankAccount (designed in part a). This class inherits members to store the account number and the balance from the base class. A customer with a savings account typically receives interest, makes deposits, and withdraws money. In addition to the operations inherited from the base class, this class should provide the following operations: set interest rate, retrieve interest rate, post interest, withdraw (override the method of the base class), and print account information. Add appropriate constructors.

Write a program to test your classes designed in parts b and c. Provide header and .cpp files.


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
bankAccount.h
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H

#include <iostream>
using namespace std;

class bankAccount {
   public:
    bankAccount();
    bankAccount(int, double);

    void setAccountNumber(int);
    int getAccountNum() const;

    void setBalance(double);
    double getBalance() const;

    void deposit(double);
    void withdraw(double);
    void print() const;

   private:
    int accountNum;
    double accountBalance;
};

#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
bankAccount.cpp
#include "bankAccount.h"

bankAccount::bankAccount() {
    accountNum = 0;
    accountBalance = 0;
};

bankAccount::bankAccount(int n, double b) {
    setAccountNumber(n);
    setBalance(b);
};

void bankAccount::setAccountNumber(int n) {
    accountNum = n;
};

int bankAccount::getAccountNum() const {
    return accountNum;
};

void bankAccount::setBalance(double b) {
    accountBalance = b;
}

double bankAccount::getBalance() const {
    return accountBalance;
};

void bankAccount::deposit(double amount) {
    accountBalance += amount;
};

void bankAccount::withdraw(double amount) {
    accountBalance -= amount;
};

void bankAccount::print() const {
    cout << "***Account Information***\n";
    cout << "Account Number: " << accountNum << endl;
    cout << "Account Balance: " << accountBalance << endl;
};
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
checkingAccount.h
#ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H

#include <iostream>

#include "bankAccount.h"
using namespace std;

class checkingAccount : public bankAccount {
   public:
    checkingAccount();
    checkingAccount(int, double, double, double, double);

    void setInterestRate(double);
    double getInterestRate();

    void setServiceCharge(double);
    double getServiceCharge();

    void setMinBalance(double);
    double getMinBalance();

    double postInterest();

    void withdraw(double);

    void print();

   private:
    double intRate;
    double minBalance;
    double serviceCharges;
};

#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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
checkingAccount.cpp
#include "checkingAccount.h"

checkingAccount::checkingAccount() : bankAccount() {
    intRate = 0;
    serviceCharges = 0;
    minBalance = 0;
}
checkingAccount::checkingAccount(int n, double b, double r, double sc, double mb) : bankAccount(n, b) {
    setInterestRate(r);
    setMinBalance(mb);
    setServiceCharge(sc);
}

void checkingAccount::setInterestRate(double i) {
    intRate = i;
    double newBalance = bankAccount::getBalance() + bankAccount::getBalance() * intRate;
    bankAccount::setBalance(newBalance);
}

double checkingAccount::getInterestRate() {
    return intRate;
}

void checkingAccount::setMinBalance(double mb) {
    minBalance = mb;
}

double checkingAccount::getMinBalance() {
    return minBalance;
}

void checkingAccount::setServiceCharge(double sc) {
    serviceCharges = sc;
}

double checkingAccount::getServiceCharge() {
    return serviceCharges;
}

double checkingAccount::postInterest() {
    double newBalance = bankAccount::getBalance() + bankAccount::getBalance() * intRate;
    bankAccount::setBalance(newBalance);
    return bankAccount::getBalance();
}

void checkingAccount::withdraw(double amount) {
    double newBalance;
    if (getBalance() > amount) {
        newBalance = getBalance() - amount;
        if (getBalance() < getMinBalance()) {
            newBalance = getBalance() - getServiceCharge();
        }
        setBalance(newBalance);
    } else
        cout << "Insufficient balance\n";
}

void checkingAccount::print() {
    cout << "***Checking Account***\n";
    cout << "Account Number: " << getAccountNum() << endl;
    cout << "Account Balance: " << getBalance() << endl;
    cout << "Minimum Balance: " << getMinBalance() << endl;
    cout << "Interest Rate: " << getInterestRate() << endl;
    cout << "Service Charge: " << getServiceCharge() << endl;
};
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
savingsAccount.h
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H

#include <iostream>

#include "bankAccount.h"

using namespace std;

class savingsAccount : public bankAccount {
   public:
    savingsAccount();
    savingsAccount(int, double, double);

    void setInterestRate(double);
    double getInterestRate();

    void postInterest();

    void withdraw(double);

    void print();

   private:
    double intRate;
};

#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
savingsAccount.cpp
#include "savingsAccount.h"

savingsAccount::savingsAccount() : bankAccount() {
    intRate = 0;
}

savingsAccount::savingsAccount(int n, double b, double r) : bankAccount(n, b) {
    setInterestRate(r);
}

void savingsAccount::setInterestRate(double r) {
    intRate = r;
}

double savingsAccount::getInterestRate() {
    return intRate;
}

void savingsAccount::postInterest() {
    double newBalance = bankAccount::getBalance() + bankAccount::getBalance() * intRate;
    bankAccount::setBalance(newBalance);
}

void savingsAccount::withdraw(double amount) {
    double newBalance;
    if (getBalance() > amount) {
        newBalance = getBalance() - amount;
        setBalance(newBalance);
    } else
        cout << "Insufficient balance\n";
}

void savingsAccount::print() {
    cout << "***Saving Account***\n";
    cout << "Account Number: " << getAccountNum() << endl;
    cout << "Account Balance: " << getBalance() << endl;
    cout << "Interest Rate: " << getInterestRate() << endl;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
main.cpp
#include <iostream>

#include "checkingAccount.h"
#include "savingsAccount.h"

using namespace std;

int main() {
    checkingAccount checkAccount(933, 1000, 0.09, 20, 300);
    checkAccount.withdraw(1200);
    checkAccount.print();
    cout << endl;
    savingsAccount savingAccount(933, 1000, 0.08);
    savingAccount.print();
    savingAccount.postInterest();
    savingAccount.print();
    return 0;
}

Sorry for the cluttered post
The account number is an int (although unsigned int would probably be better). To generate a random number, put

1
2
#include <random>
auto Rand {std::mt19937 {std::random_device {}()}};


at the beginning. Then in the function to generate the unique random number, have a static set to hold those that have already been generated. Generate a random number until the set insertion is true. This will give unique random numbers.

Eg. To generate 10 unique:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <set>
#include <random>

auto Rand {std::mt19937 {std::random_device {}()}};

unsigned int generate()
{
	static std::set<unsigned int> nos;
	unsigned int n {};

	while (nos.insert(n = Rand()).second == false);
	return n;
}

int main()
{
	for (int a = 0; a < 10; ++a)
		std::cout << generate() << ' ';
}


generate() could be a static member function of bankAccount.


Oh okay I see! Would there be a way to limit the number that the generation could go up or down to? If there is I'd like to be able to have the other values of the bank account randomly generated as well like the account balances and interest rates.

Here's what I have now, I think I did it correctly.

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
#include <iostream>
#include "checkingAccount.h"
#include "savingsAccount.h"
#include <set>
#include <random>

using namespace std;

auto Rand{ mt19937 {random_device {}()} };

unsigned int generate()
{
    static set<unsigned int> nos;
    unsigned int n{};

    while (nos.insert(n = Rand()).second == false);
    return n;
}

int main() {
    checkingAccount checkAccount(generate(), 1000, 0.09, 20, 300);
    checkAccount.withdraw(1200);
    checkAccount.print();
    cout << endl;
    savingsAccount savingAccount(generate(), 1000, 0.08);
    savingAccount.print();
    savingAccount.postInterest();
    savingAccount.print();
    return 0;
}
to limit the number that the generation could go up or down to


Yes. Use something like:

 
auto randNo {std::uniform_int_distribution<unsigned int> {minno, maxno}};


where minno and maxno are the required minimum number and maximum number.

Then use it like this:

 
auto rno = randNo(Rand);    // rno is a random number within the range of minno and maxno 


Note that you can have multiple of these defined with different names (instead of randNo) taking different min max ranges. Also the type (unsigned int) here can be int so you can get negative numbers as well if the min/max range includes negative numbers.
Topic archived. No new replies allowed.