Advanced-C++/C2/More-On-Inheritance/English
Title of script: Multiple and Hierarchical Inheritance in C++
Author: Ashwini Patil
Keywords: Multiple Inheritance, Hierarchical Inheritance, Video tutorial.
|
|
Slide 1 | Welcome to the spoken tutorial on Multiple and Hierarchical Inheritance in C++. |
Slide 2
|
In this tutorial we will learn,
Multiple Inheritance. Hierarchical Inheritnace. We will do this with the help of examples. |
Slide 3
|
To record this tutorial, I am using,
Ubuntu OS version 11.10 and g++ compiler v. 4.6.1 |
Open multiple.cpp
multiple.cpp |
In multiple inheritance derived class inherits from more than one base class.
Now, we will see an example on multiple inheritance. I have already typed the code on the editor. I will open it. Note that I have saved the file with the name multiple.cpp
|
In this program we will display the name, roll no, marks and average of the student.
Let me explain the code. | |
Highlight
#include<iostream> |
This is our headerfile as iostream |
Highlight
|
Here we are using std namespace |
Highlight
class student
|
Then we have class student.
This is the base class. |
Highlight
int roll_no; char name[20]; |
In this we have roll_no as integer variable and name as character variable.
These are declared as protected. |
Highlight
class exam_inherit |
Then we have another exam_inherit
This is also a base class. Hence we have two base class here student and exam_inherit. In this we have declared 3 private variables sub1, sub2, sub3 as protected. This is because protected variables can be accessed by derived class. |
Highlight
class grade:public student, public exam_inherit { |
Now here we have class grade which is the derived class.
This inherits the base classes. Class student and class exam_inherit.
|
Highlight
private: int avg; |
In this we have avg as integer variable declared as private. |
Highlight
public: void input(); void display(); void average(); int total; void input_exam(); void display_exam();
|
Then we have function
input() display() average() input_exam() and display_exam() as public functions. In this we have total as integer variable declared as public. |
Highlight
void grade::input() { cout<<"Enter Roll no:"; cin>>roll_no; cout<<"Enter Name:"; cin>>name; } |
Then we use input function to accept the roll_no and name of the student.
|
Highlight
void grade::display() { cout<<"Roll no is:"<<roll_no<<"\n"; cout<<"Name is:"<<name <<"\n"; } |
In display function we display the roll_no and name of the student. |
Highlight
void grade::input_exam() { cout<<"Enter marks of subject1:"; cin>>sub1; cout<<"Enter marks of subject2:"; cin>>sub2; cout<<"Enter marks of subject3:"; cin>>sub3; } |
Here we have function input_exam.
In this we accept the marks of three subjects as sub1, sub2 and sub3.
|
Highlight
void exam::display_exam() { total=sub1+sub2+sub3; cout<<"Total is :" <<total<<"\n"; } |
Then in display_exam function we calculate the total of three subjects.
And print the total. |
Highlight
void grade::average() { avg=total/3; cout<<"Average is :"<<avg<<"\n"; } |
And in function average we calculate the average.
|
Highlight
int main() |
This is our main function. |
Highlight
exam ex; |
In this we create an object of class grade which is the derived class as gd. |
Highlight
gd.input(); gd.input_exam(); gd.display(); gd.display_exam(); gd.average(); } |
Then we call all the above functions |
Highlight
return 0; |
This is the return statement. |
Now Let us execute the program | |
Open the terminal
Ctrl, Alt and T keys simultaneously |
Please open the terminal by pressing Ctrl, Alt and T keys simultaneously on your keyboard |
Type
g++ multiple.cpp -o mult To execute Type ./mult |
To compile the program type
g++ space multiple.cpp space -o space mult Press Enter
./mult Press Enter |
Type:
3 Pratham 67 78 84 |
Here we see,
Enter Roll no.: I will enter as 3 Enter Name: I will enter as Pratham Enter marks of subject1 I will enter as 67 Enter marks of subject2 I will enter as 78 Enter marks of subject3 I will enter as 84 |
Highlight
Output |
Here the output is displayed as
Roll no is: 3 Name is: Pratham Total is: 229 Average is: 76 This was multiple inheritance. Now we will see hierarchical inheritance. |
Come back to our program.
In hierarchical inheritance multiple derived classes inherits from one base class. | |
Open file hierarchical.cpp | Note that our filename is hierarchical.cpp
I will explain the code now. |
This is our header file as iostream. | |
Here we have used the std namespace | |
Highlight
protected: int roll_no; char name[20]; int sub1; int sub2; int sub3; int total; |
Then we have class 'student' which is the base class.
In this we have roll_no as integer variable. Sub1, sub2, sub3 and total as integer variables. Then name as character variable. These are declared protected. |
Highlight
class show:public student { |
Here we have another class 'show'.
This is the derived class. It inherits the properties of class student.
|
Highlight
public: void input(); void display(); }; |
In this we have two function as input and display.
These are declared as public functions. |
Highlight
void show::input() { cout<<"Enter Roll no:"; cin>>roll_no; cout<<"Enter Name:"; cin>>name; } |
In function input we accept the roll_no and name of the student. |
Highlight
void show::display() { cout<<"Roll no is:"<<roll_no<<"\n"; cout<<"Name is:"<<name <<"\n"; } |
In function display we display roll_no and name of the student. |
Highlight
class exam:public student { |
Then we have another derived class as class exam.
This also inherits class student. We can see that there are two derived class Class exam and class show. Both the classes inherits the class student.
|
Highlight
public: void input_exam(); void total_marks(); }; |
In class exam we have two functions as input_exam and total marks declared as public.
|
Highlight
void exam::input_exam() { cout<<"Enter marks of subject1:"; cin>>sub1; cout<<"Enter marks of subject2:"; cin>>sub2; cout<<"Enter marks of subject3:"; cin>>sub3; } |
Here we access the function input_exam
It accepts the marks of three subjects. sub1, sub2 and sub3
|
Highlight
void exam::total_marks() { total=sub1+sub2+sub3; cout<<"Total is :" <<total<<"\n"; } |
Then we have function total_marks.
It calculate the total of three subjects. And print the total. |
Highlight
int main() { student st; show sw; exam em; |
This is our main function.
In this we create objects of three classes as st, sw and ex. |
Highlight
sw.input(); em.input_exam(); sw.display(); em.total_marks(); |
Then we call all the above functions.
sw.input(); em.input_exam(); sw.display(); em.total_marks(); |
Highlight
return 0; } |
And this is our return statement. |
On the terminal | Now let us execute the program.
Come back to the terminal. Let me clear the prompt. |
Type
g++ hierarchical.cpp -o hier Type ./hier |
Let us compile.
Type: g++ space hierarchical.cpp space-o space hier Press Enter Type ./hier Press Enter |
On the terminal
Type: 4 Ashwini 87 67 97 |
Here What?it is displayed as
Enter Roll no.: I will enter as 4 Enter Name: I will enter as Ashwini Enter marks of subject1 I will enter as 87 Enter marks of subject2 I will enter as 67 Enter marks of subject3 I will enter as 97 |
On the terminal | The output is displayed as
Roll no is: 4 Name is: Ashwini Total is: 251 |
Switch back to the slides | This brings us to the end of this tutorial.
Come back to our slides. |
Slide 7
Summary |
Let us summarize
In this tutorial, we have seen, Multiple Inheritance. Hierarchical Inheritance. |
Slide 6
Assignment |
As an assignment
Create a class area and perimeter. Find the area and perimeter of a rectangle. |
Slide 8
About the Spoken Tutorial Project |
Watch the video available at the link shown
It summarizes the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Slide 9
Spoken Tutorial Workshops |
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, contact@spoken-tutorial.org |
Slide Number 10
|
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 Indiaabout:startpage More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay signing off
Thank You for joining. |