C++ Inheritance

When a class is derived from a base class, the derived class inherits all the characteristics of base class and can add new features as refinements and improvements. This is called inheritance.

inheritance-intro

Figure: relation between base and derived class

Inheritance doesn’t work in reverse order i.e. features in derived class can not be accessed by the base class object.

The following figure depicts the implemented version of base and derived class relationship.

inheritance-example

Figure: Class car derived from vehicle

Points to be considered with access specifiers:

  1. A private member is accessible only to member of the calss in which they are declared. They are not visible in derived class or from outside of class where they are declared except friend functions/ classes.
  2. A private member of the base class can be accessed in the derived class through public member functions of the base class.
  3. A protected member is accessible to member of its own and any of the members in a derived class or the friend of base or friend of derived class.
  4. The members that might be used in derived class should be declared as protected rather than private.
  5. A public member is accessible to members of its own class, member of derived class and even outside the class.

Derived class declarations:

class derived_class:[access_to_base] base_class {
        //members of derived class };
}

Derive class declarations:

  • Public declarations:
class derived : private base {
    //members
}
  • Protected derivations
class derived: protected base { 
    //members 
}
  • Private derivation
class derived : private base { 
    //members 
}

The following table illustrates the visibility of the derived class in reference to specifier in base class.

visibility

The following example makes the concept more clearer.

#include<iostream>
using namespace std;
class A
{
private:
    int pvtdataA;
protected:
    int prodataA;
public:
    int pubdataA;
};
class B: public A // publicly derived
{
public:
    void func()
    {
        int a;
        a=pvtdataA; //error: not accessible
        a=prodataA; //ok a=pubdataA; //ok
    }
};
class C: public A // protectedly derived
{
public:
    void func()
    {
        int a;
        a=pvtdataA; //error: not accessible
        a=prodataA; //ok
        a=pubdataA; //ok
    }
};
class D: public A // privately derived
{
public:
    void func()
    {
        int a;
        a=pvtdataA; //error: not accessible
        a=prodataA; //ok
        a=pubdataA; //ok
    }
};
int main()
{
    int num;
    B objB;
    num= objB.pvtdataA; //error: not accessible
    num= objB.prodataA; //error: not accessible
    num= objB.pubdataA; //ok: pubdataA is public to B
    C objC;
    num= objC.pvtdataA; //error: not accessible
    num= objC.prodataA; //error: not accessible
    num= objC.pubdataA; //error: pubdataA is protected to C
    D objD;
    num= objD.pvtdataA; //error: not accessible
    num= objD.prodataA; //error: not accessible
    num= objD.pubdataA; //error: pubdataA is private to D
}

Here is another more practical example.

//example
//example of inheritance
#include<iostream>
using namespace std;
class person //base class
{
protected:
    int age;
    char name[20];
public:
    void readAge()
    {
        cout<<"Enter Age: ";
        cin>>age;
    }
    void readName(void)
    {
        cout<<"\nEnter Name: ";
        cin>>name;
    }
    void printPerInformation(void)
    {
        cout<<"Name - "<<name;
        cout<<"\nAge - "<<age;
    }
}; //derived class inherits base class
class student:public person
{
private:
    int Sno;
    int percentage;
public:
    void readSno()
    {
        cout<<"Enter Sno.: ";
        cin>>Sno;
    }
    void readpercentage()
    {
        cout<<"Enter percentage: ";
        cin>>percentage;
    }
    void printStuInformation()
    {
        cout<<"\nName - "<<name;
        cout<<"\nAge - "<<age;
        cout<<"\nS.no - "<<Sno<<endl;
        cout<<"Percentage- "<<percentage<<endl;
        cout<<"conclusion"<<endl;
        if(percentage>=80)
            cout<<"\nThe student is Outstanding"<<endl;
        else if(percentage>=70)
            cout<<"The student is Medium"<<endl;
        else cout<<"The student is Poor"<<endl;
    }
};
int main()
{
    student st;
    st.readName();
    st.readAge();
    st.readSno();
    st.readpercentage();
    st.printStuInformation();
    return 0;
}
 

The output of the program is like:

op-2

C++ Data Conversion
C++ Overriding