Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

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

Popular Posts

πŸŒ™