Una función virtual puede ser privada ya que C++ tiene control de acceso, pero no control de visibilidad. Como se mencionó, las funciones virtuales pueden ser anuladas por la clase derivada, pero en todas las circunstancias solo se llamarán dentro de la clase base.
Ejemplo:
C++
// C++ program to demonstrate how a // virtual function can be private #include <iostream> class base { public: // default base constructor base() { std::cout << "base class constructor\n"; } // virtual base destructor // always use virtual base // destructors when you know you // will inherit this class virtual ~base() { std::cout << "base class destructor\n"; } // public method in base class void show() { std::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() { std::cout << "print() called on base class\n"; } }; class derived : public base { public: // default derived constructor derived() : base() { std::cout << "derived class constructor\n"; } // virtual derived destructor // always use virtual destructors // when inheriting from a // base class virtual ~derived() { std::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() { std::cout << "print() called on derived class\n"; } }; int main() { std::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; }
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
Explicación: ‘ b_ptr ‘ es un puntero de tipo Base y apunta a un objeto de clase Derivado. Cuando se llama al puntero ‘ ptr->print()’ , se ejecuta la función ‘ print()’ de Derived.
Este código funciona porque la clase base define una interfaz pública y la clase derivada la anula en su implementación, aunque la derivada tiene una función virtual privada.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA