+ Reply to Thread
Results 1 to 2 of 2

Thread: What is friend class in C++?

  1. #1

    What is friend class in C++?

    What is friend class in C++?

  2. #2
    Friend class is the class that can access private and protected member of the the class where it is declared to be friend.

    Below is the example:

    Code:
    class CModifiers
    {
    private:
    	int m_nPrivate ;
    
    protected:
    	int m_nProtected ;
    
    public:
    	int m_nPublic ;
    
    	CModifiers()
    	{
    		m_nPrivate		= 0 ;
    		m_nProtected	= 1 ;
    		m_nPublic		= 2 ;
    	}
    
    	friend class CFriendClass ;
    } ;
    
    class CFriendClass
    {
    private:
    	CModifiers objModifiers ;
    
    public:
    
    	CFriendClass()
    	{
    	}
    
    	int GetPrivate()
    	{
    		return objModifiers.m_nPrivate ; //friend class can access private data of CModifiers class!
    	}
    	
    	int GetProtected()
    	{
    		return objModifiers.m_nProtected ; //friend class can access protected data of CModifiers class!
    	}
    
    	int GetPublic()
    	{
    		return objModifiers.m_nPublic ;
    	}
    } ;
    
    
    void UseFriendClass()
    {
    	 CFriendClass objFriendClass ;
    
    	 int nPrivate	= objFriendClass.GetPrivate() ;
    	 int nProtected = objFriendClass.GetProtected() ;
    	 int nPublic	= objFriendClass.GetPublic() ;
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts