Inheritance in C++ - part II

Accessing the Base class members  - Protected members

Derived class inherits all members of base class except for constructors, destructors and assignment operators. We can call base class functions from an object of derived class. 

class shape
{
    int color;
public:
    void set_color(int color);
};
class circle:public shape
{
     int radius;
public:
     void init();
};

void circle::init()
{
     set_color(5);//call base class function. No error
     color =12;//syntax error
 }
In the example above, init() calls set_color() base class function. Which is OK. But the next line color=12 produces error. Why?

set_color() is a public function. Hence can be called by any other class. color, on the other hand color is a private member. (default access specifier is private)  Hence it can not be called by other classes, even the derived classes.

So do we have to make methods and data as public in order for them to be accessed by derived classes? That would violate encapsulation. It is always safe to make data members private. So what is the solution?

The solution is protected access specifier. A protected member is private to the class, but it can be accessed by direct and indirect derived classes. (If A is super class of B and C is derived class of B, then C is indirect derived class of A)

class shape
{
protected:
    int color;
public:
    void set_color(int color);
};
class circle:public shape
{
     int radius;
public:
     void init();
};

void circle::init()
{
     set_color(5); 
     color =12;//No error. color is a protected member
 }


Public, private and protected inheritance

We have seen in all previous examples that class derived:public base syntax is used for inheritance. public keyword specifies mode of inheritance is public, where public members of base class  remain public in derived class , protected members of base class remain protected and private members are inaccessible.

The other two modes of inheritance are private inheritance, achieved by using class derived:private base syntax or class derived: base syntax and protected inheritance which uses the syntax class derived:protected base syntax. In private inheritance, all public and protected members of base class become private members of derived class. In protected inheritance, public members of base class become, protected members in derived class. 


class shape
{
protected:
    int color;
public:
    void set_color(int color);
};
class circle:public shape
{
     int radius;
public:
     void init();
};
class rectangle:protected shape
{
  -----
};
class square:private shape
{
  ----
}
#include<iostream>
using namespace std;
int main()
{
   circle c1;
   c1.set_color(10);//OK. public inheritance
   rectangle r1;
   r1.set_color(12);//Syntax error. set_color is protected in 
   //rectangle. Can be accessed only from derived classes.
   square s1;
   s1.set_color(22);//syntax error. set_color is private method
   //of square class. 




Comments

Popular Posts