Changing Access in Inheritance in C++


/*The following program illustrates the access declaration; notice how it uses access
declarations to restore j, seti(), and geti() to public status.*/
#include <iostream.h>
#include <conio.h>
class base
{
      int i; // private to base
      public:
      int j, k;
      void seti(int x)
      {
            i = x;
      }
      int geti()
      {
            return i;
      }
};
// Inherit base as private.
class derived: private base
{
      public:
      /* The next three statements override
      base's inheritance as private and restore j,
      seti(), and geti() to public access. */
      base::j; // make j public again - but not k
      base::seti; // make seti() public
      base::geti; // make geti() public
      // base::i; // illegal, you cannot elevate access
      int a; // public
};
int main()
{
      clrscr();
      derived ob;
      //ob.i = 10; // illegal because i is private in derived
      ob.j = 60; // legal because j is made public in derived
      //ob.k = 30; // illegal because k is private in derived
      ob.a = 70; // legal because a is public in derived
      ob.seti(80);
      cout << ob.geti() << " " << ob.j << " " << ob.a;
      getch();
      return 0;
}
Custom Search