Friend Function Access

#include <iostream>
#include <cstdint>
using namespace std;

class Register8 {
private:
    uint8_t value;

    public:
        void setValue(uint8_t v) { value = v; }

            // declare a friend function
                friend void printRegister(const Register8& r);
                };

                // implement the friend function
                void printRegister(const Register8& r) {
                    cout << "Register value = " << static_cast<int>(r.value) << endl;
                    }

                    int main() {
                        Register8 r;
                            r.setValue(170);
                                printRegister(r);
                                    return 0;
                                    }
Upvote
Downvote
Loading...

Input

Expected Output

Register value = 170