Knowee
Questions
Features
Study Tools

What is a friend function? What are the merits and demerit of using friend function. Explain with example.

Question

What is a friend function? What are the merits and demerit of using friend function. Explain with example.

🧐 Not the exact question you are looking for?Go ask a question

Solution

A friend function in C++ is a function that has the ability to access the private and protected members of a class, even though it is not a member of that class. This is achieved by declaring that function as a "friend" inside the class definition.

Here is a simple example:

class MyClass {
private:
    int myPrivateInt;

public:
    MyClass() : myPrivateInt(0) {}

    // Declare a friend function
    friend void accessPrivateData(MyClass &obj);
};

// Define the friend function
void accessPrivateData(MyClass &obj) {
    obj.myPrivateInt = 42;  // This is allowed because accessPrivateData is a friend of MyClass
}

In this example, accessPrivateData is a friend function of MyClass, so it can directly modify myPrivateInt, which is a private member of MyClass.

Merits of using friend functions:

  1. They provide an elegant way to access the private members of a class when it's necessary to do so.
  2. They can improve efficiency in some cases, because they avoid the need for public "getter" and "setter" functions.

Demerits of using friend functions:

  1. They can break encapsulation. One of the main principles of object-oriented programming is that the internal state of an object should be hidden from the outside world. Friend functions violate this principle by accessing an object's private data directly.
  2. They can make code harder to maintain. If a class has many friend functions, it can be difficult to keep track of what parts of the program are able to modify the object's state. This can lead to bugs and make the code harder to understand.

This problem has been solved

Similar Questions

Once an object is declared as a friend, _________________________.

What happens when we declare a function as a friend?Select one:a.We allow the function to have access to the public members of the classb.We allow the function to have access to the private and protected members of the classc.We do not allow the function to have access to the private and protected members of the classd.We allow the function to have access to the protected members but not the private members of the class

At all ages, friendship includes:Group of answer choicesa climate of agreement.a willingness to become friends with people who live very far from you.being very different from your friend to learn from each other.a lack of stability.

If the line:friend class A;appears in class B, and the line:friend class B;appears in class C, then:a.Class A is a friend of class C.b.Class A can access private variables of class B.c.Class C can call class A’s private member functions.d.Class B can access class A’s private variables.

In which stage of how friends develop do people think of themselves as friends and begin to establish their own private ways of interacting?Multiple Choicerole-limited interactionfriendly relationsnascent friendshipstabilized friendship

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.