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 in C++. But before discussing their differences, we must know about the friend function and virtual function and their characteristics. Virtual functions and friend functions have diverse uses and apply to different scenarios 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 Friend Function:
It has several characteristics of friend function in C++. Some main characteristics 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
It is used in situations where a function has to access a class's private members without really belonging to it.
What is 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 Virtual Function:
It has several characteristics of virtual function in C++. Some main characteristics 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 kind 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.
Main differences between virtual and friend functions:
There are several differences between the virtual and friend functions. Some main differences between the virtual and friend functions are as follows:
1. Scope
Friend Function: Although a friend function is declared in the class, 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. It helps to accomplish the runtime polymorphism.
2. Access control
Friend 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. Inheritance
Friend 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. Invocation
Friend Function: It is not invoked with a class object. Rather, it is invoked just like any other function.
Virtual Function: This type 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. Polymorphism
Friend Function: It does not engage in polymorphism because it is not a part of the class hierarchy.
Virtual function: It makes polymorphism possible by letting a program call the right function depending on the object's real type at runtime.
Example:
Let us take an example to demonstrate the usage of the friend function and virtual function in C++:
#include <iostream>
class Shape
{
public:
Shape(int sides) : numSides(sides) {}
virtual void draw() const
{
std::cout << "Drawing a generic shape with " << numSides << " sides." << std::endl;
}
int getNumSides() const
{
return numSides;
}
private:
int numSides;
};
class Circle: public Shape
{
public:
Circle() : Shape(0) {}
void draw() const override
{
std::cout << "Drawing a circle." << std::endl;
}
};
void printNumSides(const Shape& shape)
{
std::cout << "Number of sides: " << shape.getNumSides() << std::endl;
}
int main()
{
Circle circle;
Shape genericShape(4);
std::cout << "Using virtual function:" << std::endl;
circle.draw();
genericShape.draw();
std::cout << "\nUsing the friend function:" << std::endl;
printNumSides(circle);
return 0;
}
Output:
Using virtual function:
Drawing a circle.
Drawing a generic shape with 4 sides.
Using the friend function:
Number of sides: 0
Explanation:
- There is a virtual function called draw() and a private member called numSides in the Shape class.
- The draw () function is overridden by the circle class, which is inherited from shape.
- Friend function that has access to the Shape class's private member numSides is printNumSides.
- After that, we generate an instance of Shape and Circle in the main function. Next, we draw shapes using the virtual function and print the number of sides using the friend function.
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.