Inheritance

Submitted by: 
Language: 

Inheritance

In this part you will learn:
1. What is inheritance?
2. Why we need inheritance?
3. Syntax
4. Program
5. Output

Inheritance
In c++, the concept of inheritance is just like inheritance in family. As we all are child of our parents, we have inherited their many things from them. Similar is the case in programming. We can create inheritance between classes, we can have more than one class in a program, we can make one class the base class, also known as parent class and declare the other one as derived class, or simply child class.
There are different inheritance levels in class inheritance, i.e. the derived class can have access to either all the members of the base class or some of them. The level of inheritance is determined by the type of inheritance we are doing. It can be,
• Private
• Public
• Protected
All of these inheritances have different levels of access of the base class.
The following are the effects of different inheritances on members of base class with different access specifications on the derived classes.
• Private
a. Private becomes inaccessible for both (class and public)
b. Public becomes Private
c. Protected becomes Private
• Public
a. Private becomes inaccessible for both (class and public)
b. Public remains Public.
c. Protected remains Protected
• Protected
a. Private becomes inaccessible for both (class and public)
b. Public becomes Protected
c. Protected remains Protected
Note that there can be multiple levels of heritance, i.e. a class which is being derived from a base class itself can have a derived class of its own. Furthermore, there can be more than one derived classes from a base class.
Syntax
Following is the syntax of declaring one class the derived class of the other.
class Class_Name1
{
//class definition
}
class Class_Name2:Inheritance_type Class_name1
{
//function definition
}
In the above syntax we have declared a base class Class_Name1 the base class of class Class_Name2. In the second class after defining the name we have used colon, then will write the Inheritance_type which can be private, public or protected. Then we will write the name of the base class.
Program with Inheritance
In this example I have defined the member function of a Class.
Basic Step:

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string.h>
  4. using namespace std;

These two are the most common and basic lines of a c++ program. The first one iostream is used to grant us most of the basic functions of C++ like Input and Output functions. The second one conio.h means console input output, the black screen we see is the console. Using namespace std; provides us an ease of using all the library functions without specifying that this function belongs to standard library. Without including it, we have to write ‘std’ with all the built-in functions.
I have added another library file known as string.h, since we are using string(array of characters) so we have included the library of it before using it in the example given below.
Classes

  1. class person
  2. {
  3. public: //access specifier
  4. string name;
  5. string gender;
  6. int age;
  7.  
  8. person(string n, string g, int a) :name(n), gender(g), age(a){
  9. }
  10. };
  11.  
  12. class student:private person
  13. {
  14. public: //access specifier
  15. int roll_no;
  16. int no_of_subjects;
  17.  
  18. student(string n, string g, int a, int r, int no) :person(n,g,a),roll_no(r), no_of_subjects(no){
  19. }
  20. void print_student_info(){
  21. cout << endl << "Student information:" << endl << endl;
  22. cout << "Name: " << name << endl;
  23. cout << "Gender: " << gender << endl;
  24. cout << "Age: " << age << endl << endl;
  25. cout << "Roll number: " << roll_no << endl;
  26. cout << "No of subjects: " << no_of_subjects << endl;
  27. }
  28.  
  29. };
  30.  
  31. class employee:private person
  32. {
  33. public:
  34. int employee_id;
  35. int experience;
  36.  
  37. employee(string n, string g, int a, int e_id, int e) :person(n, g, a), employee_id(e_id), experience(e){
  38. }
  39. void print_employee_info(){
  40. cout << endl << endl << "Employee information:" << endl << endl;
  41. cout << "Name: " << name << endl;
  42. cout << "Gender: " << gender << endl;
  43. cout << "Age: " << age << endl << endl;
  44. cout << "Employee id: " << employee_id << endl;
  45. cout << "Experience: " << experience << endl;
  46. }
  47. };

The above given classes is an example of class inheritance. It is basically a part of school management system. As we know that for every person, that is either a student or an employee, he/she must have a name, gender and age. The rest of the information can be different. So instead of adding the common member variables in both class (employee and student) , we have defined a separate class namely “person” which has the common member variables in it. We have made the class employee and class student the derived class of the class person. Note that I have chosen the inheritance private, the reason behind this is that as we know that the class person itself is useless. There won’t be any objects of it. We have made class person private for the base class so that the members of class person which are public, will become private for the derived classes (employee and student) and they would not be available for the public.
For every scenario there will be different type of inheritance, you have to decide the type of inheritance keeping in mind the effects of it on the base class.
Moreover, I have first defined the constructor for the person class then reused that in the constructor of both the derived classes. So basically while initializing an object of student or employee. First the concerned parameters will be passed to the base class and the members of the base class will be assigned their values, then the remaining members of the derived class will be assigned with values.
A function for printing the data on console is made for both the derived classes (employee and student) which is simply printing the information on the console including the values of member variables of base class person.
Main

  1. int main()
  2. {
  3. student s1("John", "male", 19, 4343, 5);
  4. employee e1("Elia", "female", 35,1067, 8);
  5.  
  6. s1.print_student_info();
  7. e1.print_employee_info();
  8.  
  9. system("pause");
  10. }

In the main (driver) of the program, first I have initialized two objects s1 and e1 of student and employee respectively using overloaded constructor with hard coded values. Then their respective functions are being called using dot operator to print the information on the screen.
Output
picture viewer


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.