Difference Between
versus

Friend Function vs. Friend Class: Know the Difference

Shumaila Saeed
By Shumaila Saeed || Published on February 10, 2024
A Friend Function can access private members of another class, while a Friend Class has all its functions as friends of another class, enabling access to its private members.
Friend Function vs. Friend Class

Key Differences

A Friend Function is a function declared with the friend keyword inside a class, allowing it to access the private and protected members of that class. A Friend Class, on the other hand, is a class whose all member functions are declared as friends of another class, giving them access to its private and protected members. The key difference lies in the scope of access granted - specific functions versus all functions of a class.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024
The concept of a Friend Function is utilized when specific external functions require access to the private members of a class without being a member of that class. In contrast, a Friend Class is used when an entire external class needs access to the private members of another class. The former is more selective and precise in granting access, while the latter is more extensive.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024
Friend Functions promote encapsulation by allowing controlled access to the private members of a class, typically for specific operations. Friend Classes, however, offer a broader level of access which could potentially lead to more extensive coupling between classes. Friend Functions are often used for operator overloading, whereas Friend Classes might be used in more complex relationships like class hierarchies.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024
Implementing a Friend Function involves declaring the function as a friend inside a class, and then defining it normally outside the class. For a Friend Class, the entire class is declared as a friend inside another class, meaning all its member functions can access the private data of the class in which it is a friend. This reflects a difference in implementation complexity and the nature of access.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024
In terms of design philosophy, Friend Functions are often seen as a way to extend functionality of a class without altering its interface, maintaining a tighter encapsulation. Friend Classes suggest a closer relationship between two classes, often used in designs where classes are mutually dependent or closely related in functionality.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024
ADVERTISEMENT

Comparison Chart

Access Scope

Specific functions access private members of another class.
All functions of the class access private members of another class.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Usage

For individual functions requiring access.
When an entire class needs access.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Encapsulation & Coupling

Promotes encapsulation, less coupling.
Potentially increases coupling between classes.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Implementation Complexity

Declared within a class, defined outside.
Entire class declared as a friend.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Design Philosophy

Extends functionality without altering class interface.
Implies closer, often mutual, dependency between classes.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024
ADVERTISEMENT

Friend Function and Friend Class Definitions

Friend Function

A Friend Function is a function declared with the friend keyword in a class's definition.
Friend void resetCounter(Counter& c); can modify private attributes of Counter.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Class

A Friend Class is a class with all its member functions able to access another class's private data.
Friend class Administrator; allows Administrator's methods to modify another class's private fields.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Function

A Friend Function is an auxiliary function that a class allows to access its non-public members.
By declaring friend void serialize(Object& obj);, serialize can access Object’s private members.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Class

A Friend Class is a class whose members have access to the private and protected members of another class.
If class Checker is a friend of class Data, Checker's methods can access Data's private attributes.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Function

A Friend Function is a non-member function that has access to the private and protected members of a class.
Void showData(MyClass obj) declared as a friend function can access private members of MyClass.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024
ADVERTISEMENT

Friend Class

A Friend Class is a class granted special access privileges by another class.
In declaring friend class Logger;, Logger can access all private parts of the class it’s a friend of.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Function

A Friend Function is a function not belonging to any class but able to access private data of a class.
The function friend int max(const Number& n); can access the private parts of the class Number.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Class

A Friend Class is a class associated with another, with permissions to access its non-public areas.
By making class Network a friend of class Computer, Network can access Computer’s private network settings.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Function

A Friend Function is an external function granted special access privileges by a class.
Friend void printCoordinates(Point p); allows this function to access Point's private data.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Friend Class

A Friend Class is an external class given comprehensive access to a class's private members.
Declaring friend class TestSuite; permits TestSuite to access and modify private parts for testing.
Shumaila Saeed
Shumaila Saeed
Jan 24, 2024

Repeatedly Asked Queries

What is a Friend Function?

A Friend Function is a non-member function with access to a class's private and protected members.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Why use a Friend Function?

For specific, external functions to access private data of a class, typically for operations like operator overloading.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Why use a Friend Class?

To allow an entire class access to another class's private data, often in tightly coupled class designs.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

How do you declare a Friend Class?

By declaring the entire class as a friend inside another class's definition.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

How is a Friend Function declared?

By using the friend keyword inside a class's definition.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Can a Friend Function be a member of another class?

No, Friend Functions are not members of any class.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Is a Friend Function part of the class interface?

No, it's external to the class but has special access privileges.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Are Friend Functions part of object-oriented design?

Yes, they are used in OOP for specific access needs, respecting encapsulation principles.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Is a Friend Function visible in the class's public interface?

No, it's declared inside the class but defined outside and not part of the public interface.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Can a Friend Class be a friend to multiple classes?

Yes, a Friend Class can be declared as a friend to more than one class.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Can a Friend Class access all private members of another class?

Yes, all its member functions can access the private and protected members.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Does a Friend Class increase coupling between classes?

Yes, it generally increases coupling due to broad access rights.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Can a Friend Function be a template?

Yes, template functions can also be declared as friend functions.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Can a Friend Function be declared in multiple classes?

Yes, a Friend Function can be a friend to more than one class.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

What is a Friend Class?

A Friend Class is a class all of whose member functions can access another class's private and protected members.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Does a Friend Class violate encapsulation?

It can potentially weaken encapsulation by broadening access to private data.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Do Friend Classes and Functions break abstraction?

They can, if not used judiciously, as they provide external entities access to a class's internals.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Are Friend Functions commonly used in C++?

Yes, they are a standard feature in C++, particularly for operator overloading.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Is there a limit to the number of Friend Classes a class can have?

No, there's no technical limit to the number of friend classes.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Can a Friend Function access all private members of a class?

Yes, a Friend Function can access all private and protected members of the class it is a friend to.
Shumaila Saeed
Shumaila Saeed
Feb 10, 2024

Share this page

Link for your blog / website
HTML
Link to share via messenger
About Author
Shumaila Saeed
Written by
Shumaila Saeed
Shumaila Saeed, an expert content creator with 6 years of experience, specializes in distilling complex topics into easily digestible comparisons, shining a light on the nuances that both inform and educate readers with clarity and accuracy.

Popular Comparisons

Trending Comparisons

Meshuga vs. MashuganaMeshuga vs. Mashugana
Hifza NasirHifza Nasir
June 24, 2025
Meshuga refers to someone being crazy in a lighthearted or humorous way, while Mashugana is a misinterpretation or less common variation of the word, both rooted in Yiddish.
Company vs. FactoryCompany vs. Factory
Hifza NasirHifza Nasir
May 8, 2026
A company is a business entity engaged in commercial, industrial, or professional activities, while a factory focuses on manufacturing goods through mass production.
Sneakers vs. Sports ShoesSneakers vs. Sports Shoes
Shumaila SaeedShumaila Saeed
May 7, 2026
Sneakers are casual, versatile footwear designed for everyday use, while sports shoes are specialized for specific athletic activities, focusing on performance and support.
Nightingale vs. MockingbirdNightingale vs. Mockingbird
Shumaila SaeedShumaila Saeed
April 21, 2026
Nightingales are known for their rich, melodious night songs, specializing in their own tunes, while mockingbirds mimic sounds from their surroundings, showcasing a wide repertoire of imitated songs.
Border vs. BoundaryBorder vs. Boundary
Hifza NasirHifza Nasir
December 11, 2025
A border is a line that marks the edges between countries, while a boundary delineates limits within countries, properties, or concepts.
Treble vs. BassTreble vs. Bass
Shumaila SaeedShumaila Saeed
July 14, 2025
Treble refers to higher frequencies in sound, producing a sharp, clear tone, while Bass encompasses lower frequencies, giving a deep, resonant sound.
Luminate vs. IlluminateLuminate vs. Illuminate
Hifza NasirHifza Nasir
May 4, 2026
Luminate and illuminate both refer to lighting up, but luminate is often used in a metaphorical or branding sense, while illuminate has a broader use in both physical and metaphorical lighting.
Royal Blue vs. Cobalt BlueRoyal Blue vs. Cobalt Blue
Dua FatimaDua Fatima
March 20, 2026
Royal Blue is a deep, vivid shade of blue with a hint of purple, symbolizing richness and sophistication, while Cobalt Blue is a slightly brighter blue with a hint of green, known for its striking vibrancy and clarity.
Duplicate vs. ReplicateDuplicate vs. Replicate
Dua FatimaDua Fatima
May 24, 2026
Duplicate means to create an exact copy, focusing on identicalness, while replicate emphasizes reproducing the essence or function, often in a scientific context.
Architype vs. ArchetypeArchitype vs. Archetype
Shumaila SaeedShumaila Saeed
December 21, 2025
An architype refers to a prototype or original model, often in design and architecture, while an archetype signifies a typical example or symbol universally recognized in psychology and literature.
Static Websites vs. Dynamic WebsitesStatic Websites vs. Dynamic Websites
Shumaila SaeedShumaila Saeed
April 19, 2026
Static websites deliver fixed content from server files as-is to each user, while dynamic websites generate content in real-time based on user interactions or other factors.
Respective vs. PerspectiveRespective vs. Perspective
Shumaila SaeedShumaila Saeed
May 17, 2026
Respective refers to relating to each person or thing mentioned separately, whereas perspective is one's viewpoint or angle on a subject.
Vinyl vs. PlasticVinyl vs. Plastic
Shumaila SaeedShumaila Saeed
May 11, 2026
Vinyl, a specific type of plastic made from polyvinyl chloride (PVC), is known for its durability and flexibility. Plastic, a broader category, encompasses a wide range of synthetic or semi-synthetic materials with various properties.
Chimpanzee vs. GorillaChimpanzee vs. Gorilla
Shumaila SaeedShumaila Saeed
June 22, 2025
Chimpanzees are smaller, more agile primates known for their complex social structures and use of tools, while gorillas are the largest primates, exhibiting remarkable strength and living in more stable family groups.
Depolarization vs. HyperpolarizationDepolarization vs. Hyperpolarization
Shumaila SaeedShumaila Saeed
April 28, 2026
Depolarization is the process where a neuron's membrane potential becomes less negative, moving towards action potential; hyperpolarization is when the membrane potential becomes more negative, moving away from action potential.
Humans vs. AliensHumans vs. Aliens
Hifza NasirHifza Nasir
July 9, 2025
Humans are Earth's native species, known for their cultural diversity and biological adaptability, whereas aliens, as hypothetical beings, are often imagined with advanced technology and diverse forms in science fiction.
Closet vs. DresserCloset vs. Dresser
Shumaila SaeedShumaila Saeed
December 5, 2025
A closet is a tall, enclosed storage space usually built into a room's walls for hanging or storing clothes, while a dresser is a piece of furniture with drawers for keeping clothes and often has a flat top for items.
Transient vs. HomelessTransient vs. Homeless
Hifza NasirHifza Nasir
July 11, 2025
Transient refers to a temporary state of being, often moving from place to place, while homeless denotes lacking a permanent, stable place to live.
Pastor vs. ApostlePastor vs. Apostle
Dua FatimaDua Fatima
June 12, 2025
Pastors shepherd local congregations, focusing on spiritual guidance and care, while Apostles are sent to establish churches and spread the gospel broadly.
Deed vs. AgreementDeed vs. Agreement
Shumaila SaeedShumaila Saeed
April 18, 2026
A deed is a legal instrument that conveys a right or interest in property, requiring signatures and witnesses, while an agreement is a broader term for a mutual understanding or contract between parties.
Vinyl Flooring vs. Hybrid FlooringVinyl Flooring vs. Hybrid Flooring
Dua FatimaDua Fatima
April 29, 2026
Vinyl flooring offers affordability and water resistance, ideal for high-moisture areas; hybrid flooring combines the best of laminate and vinyl, providing durability and a more realistic wood look.
Hemiacetal vs. HemiketalHemiacetal vs. Hemiketal
Shumaila SaeedShumaila Saeed
May 13, 2026
Hemiacetals form when an aldehyde reacts with an alcohol, featuring a carbon atom bonded to an OH and an OR group. Hemiketals result from a ketone and alcohol reaction, also with an OH and OR group but stemming from a ketone base.
Theoretical vs. HypotheticalTheoretical vs. Hypothetical
Hifza NasirHifza Nasir
May 2, 2026
Theoretical concepts are based on theories and principles, while hypothetical scenarios are imagined situations for the sake of argument.
Jelly vs. GelatinJelly vs. Gelatin
Hifza NasirHifza Nasir
June 30, 2025
Jelly is a fruit-based spread with a clear consistency, while gelatin is a flavorless protein used to thicken or gel foods.

Featured Comparisons

New Comparisons