Virtual Functions in C++ Explained With Simple Example

A virtual function is a member function which is defined in Base class and redefined by the derived class. “virtual” keyword must be added before the function declaration to define a function virtual. These functions gets inherited by Derived class from Base class and Derived class can choose to provide it’s own definition.
A pure virtual function is a virtual function which has no definition in Base class. Pure virtual function definition is used to ensure all derived class must override the base class function definition.

Normal syntax to define virtual function is as follows:

virtual int samplefun (int x, char c);
(more…)
Virtual Functions in C++ Explained With Simple Example Read More

Friend Function Explained With Simple Example

A Friend function is a function defined outside the class, but it has access to all private and protected members of the class. To declare a friend function, it’s prototype must be declared inside the class, preceding it with keyword “friend”. For eg:

class Demo
{
    private:
        int m;
        int y;

    public:
        friend int sum (Demo d);
        void print_val ();
        Demo (int m, int y);
};
(more…)
Friend Function Explained With Simple Example Read More