a they present a degree of freedom in the


A: They present a degree of freedom in the interface design options.

Member functions & friend functions are equally privileged (100% vested). The major difference is that a friend function is called such as f(x), whereas a member function is called like x.f(). Therefore the ability to select between member functions (x.f()) and friend functions (f(x)) let a designer to choose the syntax that is deemed most readable, which lowers maintenance costs.

The major disadvantage of friend functions is that they need an extra line of code while you wished dynamic binding. In order to get the effect of a virtual friend, the friend function must call a hidden (usually protected) virtual member function. It is called the Virtual Friend Function Idiom. For instance:

class Base {

public:

friend void f(Base& b);

... protected:

virtual void do_f();

...

};

inline void f(Base& b)

{

b.do_f();

}

class Derived : public Base {

public:

... protected:

virtual void do_f(); // "Override" the behavior of f(Base& b)

...

};

void userCode(Base& b)

{

f(b);

}

In userCode(Base&) , the statement f(b) will invoke b.do_f(), that is virtual. It means that Derived::do_f() will get control actually if b is a object of class Derived. Note down that Derived overrides behavior of the protected virtual member function do_f(); this does not have its own variation of the friend function, f(Base&).

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: a they present a degree of freedom in the
Reference No:- TGS0217769

Expected delivery within 24 Hours