Difference Between Friend Function and Virtual Function in C++ Last Updated : 12 Mar, 2021 Comments Improve Suggest changes Like Article Like Report A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. Just likely, a friend function is a function that is declared outside the scope of a class. This function can be invoked like a normal function and include object/s as arguments. It is mostly used for overloading <<and>> for I/O. It can generally access any member of the class to which it is friend. Illustration: class GFG { private: { Public: { friend void check(); } void check(); Now coming onto the second function is a virtual function. So a virtual function is basically a member function of a class that is declared within the base class. In this, a virtual keyword is used to make member function of base class Virtual. It also supports polymorphism at both compile-time and run time. It also allows derived class to simply replace implementation that is provided or given by the base class. Illustration: class GFG { Public: Virtual return_type function_name(arguments) { ….. } }: class A { By far we are clear with discussing friend function and virtual function, now let us see the major differences between them even to grasp a good grip over it. Friend Function Virtual Function It is non-member functions that usually have private access to class representation. It is a base class function that can be overridden by a derived class. It is used to access private and protected classes. It is used to ensure that the correct function is called for an object no matter what expression is used to make a function class.It is declared outside the class scope. It is declared using the 'friend' keyword.It is declared within the base class and is usually redefined by a derived class. It is declared using a 'virtual' keyword.It is generally used to give non-member function access to hidden members of a class. It is generally required to tell the compiler to execute dynamic linkage of late binding on function. They support sharing information of class that was previously hidden, provides method of escaping data hiding restrictions of C++, can access members without inheriting class, etc. They support object-oriented programming, ensures that function is overridden, can be friend of other function, etc. It can access private members of the class even while not being a member of that class. It is used so that polymorphism can work. Comment More infoAdvertise with us Next Article Difference Between Friend Function and Virtual Function in C++ M madhurihammad Follow Improve Article Tags : Difference Between C++ Practice Tags : CPP Similar Reads Difference between virtual function and inline function in C++ Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class. Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments 2 min read Difference between Virtual function and Pure virtual function in C++ Virtual Function in C++ A virtual function is a member function which is declared within a base class and is re-defined(Overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execu 2 min read Difference between Static and Friend Function in C++ Static Function: It is basically a member function that can be called even when the object of the class is not initialized. These functions are associated with any object and are used to maintain a single copy of the class member function across different objects of the class. This function is denot 3 min read Difference Between Inline and Normal Function in C++ Inline Function is a function that is expanded inline by the compiler when it is invoked. During function call, a lot of overhead tasks are performed like saving registers, pushing arguments to the stack, and returning to the calling function. These overheads are time-consuming and inefficient for s 4 min read Difference between Inline and Macro in C++ 1. Inline : An inline function is a normal function that is defined by the inline keyword. An inline function is a short function that is expanded by the compiler. And its arguments are evaluated only once. An inline functions are the short length functions that are automatically made the inline fun 3 min read Like