Heredar públicamente una clase base pero hacer parte del método público como privado

Hay ciertas situaciones en las que queremos hacer que algunas de las funciones de la clase base pública sean privadas en la clase derivada. Supongamos que tanto la clase base como la secundaria tienen métodos getter y setter

// CPP program to demonstrate that all base
// class public functions become available
// in derived class if we use public inheritance.
#include <iostream>
using namespace std;
  
class Base {
    int i;
  
public:
    Base() {}
    void setBaseProperties(int i)
    {
        this->i = i;
    }
    void showBaseProperties()
    {
        std::cout << endl
                  << "i = " << i;
    }
    virtual ~Base() {}
};
  
class Child : public Base {
    int j;
    int k;
  
public:
    void setChildProperties(int i, int j, int k)
    {
        setBaseProperties(i);
        this->j = j;
        this->k = k;
    }
    void showChildProperties()
    {
        showBaseProperties();
        cout << " j = " << j << " k = " << k;
    }
};
  
int main()
{
    Child c;
    c.setChildProperties(1, 2, 3);
  
    // this exposed function is undesirable
    c.setBaseProperties(4); 
  
    c.showChildProperties();
    return 0;
}
Producción:

i = 4 j = 2 k = 3

Aquí, si necesitamos restringir la llamada de la función «setBaseProperties» y «showBaseProperties» con el objeto de clase Child «c». Esto se puede lograr sin anular la función de la siguiente manera:

Volvemos a declarar las funciones de la clase base en el ámbito de la clase derivada utilizando la sintaxis «using». Lo hacemos en la sección privada de la clase derivada.

// CPP program to demonstrate that some of
// base class public functions cane be restricted
// in derived class if we re-declare them with 
// "using" in private section of derived class
#include <iostream>
using namespace std;
  
class Base {
    int i;
  
public:
    Base() {}
    void setBaseProperties(int i)
    {
        this->i = i;
    }
    void showBaseProperties()
    {
        std::cout << endl
                  << "i = " << i;
    }
    virtual ~Base() {}
};
  
class Child : public Base {
    int j;
    int k;
  
    // Redeclaring scope of base class
    // functions in private section of
    // derived class.
    using Base::showBaseProperties;
    using Base::setBaseProperties;
  
public:
    void setChildProperties(int i, int j, int k)
    {
        setBaseProperties(i);
        this->j = j;
        this->k = k;
    }
    void showChildProperties()
    {
        showBaseProperties();
        cout << " j = " << j << " k = " << k;
    }
};
  
int main()
{
    Child c;
    c.setChildProperties(1, 2, 3);
  
    // if we uncomment this part of code, it causes 
    // compilation error as the function is private 
    // now
    // c.setBaseProperties(4); 
  
    c.showChildProperties();
    return 0;
}
Producción:

i = 1 j = 2 k = 3

Publicación traducida automáticamente

Artículo escrito por Rahul_Agrawal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *