Welcome to the spoken tutorial on Inheritance in C++. In this tutorial we will learn, Inheritance Types of inheritance We will do this with the help of examples. To record this tutorial, I am using Ubuntu OS version 11.10 g++ compiler v. 4.6.1 Let us start with an Introduction to Inheritance When one object acquires the property of another it is called as inheritance. It is the process of reusing the existing class without modifying them. Types of Inheritance Single level inheritance Multiple level inheritance Hierarchical Inheritance Multilevel inheritance Hybrid Inheritance First let us know about the base class and the derived class. The base class has its own properties and functionality. It is also called as parent class. It has the common qualities that all the objects can inherit. The derived class is the child class. Derived class inherits the properties and functionality of the base class. Let us see what is single level inheritance. In single level inheritance only one base class and one derived class is needed. Multiple inheritance In multiple inheritance, derived class inherits from more than one base class. Hierarchical Inheritance In Hierarchical Inheritance multiple derived classes inherits from one base class. Multilevel inheritance In Multilevel inheritance the subclass acts as the base class for other classes. and Hybrid inheritance In hybrid inheritance more than one form of inheritance is combined. Now let us see an example on single level inheritance. I have already typed the code on the editor. I will explain it. Note that our file name is exam_inherit.cpp The example involves to display the name, roll no and marks of the student. This is our header file as iostream Here we are using the std namespace Here we have class student. Then we have an integer variable roll_no and character array name, as private members of class student. Function input and function display are the public functions of class student. Here we are using the input function to accept the roll no. and name of the student. Then we have the display function to display the roll_no and name. Here we have another class as exam_inherit. This is the derived class. It inherits the function and data of class student. Then we have declared sub1, sub2, sub3 and total. As private variables of class exam_inherit. Here we have input_exam and display_exam as public functions. Here we close the class exam_inherit. Now we are using input_exam function to accept the marks of three subject. Here, we are using display_exam function to calculate the total of three subjects. Then we print a total. This is our main function. Here we create an object of class exam_inherit as ex Then we call all the functions using the object ex And this is our return statement. Let us execute the program Open the terminal Window by pressing Ctrl, Alt and T keys simultaneously, on your keyboard. To compile, type g++ (space) exam_inherit.cpp (space)-o (space) exam Press Enter. Type ./ (dot slash)exam Press Enter. Here it is displayed as, Enter Roll no.: I will give as 1 Enter Name: I will enter as Arya Enter marks of subject1 I will give as 60 subject 2 as 70 And subjec 3 as 80 The output is displayed as Roll no is: 1 Name is: Arya and, Total is: 210 Now we will see multilevel inheritance in the same example. I have already typed the code. Let’s switch back to our editor. Note that our filename is multilevel.cpp Here we have declared the variable total. As public variable of class exam_inherit. This is because the private members are not accessed by the derived class. Here we have another class as grade. This inherits the class exam_inherit. Class grade is the derived class And class exam_inherit is the base class for class grade. All the functions and data of class exam_inherit will be inherited to class grade. Here we have declared avg as private member of class grade. And function average as public function. Then we close the class. Here we use the average function to calculate the average. Then we print the average. Inside the main function we create an object of class grade as gd. Then we call all the functions using the object gd. gd.input() input_exam display display_exam and the average function. And this is our return statement. Now you can see that class grade is the derived class. And exam_inherit is the base class for class grade. And here class exam_inherit is the derived class. And class student is the base class for class exam_inherit. Let us execute the program. Come back to our terminal. To compile, type g++ (space) multilevel.cpp (space) -o (space) mul Press Enter Type
./mul Press Enter Here we see, Enter Roll no.: I will enter as 2 Enter Name: I will enter as Pratham Enter marks of subject1 I will give as 65 subject2 as 67 and, subject3 as 82 The output is displayed as: Roll no is: 2 Name is: Pratham Total is: 214 and, Average is: 71 This brings us to the end of this tutorial. Let us move back to our slides. Let us summarize In this tutorial, we learnt, Inheritance and, Types of inheritance. As an assignment Write a program to Create a class Shape. Then Create two functions of the class as Area and Perimeter. Then find the area and perimeter of various shapes like square, rectangle and circle. Watch the video available at the link shown below It summarizes the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it The Spoken Tutorial Project Team Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to, [email protected] Spoken Tutorial Project is a part of the Talk to a Teacher project It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at the link shown below This is Ashwini Patil from IIT Bombay signing off
Thank You for joining.