โ Back to Classes
Question 176
Friend Function to Read a Private Field
Friend Function to Read a Private Field
A friend function is a non-member function that is allowed to access a class's private members. Create a class Wallet with a private double balance set by its constructor. Requirements: 1) Constructor Wallet(double b) that stores b in balance. 2) Declare a friend function inside the class: friend double getBalance(const Wallet& w); 3) Define getBalance as a normal free function (NOT a member) that returns w.balance. Do NOT write a main function.
Expected Output:
25.5 12.75
Topics:
ClassesFriend Functions
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.