Online Tutorials & Training Materials | STechies.com
Register Login

Single Inheritance in C++ Programming

08 Feb 2018 12:58 pm || 0

What is Single Level Inheritance?

Single Level Inheritance is the mechanism of deriving a class from only one single base class. The operator used for inheritance is colon(:). 

Single level inheritance is of two types:

  1. Public Visibility
  2. Private Visibility

Single-Level-Inheritance

    Public Visibility:

    Sample Program:

    # include iostream.h
    # include conio.h
    class stu 	//base class
    {
    	private:
    	int id;
    	char name[20];
    	public:
    	void getstu()
    	{
    		cout “Enter stuid, name”;
    		cin  id name;
    	}
    	void putstu()
    	{
    		cout “Id=” id end1;
    		cout “Name=” name end1;
    	}
    }
    class phy : public stu	//‘:’ is inheritance operator, ‘public’ visibility mode
    {
    	float h, w;
    	public:
    	void getphy()
    	cout “Enter student height and weight”;
    	cin  h,w;
    	{
    		void putphy()
    		cout “Height=” h end1;
    		cout “Weight=” w end1;
    	}
    }
    void main()
    {
    phy p; 	// derived object
    clrscr;
    p.getstu();
    p.getphy();
    p.putstu();
    p.putphy();
    getch();
    }

    When a class is derived from another class, all the public members of the base class become members of the derived class and can be directly accessed by the derived class. Here, getstu() and putstu() are public members of the base class stu, and hence can be directly accessed by the derived class phy().

    Private Visibility:

    Sample Program:

    # include iostream.h
    # include conio.h
    class stu 	//base class
    {
    	private:
    	int id;
    	char name[20];
    	public:
    	void getstu()
    	{
    		cout “Enter stuid, name”;
    		cin id name;
    	}
    	void putstu()
    	{
    		cout “Id=” id end1;
    		cout “Name=” name end1;
    	}
    }
    class phy : private stu	//‘:’ is inheritance operator, ‘private’ visibility mode
    {
    	float h, w;
    	public:
    	void getphy()
    	cout “Enter student height and weight”;
    	cin h w;
    	{
    		void putphy()
    		cout “Height=” h end1;
    		cout “Weight=” w end1;
    	}
    }
    void main()
    {
    phy p; 	// derived object
    clrscr;
    p.getstu();	//inaccessible because private member
    p.getphy();	//inaccessible because private member
    p.putstu();	//inaccessible because private member
    p.putphy();	//inaccessible because private member
    getch();
    }

    When using the private visibility mode, the public members of the base class become private members of the derived class. Hence, the private members are not directly accessible by objects, but by the members of the derived class. In the above program, getstu() and putstu() becomes private members of class phy. They can only be accessed by putting command in class phy as follows:

    void getphy()
    {
    	getstu();
    
    and,
    
    void putphy()
    {
    	putstu();
    
    Only then can they be accessed in the main() function using the commands 
    
    void main()
    {
    phy p; 	// derived object
    clrscr;
    p.getphy();	//inaccessible because private member
    p.putphy();	//inaccessible because private member
    getch();
    }