Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

C++ Program to Demonstrate Friend Function

C++ Program to Demonstrate Friend Function

C++ Program to Demonstrate Friend Function


#include <iostream>
using namespace std;

class Sample
{
    int x;
public:
    Sample()
    {
        x = 10;
    }
    friend void show(Sample s);
};

void show(Sample s)
{
    cout << "Value of x = " << s.x << endl;
}

int main()
{
    Sample obj;
    show(obj);
    return 0;
}
  

πŸ“˜ Explanation:

This program demonstrates the concept of a friend function in C++. A friend function is not a member of the class, but it can access the private and protected data members of the class.

  • The class Sample contains a private variable x.
  • The constructor initializes x with the value 10.
  • The function show() is declared as a friend inside the class.
  • Because it is a friend, show() can access the private variable x.

Friend functions are mainly used when a function needs access to class data but does not logically belong to the class.

🧾 Sample Output:

Value of x = 10
  

πŸ”‘ Keywords:

C++ friend function, accessing private members, C++ OOP concepts, friend function example, C++ classes and objects

πŸ“Œ Hashtags:

#CPlusPlus #FriendFunction #OOP #CPPBasics #Programming #1printf

πŸ” Search Description:

Learn how friend functions work in C++ with a simple example that accesses private data members. Includes explanation, output, and dark-themed code.

Comments

πŸŒ™