
A friend function is a non-member function that is allowed to access private and protected members of a class.
Why needed:
operator+) and when symmetrical access is required.To make a function friend, we declare it inside the class using keyword friend.
Key characteristics:
friend keyword inside class.)Important note: friendship is not inherited and not transitive.
A friend class is a class whose member functions can access private/protected members of another class.
Use case:
But using friend class too much can reduce encapsulation, so use carefully.
A static data member is shared by all objects of the class.
Key points:
Common use:
Static data members are typically defined outside the class.
A static member function belongs to the class, not to a particular object.
Rules:
this pointerClassName::func()Useful for utility operations related to the class.
Access the complete note and unlock all topic-wise content
It's free and takes just 5 seconds
Download this note as PDF at no cost
If any AD appears on download click please wait for 30sec till it gets completed and then close it, you will be redirected to pdf/ppt notes page.
A friend function is a non-member function that is allowed to access private and protected members of a class.
Why needed:
operator+) and when symmetrical access is required.To make a function friend, we declare it inside the class using keyword friend.
Key characteristics:
friend keyword inside class.)Important note: friendship is not inherited and not transitive.
A friend class is a class whose member functions can access private/protected members of another class.
Use case:
But using friend class too much can reduce encapsulation, so use carefully.
A static data member is shared by all objects of the class.
Key points:
Common use:
Static data members are typically defined outside the class.
A static member function belongs to the class, not to a particular object.
Rules:
this pointerClassName::func()Useful for utility operations related to the class.
Similarly, normal member function: called via object and can access non-static members. Static member function: called via class and accesses static members.
this is a special pointer available inside non-static member functions.
It points to the current object that invoked the member function.
So, inside a method, this helps identify which object’s data is being accessed.
Common uses:
this->x = x;return *this;) for method chainingType conversion means converting a value from one type to another.
Two broad groups:
Implicit conversion: automatic conversion by compiler (type promotion)
double x = 5;Explicit conversion: forced conversion (casting)
avg = total / (double)n;Explicit casting avoids unexpected integer division or precision loss.
C++ allows conversions involving classes, e.g.:
This helps objects interact naturally with other types.
Two common techniques:
operator int() kind of methodIn Sem 1, write concept + use cases; implementation details can be minimal.
this inside static function (not allowed).Object calls method → inside method 'this' points to that object → this->member accesses that object’s data
this.this points to current object; useful for this->x=x and chaining.Get instant access to notes, practice questions, and more benefits with our mobile app.
From this topic
A friend function can access private/protected members though it is not a class member. Characteristics (any two):
.)this pointerUsed in operator overloading and external helper functions.
Friend function is not a member but can access private data if declared friend; it is called like a normal function and has no this. Member function is part of the class, called using object (or class for static), and has this pointer (for non-static).
A friend function is a non-member function that can access a class’s private and protected members.
Key characteristics:
. (called like normal function)this pointerThus friend functions are useful in operator overloading and when external functions need special access.