C++ Friend Virtual Function

Posted on

C++ Good friend Operate

In C++ a operate or a complete class could also be declared to be a buddy of one other class or operate. Good friend operate will also be used for operate overloading.

Good friend operate declaration can seem anyplace within the class. However a very good observe can be the place the category ends. An peculiar operate that isn’t the member operate of a category has no privilege to entry the non-public information members, however the buddy operate does have the aptitude to entry any non-public information members. The declaration of the buddy operate may be very easy. The key phrase buddy within the class prototype inside the category definition precedes it.

Instance to Show working of buddy Operate

Program Output:

friend function - C++ Friend  Virtual Function

Right here, buddy operate func() is asserted inside Distance class. So, the non-public information could be accessed from this operate. Although this instance provides you what concept in regards to the idea of buddy operate.

In C++, buddy means to offer permission to a category or operate. The non-member operate has to grant an entry to replace or entry the category.

The benefit of encapsulation and information hiding is {that a} non-member operate of the category can’t entry a member information of that class. Care have to be taken utilizing buddy operate as a result of it breaks the pure encapsulation, which is without doubt one of the benefits of object oriented programming. It’s best used within the operator overloading.


C++ Digital Operate

Polymorphism is refers back to the property by which objects belonging to completely different courses are ready to reply to the identical message however in several types. When there are C++ capabilities with identical title in each tremendous class in addition to sub class, digital capabilities provides programmer functionality to name member operate of various class by a identical operate name primarily based upon completely different context. This function is named polymorphism which is without doubt one of the vital options of OOP. Pointer can be one of many key elements of C++ language just like that of C. On this chapter we might be coping with digital capabilities and pointers of C++.


What’s Digital Operate?

A digital operate is a particular type of member operate that’s declared inside a base class and redefined by a derived class. The key phrase digital is used to create digital operate, precede the operate’s declaration within the base class. If a category features a digital operate and if it will get inherited, the digital class redefines a digital operate to go together with its personal want. In different phrases, a digital operate is a operate which will get override within the derived class and instructs the C++ compiler for executing late binding on that operate. Operate name is resolved at run time in late binding and so compiler determines the kind of object at run time.

Program for Digital Operate:

Instance: