friend class for each other : Friend « Class « C++






friend class for each other

   
#include <iostream>
#include <string.h>
using namespace std;
class Curly 
{
 public:
   Curly(char *msg) { strcpy(message, msg); };
   void show_message(void) { cout << message << endl; };
   friend class Moe;
   void show_moe(class Moe moe);
 private:
   char message[256];
 };


class Moe 
{
 public:
   Moe(char *msg) { strcpy(message, msg); };
   void show_message(void) { cout << message << endl; };
   friend class Curly;
   void show_curly(class Curly curly);
 private:
   char message[256];
 };


void Curly::show_moe(class Moe moe) { cout << moe.message << endl; };

void Moe::show_curly(class Curly curly) { cout << curly.message << endl; };

int main(void)
{
   class Moe moe("nuck...");
   class Curly curly("whoop...");
 
   moe.show_message();
   moe.show_curly(curly);
   curly.show_message();
   curly.show_moe(moe);
}
  
    
    
  








Related examples in the same category

1.Friend function DemoFriend function Demo
2.Make the inserter into a friend functionMake the inserter into a friend function
3.Define friend function for <<Define friend function for <<
4.Friends can access private members of a class.
5.Use friend function to access the non-public member variable