Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 68

Friend Function Syntax in C++

In this lesson: Declare friend functions in C++ and overload operator<< with friend — syntax and examples. In Hindi & English.

Quick recap

A friend function is a non-member that a class trusts with access to its private data. This page is a practical syntax reference; for the full concept, when to use friends, and friend classes, see the main friend functions lesson.

Declaration syntax

Put friend before the prototype inside the class; define the body outside with no friend and no scope prefix:

C++
class Box {
    int w = 5;
    friend void print(const Box &b);   // declaration
};
void print(const Box &b) {             // definition (no Box::)
    cout << b.w;
}

Three friend forms

FormDeclaration
Free functionfriend void f(const T&);
Another class's methodfriend void C::m(const T&);
Whole classfriend class C;

Friend for operator<<

The classic real use: printing with cout << obj. Because the stream is the left operand, the operator must be a non-member — so it is made a friend to reach private data:

C++
#include <iostream>
using namespace std;
class Point {
    int x, y;
public:
    Point(int a, int b) : x(a), y(b) {}
    friend ostream& operator<<(ostream &os, const Point &p);
};
ostream& operator<<(ostream &os, const Point &p) {
    os << "(" << p.x << ", " << p.y << ")";
    return os;
}
int main() {
    Point p(3, 4);
    cout << p << "\n";   // (3, 4)
}
Output:
(3, 4)

See operator overloading for the wider topic.

Reading two objects

A friend can also read the privates of two objects at once — handy for comparisons or sums:

C++
class Money {
    int paise;
public:
    Money(int p) : paise(p) {}
    friend int total(const Money &a, const Money &b) {
        return a.paise + b.paise;   // both privates
    }
};

Common mistakes

  • Adding ClassName:: before a friend's definition — it is not a member.
  • Writing friend again on the outside definition (only in the declaration).
  • Making operator<< a member — the stream must be the left operand.
  • Forgetting to return the stream from operator<<.
🏋️ Practice

Give a Time class private hours and minutes, then write a friend operator<< that prints it as hh:mm. Test with cout << t;.

Summary

  • Declare with friend inside the class; define outside with no scope prefix.
  • Three forms: free function, another class's method, or a whole class.
  • Friends are the standard way to write operator<< for a class.
  • A friend can read the private data of two objects at once.
  • For the concept and design guidance, see the main friend functions lesson.

Frequently Asked Questions

How do you declare a friend function in C++?
Inside the class you write friend before the function's prototype, such as friend void print(const Box &b);. The function is then defined outside the class without the friend keyword and without the class scope, because it is not a member.
How is a friend function used for operator overloading in C++?
A friend is commonly used for operators like operator<< that must take the stream on the left and the object on the right, which a member function cannot do. Declaring the operator a friend lets it access the object's private data while still being a non-member.
Why use a friend function for operator<< instead of a member?
Because cout << obj has the stream as the left operand, the operator must be a non-member so its first parameter can be the stream. Making it a friend gives it access to the object's private members while keeping the natural stream << object syntax.
Can a friend function be a member of another class in C++?
Yes. A member function of one class can be declared a friend of another, using the form friend void OtherClass::method(...). This grants just that one method access, which is more restrictive and often cleaner than making the whole other class a friend.
Where is a friend function defined in C++?
The friend declaration goes inside the class, but the function body is defined outside the class like an ordinary non-member function, with no ClassName:: prefix. It can be defined inline inside the class body too, though it is still treated as a non-member.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicCopy Constructor Your progress: 68 / 81 (84%) · Lesson 69 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free