answersLogoWhite

0


Best Answer

I have not used friend concept in C#. Actually it may not exist in C#.

Based on OO principles, the friend feature in C++ violates the privacy or encaptualization of private data memeber.

Would you allow your friend (a friend class) to access your wallet or bank account (private member)?

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Virtual Function

A virtual function is a class method that is intended to be overridden by derived classes. This is achieved be preceding the function declaration with the keyword 'virtual'. If you fail to do this, derived classes can still override the method, but they will effectively hide the base class method. Moreover, when dealing with references or pointers to the base class of a derived class, calling the method will execute the base class method, not the override. By declaring the method virtual, you ensure expected behaviour. The derived class can still call the base class method explicitly, if required, but the base class will always call the override without the need to include runtime code to determine the actual type of the derived class. The virtual-table (or vtable) takes care of all that for you and costs very little in terms of memory and speed compared to using runtime information. In fact, the use of runtime information to access derived class behaviour is a clear indication of poor class design.

As a side-note, a pure-virtual function is one that is declared to be virtual but that MUST be overridden by derived classes. You achieve this by appending '=0' to the function declaration, before the semi-colon. When you do this, you are signifying that your class is intended to act as a common interface to all its derived classes, which will themselves provide the specific implementations of those methods. You also signify that your class is abstract; you cannot instantiate an abstract class other than by derivation. Derived classes that do not fully implement all the pure-virtual methods inherited from their base classes become abstract themselves. However, derived classes can inherit implementations defined in higher base classes, other than the base class that originally declared the method pure-virtual. Typically, abstract classes have none or few member variables, and little if any implementation; they are conceptual classes rather than concrete classes. By way of an example, circles and squares are types of shape. You cannot draw a shape without knowing its type, but since all shapes can be drawn, all shapes have a common interface: Draw(). Thus the Shape class can be declared abstract with a pure-virtual Draw() method. All classes derived from Shape (such as Circle and Square) must therefore implement the Draw() method. You can then create collections of shapes without regard to their actual type; calling the Draw() method will invoke the appropriate override. While this behaviour is really no different to the way in which a standard virtual function works, the difference is that you cannot instantiate a Shape class (other than by derivation) and the Shape class need not provide any implementation. Any implementation that is provided by the abstract class must be called explicitly, just as it would if it were declared virtual rather than pure-virtual.

Friend Function

A friend function is any function that is external to a class (including methods of other classes) that is declared to be a friend of the class. Entire classes can also be declared friends of other classes, if required. This is achieved by including the function prototype (exactly as it appears in the function's declaration) within the class declaration, preceded by the keyword 'friend'. Friend classes can be declared in their entirety by preceding the class name with the keywords 'friend class' in the class declaration.

Friends of a class are treated as extensions to the class interface and have the same access rights as the class itself. That is; unrestricted access to the private members of the class. As such they are highly privileged and should be used with a measure of caution. If misused, they can potentially undermine the encapsulation of your class. As a general rule, if the class interface is such that the public interface is sufficient for the external function to achieve its purpose, there is absolutely no need to declare the function a friend of the class. But if the function requires access to private and/or protected members of the class and exposing those members to public access would itself undermine the encapsulation of the class, then a friend function is the simplest solution. Unfortunately, there is no way to expose individual members of a class to a friend function -- it's all or nothing. However, friends are not subject to inheritance. That is, if Tom is derived from Dick and Harry is Dick's friend, Tom is not a friend of Harry unless Harry is specifically declared a friend of Tom.

Friend classes are often declared when two classes work closely together as a single unit. In such cases, one class may be more dominant than the other, but private access may still be required in both directions. Therefore the dominant class must be declared a friend of the other class (allowing the dominant class complete access to the other class) while specific methods of the other class can be declared friends of the dominant class, allowing only those specific methods access to the dominant class. This is the only way to achieve bi-directional access to private members. You cannot declare specific friend functions in both as the classes will not compile. At least one class must be late-bound to the other by declaring it a friend (in its entirety) of the other. However, in some cases it may be better to simply declare external functions to be friends of both instead. In this way, the classes can be treated separately and can be fully encapsulated, while the external functions can be used to treat them as a single unit wherever required.

Ultimately, friends should be used as minimally as possible in order to minimise coupling and dependency. With a well-designed interface, friends should rarely, if ever, be required, but the option is there for those cases where there is no other solution.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the virtual function and friend function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the merit and demerit of using friend function in c?

disadvantage of friend functions is that they require an extra lineof code when you want dynamic binding. To get the effect of a virtual friend,the friend function should call a hidden (usually protected:) virtual[20]member function.Read more: Demerits_of_friend_function


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


A pure virtual function is a virtual function that has?

A pure-virtual function is a function that must be overridden in derived classes. You simply add "=0" to the end of the function declaration. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


What is the difference between virtual function and function overriding?

Virtual Functions and Pure Virtual Functions are relevant in the context of class inheritance.Unlike Virtual Functions, Pure Virtual Functions do not require a body. This implies that when a base class defining such a function is inherited, the derived class must implement that function. Furthermore, the base class becomes abstract; meaning you cannot create an instance of the base class even if a body is implemented for the function. You are expected to derive from abstract classes; only the derived classes that implement all the inherited Pure Virtual functions can be instantiated.Here are some examples of Virtual and Pure Virtual function signatures:- Virtual Function: E.g. virtual void myFunction();- Pure Virtual Function: E.g. virtual void myFunction() = 0;


Can you have inline virtual functions in a class?

No, inlining is done at compile time whereas virtual functions are resolved at run time(late binding). So, virtual functions can't be inlined. Both properties are orthogonal.Inlining is a mere suggestion the compiler may ignore it if it is declared with virtual function.

Related questions

What are the merit and demerit of using friend function in c?

disadvantage of friend functions is that they require an extra lineof code when you want dynamic binding. To get the effect of a virtual friend,the friend function should call a hidden (usually protected:) virtual[20]member function.Read more: Demerits_of_friend_function


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


What are the merits and demerits of using function in c?

disadvantage of friend functions is that they require an extra lineof code when you want dynamic binding. To get the effect of a virtual friend,the friend function should call a hidden (usually protected:) virtual[20]member function.Read more: Demerits_of_friend_function


A pure virtual function is a virtual function that has?

A pure-virtual function is a function that must be overridden in derived classes. You simply add "=0" to the end of the function declaration. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


What is virtual function table?

A virtual function table is a table of pointers to functions.


What is the difference between virtual function and function overriding?

Virtual Functions and Pure Virtual Functions are relevant in the context of class inheritance.Unlike Virtual Functions, Pure Virtual Functions do not require a body. This implies that when a base class defining such a function is inherited, the derived class must implement that function. Furthermore, the base class becomes abstract; meaning you cannot create an instance of the base class even if a body is implemented for the function. You are expected to derive from abstract classes; only the derived classes that implement all the inherited Pure Virtual functions can be instantiated.Here are some examples of Virtual and Pure Virtual function signatures:- Virtual Function: E.g. virtual void myFunction();- Pure Virtual Function: E.g. virtual void myFunction() = 0;


Can you have inline virtual functions in a class?

No, inlining is done at compile time whereas virtual functions are resolved at run time(late binding). So, virtual functions can't be inlined. Both properties are orthogonal.Inlining is a mere suggestion the compiler may ignore it if it is declared with virtual function.


Which binding virtual function used?

Virtual functions are dynamically bound at runtime.


How do virtual functions differ from pure virtual functions?

Virtual functions is a function that can be overridden in inheriting class with the same signature (function name, parameters number, parameters types and return type);Pure virtual function is function that does not have implementation and if class has pure virtual function is called abstract. It is not possible to instantiate that class. Some other class must inherit it and define the body for it (implement). In other words class only have function prototype/declaration(signature) and no definition(implementation).


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


Explain the advantages and disadvantages of friend functions?

by: THE DJ AKwww.the-dj-ak.webs.comwww.thedjak.co.nrwww.thedjak.webs.comWhat is a Friend Function?A friend function is a special function in c++ which inspite of not being member fuctionof a class has privalage to access private and protected data of a class.A friend function is a non member function of a class, that is declared as a friend usingthe keyword "friend" inside the class. By declaring a function as a friend, all the accesspermissions are given to the function.A friend function is used for accessing the non-public members of a class.A class can allow non-member functions and other classes to access its ownprivate data, by making them friends. Thus, a friend function is an ordinaryfunction or a member of another class.Need for Friend Function:As discussed in the earlier sections on access specifiers, when a datais declared as private inside a class, then it is not accessible from outsidethe class. A function that is not a member or an external class will notbe able to access the private data. A programmer may have a situation wherehe or she would need to access private data from non-member functions andexternal classes. For handling such cases, the concept of Friend functionsis a useful tool.How to define and use Friend Function in C++:The friend function is written as any other normal function, exceptthe function declaration of these functions is preceded with the keywordfriend. The friend function must have the class to which it is declared asfriend passed to it in argument.Some important points to note while using friend functions in C++:* The keyword friend is placed only in the function declaration of the friendfunction and not in the function definition..* It is possible to declare a function as friend in any number of classes..* When a class is declared as a friend, the friend class has access to theprivate data of the class that made this a friend..* A friend function, even though it is not a member function, would have therights to access the private members of the class..* It is possible to declare the friend function as either private or public..* The function can be invoked without the use of an object. The friend functionhas its argument as objects, seen in example below.properties of friend function:1. if a function to be made friend of a class than it should be declared within bodyof the class priciding with keyword friend.2.freind function never breaks the security.3.it should not be defined in name of class nor scope resolution operator is used in it'sdefination even the keyword freind is also not used while defining friend function.4.when friend function is called nither name of object nor dot operator is used. howeverit may accept the object as argument who's value it want's to access.5.it doen't matter in which section of the class we have declared a freind function.Example to understand the friend function:#includeclass exforsys{private:int a,b;public:void test(){a=100;b=200;}friend int compute(exforsys e1)//Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passedto it};int compute(exforsys e1){//Friend Function Definition which has access to private datareturn int(e1.a+e2.b)-5;}main(){exforsys e;e.test();cout


What is meant by extension?

extensible means to enhance the program with new capabilities. In c++, extensibility can be achieved by using virtual function. now first we discuss what is virtual function in c++. " C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class." Through this u can be able to promote extensibility in your program by using Virtual function. Very simple concept.