Classes
Contents
Friend
A class can have data members that are private. Normally these are not accessible outside the class. However we can declare that a function, class or a class method is a friend and then that function or class methods can access our private members.File: friend1.cpp
#include <iostream> using namespace std ; class point { private: int x1 ; int y1 ; //Does not matter under what access we place the declaration under friend void printPoint ( point p1 ) ; friend class B ; }; void printPoint ( point p1 ) { cout << p1.x1 << " " << p1.y1 << endl ; } class B { void anotherPrintFunction ( point p1 ) { cout << p1.x1 << endl ; } }; int main() { return 0 ; }The below example shows how we can declare a member function of another class as a friend. We have 2 classes "base" and "anotherClass" .
File: friend2.cpp
// C++ program to create a member function of another class // as a friend function #include <iostream> using namespace std; class base; // forward definition needed // another class in which function is declared class anotherClass { public: void memberFunction(base& obj); }; // base class for which friend is declared class base { private: int private_variable; protected: int protected_variable; public: base() { private_variable = 10; protected_variable = 99; } // friend function declaration friend void anotherClass::memberFunction(base&); }; // friend function definition void anotherClass::memberFunction(base& obj) { cout << "Private Variable: " << obj.private_variable << endl; cout << "Protected Variable: " << obj.protected_variable; } int main() { base baseObject1 ; anotherClass object2; object2.memberFunction( baseObject1 ); return 0; }Let's say we declare the order of the classes as:
anotherClass This has a method that relies on the base class: void memberFunction(base& obj); baseEither way we run into a problem. The solution is to use something called a forward declaration:
class base ;
This allows the compiler to compile the code under certain conditions. We are declaring that we are using a class but if the following code has to use the members of the class then the compile will not compile the code. In the above code the "memberFunction" is taking a reference to base and the code compiles. However if instead of defining the method:
void anotherClass::memberFunction(base& obj)
outside the class we instead define it in the class then it creates a problem. We have a forward declaration but the compiler does not know what the data members of the class base are and cannot verify the code in the "memberFunction" .
File: friend3.cpp
// C++ program to create a member function of another class // as a friend function #include <iostream> using namespace std; class base; // forward definition needed // another class in which function is declared class anotherClass { public: //void memberFunction(base obj); void memberFunction(base& obj) { cout << "Private Variable: " << obj.private_variable << endl; cout << "Protected Variable: " << obj.protected_variable; } }; // base class for which friend is declared class base { private: int private_variable; protected: int protected_variable; public: base() { private_variable = 10; protected_variable = 99; } // friend function declaration friend void anotherClass::memberFunction(base&); }; int main() { base baseObject1 ; anotherClass object2; object2.memberFunction( baseObject1 ); return 0; } $ g++ friend3.cpp friend3.cpp: In member function ‘void anotherClass::memberFunction(base&)’: friend3.cpp:15:49: error: invalid use of incomplete type ‘class base’ 15 | cout << "Private Variable: " << obj.private_variable | ^~~ friend3.cpp:6:7: note: forward declaration of ‘class base’ 6 | class base; // forward definition neededExercise:
Correct the compiler error in the below code by making class A1 a friend of the Point class.
File: friend_ex1.cpp
#include <iostream> using namespace std ; class point { private: int x1 ; int y1 ; }; class A1 { static void printPoint ( point p1 ) { cout << p1.x1 << " " << p1.y1 << endl ; } }; int main() { return 0 ; }