C++ Inheritance
Inheritance is special and most useful feature of a object oriented programming. Inheritance is provide a usability to access the property from one class to another. That is dependent upon the situation of in access specifiers. This process are provide an opportunity to reuse codes. let as view an example, how to inherit a one class properties to another class.
//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 Or 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 Or 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.
//Base class or Supper class
class A{
//data members
//member function
};
//B is derived class
class B:specifier A{
//data members
//member function
};
//C is derived class
class C:specifier A{
//data members
//member function
};
Hierarchical Inheritance

A class may be inherit more than one class. that is called hierarchical inheritance. there is syntax as follows.
//Base class or Supper class
class A{
//data members
//member function
};
//B is sub class
class B:specifier A{
//data members
//member function
};
//C is sub class
class C:specifier A{
//data members
//member function
};
//D is sub class
class D:specifier A{
//data members
//member function
};
Hybrid Inheritance

Hybrid inheritance is a combination of more than one type of inheritance.
//Base class or Supper class
class A{
//data members
//member function
};
//B is derived class
class B:specifier A{
//data members
//member function
};
//C is derived class
class C:specifier A{
//data members
//member function
};
//D is derived class
class D:specifier C,specifier D{
//data members
//member function
};
Advantages of inheritance
Inheritance is build a relation between two different class. so this reason one class properties are access by another class. this process are provide reliable environment to solve complex problem in very easier manner. and reuse the code are very effective manner. there is following main advantage of inheritance.
1) Fast development
2) Reuse code and functionality
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment