Open In App

Can Virtual Functions be Private in C++?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class.

Example 1: Calling Private Virtual Function from Base Class Pointer to Derived Class Instance

C++
// C++ program to demonstrate how a
// virtual function can be private
#include <iostream>

using namespace std;

class base {
public:
    // default base constructor
    base() { cout << "base class constructor\n"; }

    // virtual base destructor
    // always use virtual base
    // destructors when you know you
    // will inherit this class
    virtual ~base() { cout << "base class destructor\n"; }
    // public method in base class
    void show() { cout << "show() called on base class\n"; }

    // public virtual function in base class,
    // contents of this function are printed when called
    // with base class object when called with base class
    // pointer contents of derived class are printed on
    // screen
    virtual void print()
    {
        cout << "print() called on base class\n";
    }
};

class derived : public base {
public:
    // default derived constructor
    derived()
        : base()
    {
        cout << "derived class constructor\n";
    }
    // virtual derived destructor
    // always use virtual destructors
    // when inheriting from a
    // base class
    virtual ~derived()
    {
        cout << "derived class destructor\n";
    }

private:
    // private virtual function in derived class overwrites
    // base class virtual method contents of this function
    // are printed when called with base class pointer or
    // when called with derived class object
    virtual void print()
    {
        cout << "print() called on derived class\n";
    }
};

int main()
{
    cout << "printing with base class pointer\n";

    // construct base class pointer with derived class
    // memory
    base* b_ptr = new derived();

    // call base class show()
    b_ptr->show();

    // call virtual print in base class but it is overridden
    // in derived class also note that print() is private
    // member in derived class, still the contents of
    // derived class are printed this code works because
    // base class defines a public interface and drived
    // class overrides it in its implementation
    b_ptr->print();

    delete b_ptr;
}

Output
printing with base class pointer
base class constructor
derived class constructor
show() called on base class
print() called on derived class
derived class destructor
base class destructor

Explanation: 'b_ptr' is a pointer of Base type and points to a Derived class object. When pointer 'ptr->print()' is called, function 'print()' of Derived is executed.

This code works because the base class defines a public interface, and the derived class overrides it in its implementation even though the derived has a private virtual function.

Example 2: Calling Private Virtual Function from within Derived Class

C++
#include <iostream>

using namespace std;

// Base class declaration
class Base {
public:
    Base() {}
    virtual ~Base() {}

    // Public method that calls the private virtual function
    void show() { printMe(); }

private:
    // Private virtual function that can be overridden in
    // derived classes
    virtual void printMe()
    {
        cout << "Base calling printMe!!!\n";
    }
};

// Derived class inheriting from Base class
class Derived : public Base {
public:
    Derived(){}
    virtual ~Derived() {}

private:
    // Override the private virtual function in the derived
    // class
    virtual void printMe() override
    {
        cout << "Derived calling printMe!!!!\n";
    }
};

int main()
{
    Derived dd;
    dd.show();
    return 0;
}

Output
Derived calling printMe!!!!

Explanation

Base Class:

  • The Base class has a public method show which calls a private virtual function printMe.
  • The printMe function is declared as private and virtual, allowing it to be overridden in derived classes.

Derived Class:

  • The Derived class inherits from Base and overrides the printMe function.

main Function:

  • An instance of the Derived class is created.
  • The show method of the Derived instance is called.

We then call the printMe method, but since it is overridden in the Derived class, the derived class implementation is executed, printing "Derived calling printMe!!!!" even though it is private in the Base class.



Article Tags :
Practice Tags :

Similar Reads