Prag Rao
Prag Rao

Reputation: 1501

Access friend function

Need Help in accessing the function friend declared in Class_D from main. Guidance to proceed.

    /* Main.cpp */

    #include <iostream>
    #include "types.h"
    #include "Class_A.h"
    #include "Class_C.h"

    int main()
    {
     cout << " Project started" << endl; 
     /* Creating Obj of Class A */
     A obj1; 
     /* Accessing Funcition of Class C through A */
     obj1.SetFuncA();         
     /* How to access GetFuncD(); from main*/
     cin.get();   
     return 0;   
    }

/* types.h */
#ifndef TYPES_H
#define TYPES_H

#include <iostream>
#include <stdlib.h>

#define ENAB_FRIEND_CLASS(x) friend class x

using namespace std;

typedef unsigned int U32;
typedef unsigned short  U16;
typedef unsigned char U8;


typedef int S32;
typedef short S16;
typedef char S8;


#endif

/* Class_A.h*/

#ifndef CLASS_A_H
#define CLASS_A_H


class D;

class A {
      private :
         int i;
         int j;     
      public :                  
         A();           /* Default Constructor */                                                                  
         ~A();          /* Default Destructor */                                                                                              
         void SetFuncA();
         int GetFuncA();
         friend int D::FGetFuncD(D &obj);
      protected:            
        };

#endif

/* Class_D.h */
#ifndef CLASS_D_H
#define CLASS_D_H

class D {
      private :
         int i;
         int j;     
      public :                  
         D();           /* Default Constructor */                                                                  
         ~D();          /* Default Destructor */                                                                                              
         void SetFuncD();
         int GetFuncD();
         void FGetFuncD(D &obj);
      protected:            
        };

void FGetFuncD(D &obj)
{
cout << "\n i " << obj.i << endl;     
cout << "\n i " << obj.j << endl;
}


#endif

/* Class_A.cpp */


#include "Class_A.h"
#include "types.h"
#include "Class_C.h"

A :: A()
{
cout << "Default CTOR Called\n" << endl;     
}

A :: ~A()
{
cout << "Default DTOR Called\n" << endl;     
}

void A::SetFuncA()
{
  int ret = 0;   
  cout << "\n SetFuncA " << endl;

  /* Creating Object of class C in Class A*/
  C Obj2;   

  /* Setting Private members */
  Obj2.SetFuncC();

  /* Calling Function of class C in Class A */
  ret = Obj2.GetFuncC();

  cout << " M = " << ret << endl;

  /* Dynamically Allocate memory for Class C */
  C *ptr = new C();

  /* Accessing private members of Class C */
  ptr->m =20;

  /* Accessing Public Function of Class C*/
  ret = ptr->GetFuncC();

  cout << " M = " << ret << endl;   

  /* Accessing Enum */
  ptr->m_bLEVEL = ptr->KN_LEVEL_1;

  cout << " ENUM = " << ptr->m_bLEVEL << endl;



}


int A::GetFuncA()
{
    cout << "\n GetFuncA " << endl;   
}


/* Class_D.cpp*/

#include "types.h"
#include "Class_D.h"

D :: D()
{
cout << "Default CTOR Called\n" << endl;     
}

D :: ~D()
{
cout << "Default DTOR Called\n" << endl;     
}

void D::SetFuncD()
{
  cout << "\n SetFuncD " << endl;
  i = 30;
}

int D::GetFuncD()
{
  cout << "\n GetFuncD " << endl;
  return i;
}

Please guide me the modification need to be done to access the private member of class_d with friend function.

I am trying to explore the feature of friend function. And i have added the Class_A.cpp/.h Class_D.cpp/.h and main.cpp.

Upvotes: 0

Views: 329

Answers (1)

mxdg
mxdg

Reputation: 314

A friend function is a function that is not a member of a class but has access to the class's private and protected members. So, your class D should change from:

 public :                  
     D();           /* Default Constructor */                                                                  
     ~D();          /* Default Destructor */                                                                                              
     void SetFuncD();
     int GetFuncD();
     void FGetFuncD(D &obj);

to:

 public :                  
     D();           /* Default Constructor */                                                                  
     ~D();          /* Default Destructor */                                                                                              
     void SetFuncD();
     int GetFuncD();
     friend void FGetFuncD(D &obj);  /* changed to friend function */

Here's pretty good documentation for it from Microsoft.

Then, in main, you can just call FGetFuncD without an instantiated object of D.

int main()
{
 D obj2;
 obj2.SetFuncD();
 int i_of_obj2 = FGetFuncD(obj2); /*using GetFuncD WITHOUT calling on a D object*/
 cout << "i_of_obj2: " << i_of_obj2 << endl;


 cin.get();   
 return 0;   
}

The output should be: i_of_obj2: 30

Upvotes: 1

Related Questions