-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendFunction.cpp
More file actions
44 lines (31 loc) · 1.15 KB
/
Copy pathFriendFunction.cpp
File metadata and controls
44 lines (31 loc) · 1.15 KB
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
#include <iostream>
// Forward declaration of the BankAccount class
class BankAccount {
private:
double balance;
public:
BankAccount(double initialBalance) : balance(initialBalance) {}
void checkBalance() {
std::cout << "Account balance: $" << balance << std::endl;
}
friend class BankEmployee; // Declare BankEmployee as a friend class
};
class BankEmployee {
public:
// Friend function to access and modify the balance of a BankAccount
void accessAccountBalance(BankAccount& account, double newBalance);
};
// Define the accessAccountBalance function after the BankAccount class is fully declared
void BankEmployee::accessAccountBalance(BankAccount& account, double newBalance) {
account.balance = newBalance;
std::cout << "Bank employee updated account balance to $" << newBalance << std::endl;
}
int main() {
BankAccount myAccount(1000.0);
BankEmployee employee;
myAccount.checkBalance(); // Check initial balance
// The BankEmployee can modify the balance directly
employee.accessAccountBalance(myAccount, 1500.0);
myAccount.checkBalance(); // Check the updated balance
return 0;
}