Friend Functions & Static Members in C++

By Shakib Ansari | Date: Mon, Jun 2, 2025

C++ mein hum kabhi-kabhi chahte hain ki ek function ya class dusri class ke private ya protected members ko access kar sake. Aise mein aate hain friend functions aur friend classes.

Dusri taraf, jab kisi member ko class level par define karna hota hai (na ki har object ke liye), tab use kiya jaata hai static members.

Chaliye in dono concepts ko simple example ke saath samjhte hain.

1. Friend Function kya hota hai?

A friend function kisi class ka member nahi hota, lekin fir bhi us class ke private/protected data ko access kar sakta hai.

Syntax:

class ClassName {
    friend returnType functionName(arguments);
};

Example:

#include <iostream>
using namespace std;

class Box {
private:
    int length;
public:
    Box() { length = 0; }
    void setLength(int l) { length = l; }

    // friend function declaration
    friend void printLength(Box b);
};

// friend function definition
void printLength(Box b) {
    cout << "Length is: " << b.length << endl;
}

int main() {
    Box b;
    b.setLength(10);
    printLength(b);
    return 0;
}

Friend function printLength directly access kar raha hai length even though it's private.

2. Friend Class

Ek class dusri class ki friend ho sakti hai, jisse wo saare private/protected members ko access kar sake.

class B;

class A {
private:
    int data = 5;
    friend class B;  // B is a friend of A
};

class B {
public:
    void show(A a) {
        cout << "Accessing private data: " << a.data << endl;
    }
};

int main() {
    A obj;
    B b;
    b.show(obj);
    return 0;
}

3. Static Members in C++

C++ mein static data members aur static member functions class ke level par hote hain. Iska matlab ye hai ki unka ek hi copy banta hai, chahe kitne bhi objects ban jaayein.

Static Data Member

class Student {
public:
    static int count;
    Student() { count++; }
};
int Student::count = 0;  // static member initialization

int main() {
    Student s1, s2, s3;
    cout << "Total students: " << Student::count << endl;
    return 0;
}

Chahe jitne bhi objects banayein, count variable class-level par ek hi copy mein update hota hai.

Static Member Function

Static functions class ke static members ko access karte hain, aur unhe call karne ke liye object ki zarurat nahi hoti.

class Counter {
private:
    static int value;
public:
    static void increment() {
        value++;
    }
    static void show() {
        cout << "Current value: " << value << endl;
    }
};

int Counter::value = 0;

int main() {
    Counter::increment();
    Counter::increment();
    Counter::show();
    return 0;
}

4. Real-world Use Cases

  • Friend Functions: Jab ek external function ko internal private data access karne ki permission chahiye – jaise operator overloading.
  • Friend Classes: Jab do classes closely linked ho – jaise LinkedList and Node.
  • Static Data Members: Object counter, shared ID generator, global configuration.
  • Static Functions: Utility functions, shared functionality without object creation.

Important Points

  • Friend functions member function nahi hote.
  • Static members are shared by all objects.
  • Static functions only access static data.
  • Friend functions break encapsulation, so use them wisely.
About the Author

Hi, I'm Shakib Ansari, Founder and CEO of BeyondMan. I'm a highly adaptive developer who quickly learns new programming languages and delivers innovative solutions with passion and precision.

Shakib Ansari
Programming

Comments