Introduction:

There are three accessors in c++ , public, protected and private. They are declared and used in a class like below.

class Base {
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

Look at above above Base class data members where a is public, b is protected and c is private. Then what does it mean?

We know that these public ,protected and private are access specifier,  these keywords  control the access of data members of a class. The private data members are accessed by by member functions and friends of that class only. The protected members declared in the class are accessed only by member functions and friends of that class, and by member functions and friends of derived classes. The members declared in a public section of a class can be accessed by any other class that is aware this class.

Let us see below code.

#include<iostream>
using namespace std;

class Base
{
private:
  int a;
protected:
  int b;
public:
  int c;
    Base ()
  {
    a = 5;
    b = 6;
  };
  void print ();
};

class Derived:public Base
{
private:
  int d;
public:
    Derived ()
  {
    d = 7;
  };
  void print ();
};

int
main ()
{
  cout << "size of Base class is " << sizeof (Base) << endl;
  cout << "size of Derived class is " << sizeof (Derived) << endl;
  return 0;
}

void
Base::print ()
{
  cout << a;
}

void
Derived::print ()
{
  cout << b;
}

output:

size of Base class is 12
size of Derived class is 16

Note: The size of Derived class is the size of base class plus the size of Data members of Derived class.

Difference between private, public, and protected inheritance?

When a class is publicly derived from base class

  • The protected and public members of base class become protected and public members of derived class.

When a class is protectedly derived from base class

  • The protected and public members of base class become protected member of derived class.

When a class is privately derived from base class

  • The protected and public member of base class become private member of derived class.

Note:

Private members of base class are not accessible from within the derived class.

#include<iostream>
using namespace std;

class Base
{
private:
  int a;
  public:
    void print_base();
};

class Derived:public Base
{
private:
  int d;
public:
    void print_der();
};

void Base::print_base()
{
   cout<<a;
}

void Derived::print_der()
{
  cout<<a;  // will give error as it is private in Base
}
int
main ()
{
  return 0;
}

The private members are  just being added to the derived class member list. They can be accessed only through the inherited public or protected member functions of the base class.

#include<iostream>
using namespace std;

class Base
{
private:
  int a=5;
  public:
    void print_base();
};

class Derived:public Base
{
private:
  int d=6;
public:
    void print_der();
};

void Base::print_base()
{
   cout<<a<<endl;
}

void Derived::print_der()
{
  cout<<d<<endl;
}
int
main ()
{
  Derived d;
  // print private member of base class
  // by inherited function print_base
  d.print_base();
  return 0;
}

Now let us discuss about protected inheritance

Private inheritance Protected  inheritance
#include<iostream>
using namespace std;class Base
{
private:
int a;
protected:
int b;
public:
int c;
};
class Derived:private Base
{
private:
int d;
};class Der:public Derived
{
private:
int d;
};

Note: Here Derived class is privately derived ,in this case the private, protected and public data members become private for the Derived class ,hence They are not available to the Der class.

#include<iostream>
using namespace std;class Base{
private:
int a;
protected:
int b;
public:
int c;
};
class Derived:protected Base
{
private:
int d;
};class Der:public Derived
{
private:
int d;
};

Note: Here Derived class is protectedly  derived ,in this case the private, protected and public data members become protected for the Derived class ,hence They are available to the Der class.

 

 



Related Contents to follow