← 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 doublegetBalance(const Wallet& w);3) Define getBalance as a normal free function (NOT a member) that returnsw.balance. Do NOT write a main function.
Expected Output:
25.5 12.75
= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
ClassesFriend Functions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.