Friend Functions And Classes

Introduction

  • There are situations where you will have classes and functions that need to work very closely together.
  • Friend classes and friend functions can give your code access to the private details of another class.

Friend Functions

  • A friend function is a function that can access the private members of a class as though it were a member of that class.

Example: A function Reset() that because it's a friend with class Accumulator can access private variable m_nValue.

class Accumulator
{
private:
    int m_nValue;
public:
    Accumulator() { m_nValue = 0; }
    void Add(int nValue) { m_nValue += nValue; }
 
    // Make the Reset() function a friend of this class
    friend void Reset(Accumulator &cAccumulator);
};
 
// Reset() is now a friend of the Accumulator class
void Reset(Accumulator &cAccumulator)
{
    // And can access the private data of Accumulator objects
    cAccumulator.m_nValue = 0;
}

Example: We declare function isEqual() to be a friend of the Value class. It can access the private members of all Value objects.

class Value
{
private:
    int m_nValue;
public:
    Value(int nValue) { m_nValue = nValue; }
    friend boo IsEqual(const Value &cValue1, const Value &cValue2);
};
 
boolean IsEqual(const Value &cValue1, const Value &cValue2)
{
    return (cValue1.m_nValue == cValue2.m_nValue);
}

Friend classes

  • When you make a whole class friend with another class, it gives all of the members of the friend class access to the private members of the other class.

Example

class Storage
{
private:
    int m_nValue;
    double m_dValue;
public:
    Storage(int nValue, double dValue)
    {
        m_nValue = nValue;
        m_dValue = dValue;
    }
 
    // Make the Display class a friend of Storage
    friend class Display;
};
 
class Display
{
private:
    bool m_bDisplayIntFirst;
 
public:
    Display(bool bDisplayIntFirst) { m_bDisplayIntFirst = bDisplayIntFirst; }
 
    void DisplayItem(Storage &cStorage)
    {
        if (m_bDisplayIntFirst)
            std::cout << cStorage.m_nValue << " " << cStorage.m_dValue << std::endl;
        else // display double first
            std::cout << cStorage.m_dValue << " " << cStorage.m_nValue << std::endl;
    }
};

Example: Dogs can be friends with Cats!

#include <iostream>
using std::cout;
using std::endl;
 
class Cat
{
private:
    int age;
    int weight;
 
public:
 
    Cat()
    {
        age = 3;
        weight = 10;
    }
 
    inline int getAge()
    {
        return age;
 
    }
 
    inline int getWeight()
    {
        return weight;
    }
 
    friend class Dog; // now class Dog has access to the private members of Cat
};
 
class Dog
{
private:
    int age;
    int weight;
 
public:
 
    Dog()
    {
        age = 4;
        weight = 22;
    }
    inline int getAge()
    {
        return age;
 
    }
 
    inline int getWeight()
    {
        return weight;
    }
 
    void printCatDetails(Cat cat)
    {
        cout << "cat is " << cat.age << " and has weight " << cat.weight << endl;
    }
 
};
 
int main()
{
    Cat cat1;
    cout << "cat1 is " << cat1.getAge() << " and has weight " << cat1.getWeight() << endl;
 
    Dog dog1;
    cout << "dog1 is " << dog1.getAge() << " and has weight " << dog1.getWeight() << endl;
 
    dog1.printCatDetails(cat1);
 
    getchar();
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.