Inheritance


Reusability--building new components by utilizing existing components- is yet another important aspect of OO paradigm. It is always good/ “productive” if we are able to reuse something that is already exists rather than creating the same all over again. This is achieve by creating new classes, reusing the properties of existing classes. It saves money , time etc. This mechanism of deriving a new class from existing/old class is called “inheritance”. The old class is known as “base” class, “super” class or “parent” class”. The new class is known as “sub” class “derived” class, or “child” class.

Types of Inheritance


1.Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4.Multilevel Inheritance
5.Hybrid Inheritance

Single Inheritance


1 base class and 1 derived class
           
                class B
                {
                int a;  	//private not inheritable
                public: //ready for inheritance
                int b;
                void get_ab(){
                  cout<<"4 in 1";
                }
                
                }
                class A:public B
                {
                  int y;
                  void put();
                }
            
        

Multiple Inheritance


more than one base class and 1 derived class
               
                    class B
                    {
                    int a;  	//private not inheritable
                    public: //ready for inheritance
                    int b;
                    void get_ab(){
                      cout<<"4 in 1";
                    }
                    
                    }
                    class A:public B
                    {
                      int y;
                      void put();
                    }
                class c:public B,public A
                {

               //content of class c


                }
            

Multilevel Inheritance


1 after one example 1 base -> 1 derived -> again 1 derived -> 1.....
                   
                        class B
                        {
                        int a;  	//private not inheritable
                        public: //ready for inheritance
                        int b;
                        void get_ab(){
                          cout<<"4 in 1";
                        }
                        
                        }
                        class A:public B
                        {
                          int y;
                          void put();
                        }
                        class c:public A
                        {
                          //content of class c
                        }
                    
                

Hybrid Inheritance


base class =B , A
derived from them are= c
derived from derived class= d
                       
                            class B
                            {
                            int a;  	//private not inheritable
                            public: //ready for inheritance
                            int b;
                            void get_ab(){
                              cout<<"4 in 1";
                            }
                            
                            }
                            class A:public B
                            {
                              int y;
                              void put();
                            }
                            class  c:public public B,public A
                            {
                              //CONTENT OF class c
                            }
                            class d:public c
                            {

                             // content of class d


                            }
                        
                    

Hierarchical Inheritance


base class =B
derived from them are= A ,c
derived from derived class= d
                           
                                class B
                                {
                                int a;  	//private not inheritable
                                public: //ready for inheritance
                                int b;
                                void get_ab(){
                                  cout<<"4 in 1";
                                }
                                
                                }
                                class A:public B
                                {
                                  int y;
                                  void put();
                                }
                                class c:public B
                                {

                                  //CONTENT OF class c
                                }
                                class d:public A,public c
                                
                                {

                                  //content of class d

                                }
                            
                        

Visiblity modes