Difference between Friend Function and Virtual Function in C++In this article, we will discuss the difference between the Friend function and the Virtual function. But before discussing their differences, we must know about the Friend function and the Virtual functions in C++. What is Friend Function?In the C++ programming language, a friend function is a function that is not part of a class but has access to its protected and private elements. It means that a friend function can access the private and protected members of a class as if it were a member function, even though it is not actually part of the class. Friend functions are declared using the friend keyword in the class declaration. Characteristics of the Friend function:There are several characteristics of the friend function. Some main characteristics of the friend function are as follows: 1. Not a Class Member In contrast to ordinary member functions, a friend function is declared inside the class using the friend keyword; it is not a part of the class itself. 2. Access to Private and Protected Members By overcoming the encapsulation barrier, a friend function can access members of the class it is friends with that are private or protected. 3. Syntax Although the friend function declaration is declared outside of the class scope, it is usually included in the class declaration. 4. Usage Practical in situations where a function has to access a class's private members without really belonging to it. What is the Virtual Function?Runtime polymorphism in C++ is accomplished through the use of virtual functions. It permits the invocation of the relevant method at runtime from a base class pointer or reference to derived class objects. Characteristics of the Virtual function:There are several characteristics of the virtual function. Some main characteristics of the virtual function are as follows: 1. Member of a Class A virtual function is a class member that is defined with the use of the virtual keyword. It is intended for derived classes to override. 2. Polymorphism and Inheritance In class hierarchies, virtual functions are utilized in which a base class specifies a function as virtual and the derived classes implement it independently. It makes polymorphic behavior possible. 3. Late Binding Late binding allows for flexibility in selecting the appropriate function implementation dependent on the type of object being referred to, as the actual function to be called is decided upon at runtime. 4. Syntax The virtual keyword is used to identify virtual functions, which are declared in the base class. After that, the override keyword is used by the derived class to override this function. 5. Usage It can be applied in situations where derived classes can modify their behavior and a base class must provide a generic interface. Example:Let's take an example to illustrate the Friend function and Virtual function in C++: Output: Using virtual function: Drawing a circle. Drawing a generic shape with 4 sides. Using the friend function: Number of sides: 0 Explanation:
Differences between virtual and friend functionsThere are several differences between the Virtual and Friend Functions. Some main differences between the virtual and friend function are as follows: 1. ScopeFriend Function: Although a friend function is declared in the class, but it is not a member of the class. It has access to the members of the class that it is friends with who are private and protected. Virtual Function: A virtual function is a class member function that is overridable in a derived class. Runtime polymorphism is accomplished with its help. 2. Access controlFriend Function: As a friend of a class, it has access to both private and protected members of that class. Virtual Function: This function can be declared in a class's protected, private, or public sections; however, the derived class's overriding function needs to have the same level of access. 3. InheritanceFriend Function: Derived classes do not inherit it. All that it is a stand-alone function to which the class's private members have access. Virtual Function: Derived classes can override the virtual function to give an implementation of their own. It is inherited by derived classes. 4. InvocationFriend Function: It is not invoked with a class object. Rather, it is invoked just like any other function. Virtual Function: This sort of function is called using a pointer or reference to a base class; during runtime, the type of the object it references or points to determines which function will be executed. 5. PolymorphismFriend Function: As it is not a part of the class hierarchy, it does not engage in polymorphism. Virtual function: It makes polymorphism possible by letting a program call the right function depending on the object's real type at runtime. Conclusion:In conclusion, friend functions are meant to give non-member functions access to a class's private and protected members, whereas virtual functions are meant to allow derived classes to provide their implementation to achieve polymorphism. Within C++ programming, they have several uses and are implemented in various contexts. |