Advanced-C++/C2/Inheritance/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Objects and Classes in C++

Author: Ashwini Patil

Keywords: Inheritance, Single level Inheritance, Multilevel Inheritance, Multiple Inheritance, Video tutorial.


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Inheritance in C++.
Slide 2


In this tutorial we will learn,

Inheritance.

Types of inheritance.

We will do this with the help of examples.

Slide 3


To record this tutorial, I am using

Ubuntu OS version 11.10

g++ compiler v. 4.6.1

Slide 4 Let us start with an ontroduction 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.

# Slide 5-\


Types of Inheritance

Single level inheritance

Multiple level inheritance

Hierarchical Inheritance

Multilevel inheritance

Hybrid Inheritance

Slide 6 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.

Slide 7


In single level inheritance only one base class and one derived class is needed.

Slide 8



In multiple inheritance derived class inherits from more than one base class.

Slide 9 In Hierarchical Inheritance multiple derived classes inherits from one base
Slide 10





In Multilevel inheritance the subclass acts as the base class for other classes.

Slide 11 In hybrid inheritance more than one form of inheritance is combined.
Open exam_inherit.cpp


Point the cursor

exam_inherit.cpp

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 I have saved the file with the name exam_inherit.cpp



The example involves to display the name, roll no and marks of the student.
Highlight

#include <iostream>

This is our header file as iostream
Highlight

using std namespace

Here we are using the std namespace
Highlight

class student


Here we have class student.
Highlight

int width;

int height;

Then we have integer variable roll_no and character array as name.

As private members of class student.

Highlight

public:

void input();

void display();

};

Function input and function display are public functions of class student.



Highlight

void student :: input()

{

cout << “Enter Roll no.”;

cin>>roll_no;

cout<<"Enter Name:";

cin>>name;

}

Here we are using the input function to accept the roll no. and name of the student.



Highlight

void student::display()


Then we have the display function to display the roll_no and name.



Highlight

class exam:public student

{


Here we have another class as exam_inherit.

This is the derived class.

It inherits the the data and function of class student.

Highlight

private:

int msub1;

int msub2;

int msub3;

int total;

Then we have declared four variables as sub1, sub2, sub3 and total.

As private variables of class exam_inherit.

Highlight

public:

void input_exam();

void display_exam();

};

Here we have two functions input_exam and display_exam as public functions.

Here we close the class.

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;

}

Now we are using function input_exam function to accept the marks of three subjects.



Highlight

void exam::display_exam()

{

total=sub1+sub2+sub3;

cout<<"Total is :" <<total<<"\n";

}

Here, we are using display_exam function to calculate the total of three subjects.

Then we print the total.

Highlight

int main()

This is our main function.
Highlight

exam ex;

Here we create an object of class exam_inherit as ex
Highlight

ex.input();

ex.input_exam();

ex.display();

ex.display_exam();

return 0;

}

Then we call all the functions using the object ex
Highlight

return 0;

And this is the return statement.
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
Type

g++ exam_inherit.cpp -o exam

To execute

Type

./exam

To compile the program type

g++ exam_inherit.cpp (space)-o (space) exam

To execute

Type

./exam

Highlight

Output

Here we see,

Enter Roll no.:

I will enter as 1

Enter Name:

I will enter as Arya

Enter marks of subject1

I will enter as 60

Enter marks of subject1

I will enter as 70

Enter marks of subject1

I will enter as 80

Highlight

Output

Here the output is displayed as

Roll no is: 1

Name is: Arya

Total is: 210

Multilevel Inheritance Now we will see multilevel inheritance in the same example.
Point the cursor to

multilevel.cpp

I have already typed the code.

Let us switch back to our ediotr.

Note that our filename is multilevel.cpp

public:

int total;


Here we have declared the variable total.

As public variable of class exam_inheri.

This is because the private members are not accessed by the derived class.

Highlight:

class grade:public exam_inherit

{

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.
Highlight

private:

int avg;

public:

void average();

};

Here we have declared avg as private member of class grade.

And function average as public function.

Then we close the class.

Highlight


void grade::average()

{

avg=total/3;

cout<<"Average is :"<<avg<<"\n";

}

Here we use the average function to calculate the average.

Then we print the average.



Highlight

int main()

{

grade gd;

gd.input();

gd.input_exam();

gd.display();

gd.display_exam();

gd.average();

Inside the main function we create an object of class grade as gd.

Then we call all the functions usign gd.

gd.input()

input_exam

display

display_exam

and the average function.

Highlight

return 0;

And this is our return statement.
Show in the program Now you can see that class grade is the derived class.

And exam_inherit is the base class for grade.



Show in the program And here class exam_inherit is the derived class.

And class student is the base class for class exam_inherit.

On the terminal Now let us execute the program.

Come back to our terminal.

Type

g++ multilevel.cpp -o multilevel

Type

./multilevel

To compile type

g++ multilevel.cpp -o mul

Press Enter

Type

./multilevel

Press Enter

Highlight

Type:

2

Pratham

65

67

82

Here the output is displayed:

Enter Roll no.:

I will enter as 2

Enter Name:

I will enter as Pratham

Enter marks of subject1

I will enter as 65

Enter marks of subject1

I will enter as 67

Enter marks of subject1

I will enter as 82

Highlight

Output

The output is displayed as:

Roll no is: 2

Name is: Pratham

Total is: 214

Average is: 71

Switch back to the slides This brings us to the end of this tutorial.

Let us come back to the slides

Slide 7

Summary

Let us summarize:

In this tutorial, we have seen,

Inheritance.

Types of inheritance.

Slide 6

Assignment

As an assignment

Create a class Shape.

Create two functions of the class as Area and Perimeter.

Find the area and perimeter of various shapes like square, rectangle and circle.

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


Acknowledgement

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.

Contributors and Content Editors

Ashwini