jeudi 20 mai 2021

Why does friend function not able to access private variables of a class?

#include <iostream>
using namespace std;
class base_A {
int a = 5, b = 6;
friend void derive::print(base_A);
};
class derive {
public:
    void print(base_A obj) {
        cout << obj.a << " " << obj.b;
    }
};
int main() {
    base_A obj1;
    derive obj2;
    obj2.print(obj1);
}

Though I've used friend key word I can't able to access the private variables. Anyone please help me on this.

Aucun commentaire:

Enregistrer un commentaire