C++ Inheritance

Inheritance is special and most useful feature of object oriented programming. Inheritance is provide accessibility of use one class properties into another one. This process are provide an opportunity to reuse codes.

//Class machine is {base and parent class}
class machine{
  //Data member
  //Member function
};
//Class programmer is {child and Derived class}
class programmer: public machine{
  //Data member
  //Member function
};

In this post understand all the terminology of inheritance.

Parent and Base class

parent class is a class those properties like data member and function are used by another class. there are also known as base class.

Derived and child class

Those class which are used other class properties it is called derived and child class. see the example and understand.

//Class computer is {base and parent class}
class computer{
  //data member
  int binary,process;
  float capacity;
  public:
    //member function
    int memory();
    void processor();
    void registers();
    void language();
    void bit_system();

};
//Class program is {child and Derived class}
class program: public computer{
  public:
    //Member function
    void assembly_code();
    void cpp_code();
    void java_code();
    int execute();
};
int main(){
  //make logic here
}

observe that there are two class computer and program. Assume that this is real. our pc is like a computer class, and program class like a programming. interesting computer class have on member function and data member. This are perform utility of system and perform real task.so like that memory and process related work. suppose we are make a new programming language. and this will execute in your system. so when we are create new programming language we will need to feature of computer class. like how many memory are sufficient to execute program and how can utilize processor and power in our program to make faster execution. in above example. we are try to relating this situation into c++ inheritance. understand computer is base class. And program is derived class in this example.

Type of Inheritance Access

Three way we are inherit the properties one class to another in C++. view accessibility areas.

Access Area Public Protected Private
Similar Class Yes Yes Yes
Inherit Class Yes Yes No
Outside Class Yes No No

Public inherit : when we inherit base class as public. then c++ are allowed to access public and protected member function in our derived class. this is provide less restriction but mostly this is used. In this situation public function are base class as public function of derived class. and protected function of base class also similar to protected function of derived class.

Protected inherit : when we inherit protected base class. then including public and protected member function of base class in protected section of relative derived class

//example to protected inherit of base class
#include<iostream>
using namespace std;
//Class teacher is {base and parent class}
class teacher{
  private:
    //member function
    void meeting(){
      cout<<"Meeting information"<<endl;
    }
  public:
    //member function
    void notice(){
      cout<<"Holidays are Declared"<<endl;
    }
  protected:
    //member function
    void result(){
      cout<<"Result Are pending"<<endl;
    } 

};
//Class student is {child and Derived class}
class student: protected teacher{
  public:
    void inform(){
      //execute teacher class public function.
      notice(); 
      //execute teacher class protected function.
      result();
        /*
      note: not allowed to access private 
      function of base class. like this.
      //meeting(); 
        */  
    }
};
int main(){
  //create object of base class student
  student p1;
  p1.inform();
}
Output
Holidays are Declared
Result Are pending

Private inherit : when we inherit private base class. then public and protected member function of base class are include in private section of relative derived class.

Type of Inheritance

Single Inheritance

what is single inheritance?. when derived class inherit the properties of single base class this process is called single inheritance. that process are reliable to add extra feature using base class.

class derived_class : access_mode base_class{
  //declared data member and member function
  //body of derived class
};
/*
  class : keyword
  derived_class: Name of derived class
  access_mode: Like public, private protected
  base_class: name of base class
*/

for example

/*
  basic example of single inheritance in c++
*/
#include <iostream>
using namespace std;
class Library
{
  public:
  void cs_book(){
    cout<<"20000 CSE books are available"<<endl;
  }
  void it_book(){
    cout<<"10000 IT books are available"<<endl;
  }
};
//inheriting of Library class publicly
class Student : public Library
{
  public:
  void my_book(){
    //access base class properties
    cs_book();
    it_book();
  }
};
int main(){
  //create object of student class
  Student obj;
  obj.my_book(); //call member function
}
Output
20000 CSE books are available
10000 IT books are available

Multilevel Inheritance

Derived class are access the properties of base class. and this derived class also inherit by another derived class. this situation are suitable for multilevel inheritance.

Multiple Inheritance

One derived class are access the properties of more than one base class. this process is called multiple inheritance.

Hierarchical Inheritance

A class may be inherit more than one class. that is called hierarchical inheritance.

Hybrid Inheritance

Hybrid inheritance is a combination of more than one type of inheritance.

Advantages of inheritance

re-usability

© 2019, regularcodes.com, All rights reserved