Publicly inherit a base class but making some of public method as private

There are certain situation when we want to make some of the public base class functions as private in the derived class. Suppose both base and child class has getter and setter methods

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

i = 4 j = 2 k = 3

Here if we need to restrict the call of function “setBaseProperties” and “showBaseProperties” with Child class object “c”. This can be achieved without overriding the function as below:

We re-declare base class functions in derived class scope using “using” syntax. We do it in private section of derived class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

i = 1 j = 2 k = 3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.