Inheritance in C++ | Part 2

05 min read

Inheritance and Member Accessibility: 

  • A private member is accessible only to members of the class in which the private member is declared. They cannot be inherited. 
  • A private member of the base class can be accessed in the derived class through the member functions of the base class.
  • A protected member is accessible to members of its own class and to any of the members of the derived class.
  • If a class is expected to be used as a base class in future, then member which might be needed in the derived class should be declared as protected rather than private.
  • A public member is accessible to members of its own class, members of the derived class, and outside users of the class.
  • The private, protected and public sections may appear as many times as necessary in a class and in any order.
  • The visibility mode in the derivation of a new class can be public, private or protected. 
  • Constructors of the base class and the derived class are automatically invoked when the derived class is instantiated. If a base class has constructors with arguments, then their invocations must be explicitly specified in the derived class’s initialization section. However, no-argument (default) constructors need not be invoked explicitly. Remember that, constructors must be defined in the public section of a class (base and derived) otherwise, the compiler generates the error message: unable to access constructor. 

Consider the following declaration of the base class to illustrate public, private and protected inheritance.

class Person

private:
int contactNo; 
protected:
char gender;
public:
char name[20];   
void getContactNo( )   
{   
cout<< "Contact Number:"<<contactNo; 
} }

 

Public Inheritance: Consider the following declaration to illustrate the derivation of a new class Student from the base class Person in 'public' mode:

class Student : public Person

private:     
int id; 
public:         
void setVal (int i, char n[], int cNo, char gen)     
  {   
d = i; //Ok, its own private member   
strcpy (name,n);  
contactNo = cNo;  // Error: name is not accessible as it is private   
gender = gen;               
 } }

void main

Student st; 
strcpy (st.name,"Shams");   
st.contactNo = 420007;     // Error: contactNo is not accessible as it is private 
st.gender = 'M';         // Error: gender is not accessible as it is protected 
st.id = 0;                             // Error: id is not accessible as it is private       
cout<< sizeof(st);        // prints 29 
st.getContactNo( );

The member function, setVal( ) of the derived class Student can access name (public member) and gender (protected member) of Person class. Since the class Person is inherited as public by the derived class Student, the status of members gender, name, getContactNo( ) remain unchanged in the derived class.  

 


Private Inheritance: Consider the following declaration to illustrate the derivation of a new class Student from the base class Person in 'private' mode:

class Student : private Person
{
private:
int id;
public:
void setVal (int i, char n[], int cNo, char gen)
{
d = i;   //Ok, its own private member
strcpy (name,n);     
contactNo = cNo;  // Error: name is not accessible as it is private   
gender = gen;                 
} }

void main

Student st; 
strcpy (st.name,"Shams");   // Error, name is not accessible as it is private 
st.contactNo = 420007;     // Error: contactNo is not accessible as it is private 
st.gender = 'M';         // Error: gender is not accessible as it is private 
st.id = 0;                             // Error: id is not accessible as it is private   
st.getContactNo( );        //Error

The member function, setVal( ) of the derived class Student can access name (public member) and gender (protected member) of Person class. Since the class Person is inherited as private by the derived class Student, the status of members gender, name, getContactNo( ) changes as private in the derived class. 

 

Protected Inheritance: Consider the following declaration to illustrate the derivation of a new class Student from the base class Person in 'protected' mode:

class Student : protected Person

private:     
int id; 
public:         
void setVal (int i, char n[], int cNo, char gen)       
{   
d = i; //Ok, its own private member   
strcpy (name,n);    
contactNo = cNo;  // Error: name is not accessible as it is private   
gender = gen;                 
} }

void main

Student st; 
strcpy (st.name,"Shams");   // Error, name is not accessible as it is protected 
st.contactNo = 420007;     // Error: contactNo is not accessible as it is private 
st.gender = 'M';         // Error: gender is not accessible as it is protected 
st.id = 0;                             // Error: id is not accessible as it is private   
st.getContactNo( );        //Error
}

The member function, setVal( ) of the derived class Student can access name (public member) and gender (protected member) of Person class. Since the class Person is inherited as private by the derived class Student, the status of members gender, name, getContactNo( ) changes as private in the derived class. 

 

Member Functions Accessibility:

The different functions that can have access to the private and protected members could be any of the following: 

  1. A member function of a class 
  2. A member function of a derived class 
  3. A friend function of a class
  4. A member function of a friend class
  • A member of a class can have access to all the members of its own class, be it private, protected or public. 
  • The member functions of a derived class can directly access only the protected and public members; however, they can access the private members through the member functions of the base class.
  • The friend functions and member functions of a friend class can have direct access to both the private and protected members of a class.
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error