How to access private member of a class using friend function in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Test {
	friend void set(Test &, int);
public:
	Test() { a = 0; }
	void print() const { cout << a << endl; }
private:
	int a;
};

void set(Test &t, int n)
{
	t.a = n;  
}

int main()
{
	Test T;

	T.print();
	set(T, 13); 
	T.print();

	return 0;
}


/*
run:

0
13

*/

 



answered Mar 25, 2018 by avibootz
edited Mar 25, 2018 by avibootz

Related questions

1 answer 184 views
1 answer 182 views
1 answer 156 views
1 answer 221 views
...