9. Friend Function Access

Your task is to define a class named Register8 with:

  • private variable uint8_t value.
  • public method setValue(uint8_t v) to set the value.
  • friend function printRegister(const Register8& r) that directly prints the private value.
     

The program will:

  1. Create a Register8 object.
  2. Call setValue(170) (decimal 170 = 0xAA).
  3. Call printRegister(r).
     

Example

Output:

Register value = 170

 

Why this output?

Because the friend function is allowed to peek into the private value and print it.

 

Question Significance

This introduces friend functions, which can access private members of a class — useful in debugging or helper utilities.

Loading...

Input

Expected Output

Register value = 170