0% found this document useful (0 votes)
35 views26 pages

Lecture 1.3.1

The document outlines a course on Basic Data Structures using C++, focusing on inheritance and its significance in object-oriented programming. It details course objectives, outcomes, evaluation methods, and the concept of inheritance, including its types and modes. Additionally, it provides assessment questions and references for further learning.

Uploaded by

24BAI70081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views26 pages

Lecture 1.3.1

The document outlines a course on Basic Data Structures using C++, focusing on inheritance and its significance in object-oriented programming. It details course objectives, outcomes, evaluation methods, and the concept of inheritance, including its types and modes. Additionally, it provides assessment questions and references for further learning.

Uploaded by

24BAI70081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNITS


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Basic Data Structure Using C++
Code:24CSH-103

Inheritance DISCOVER . LEARN . EMPOWER


Basic Data Structure
Using C++

Course Objectives
• To enable the students to understand various stages
and constructs of C++ programming language.
• To improve their ability to analyze and address variety
of problems in C++.
• To understand the concept of data structures and
various operations on them.
• To understand the properties of various data structures
and able to identify the strengths and weaknesses of
different data structures.
• To analyze and compare the efficiency of algorithms
and learn to design efficient algorithms for solving
computing problems
2
Course Outcomes
CO Course Outcome
Number
CO1 Understand the concepts of object-oriented programming including
programming process and compilation process.
CO2 Apply different techniques to decompose a problem and
programmed a solution with various concepts of object-oriented
programming language.
CO3 Analyse and explain the behaviour of linear data structure
operations using the programming addressed in the course.
CO4 Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5 Design the solution of real-world problems in order to determine
that the program performs as expected.

3
Scheme of Evaluation
Direct Evaluation Weightage of actual Final Weightage in Mapping with SIs Remarks
Sl No. Frequency of Task BT Levels CO Mapping Mapping with PIs
Instruments conduct Internal Assessment (ABET) (Graded/Non-Graded)

SO1 1a,1b,1c
10 marks for each
1 Assignment One per unit 10 Hard CO4,CO5 Graded
assignment

SO6
2 Exam 20 marks for one MST 2 per semester 20 Medium CO1,CO2,CO3,CO4 6a,6b Graded

3 Case Study 8 marks 1 per unit 8 Easy CO2 SO1, SO6 1c,6b Graded
NA NA NA NA
One per lecture
4 Homework NA topic (of 2 NA Non-Graded
questions)

NA NA NA NA
5 Discussion Forum NA One per unit NA Non-Graded

NA NA NA NA
6 Presentation NA NA NA Non-Graded

7 Attendance NA NA 2 NA NA NA NA Graded

Remarks
Direct Evaluation Final Weightage in Mapping with SIs
S No. Weightage of actual conduct Frequency of Task BT Levels CO Mapping Mapping with PIs (Graded/Non-
Instruments Internal Assessment (ABET)
Graded)
Unit wise Practical 1a, 1b, 1c, 6a, 6b SO1, SO6
1 15 marks 3 45 Medium 1,2,3,4,5 Graded
Evaluation
1a, 1b, 1c, 6a, 6b
2 Exam 15 marks for one MST 1 per semester 15 Medium 1,2,3 SO1, SO6 Graded
3 Attendance NA NA 2 NA NA NA NA Graded
4
• Defining derived class
• modes of inheritance
• types of inheritance

CONTENTS

The capability of a class to derive properties and


characteristics from another class is called
Inheritance.
Inheritance is one of the most important feature
of Object Oriented Programming

5
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is called Base or Parent class. The New class is called as
Child or Derived Class.
Advantages
• It permits code reusability. So, save time and increase the program reliability.
• A programmer can use a class created by another person or company without modifying it derive other classes from it that are suited to
particular situations
• Improve Program Reliability
• It Permits code sharing
• The base class need not be changed but can be adapted to suit the requirements in different applications.

6
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is called Base or Parent class. The New class is called as
Child or Derived Class.

Base/Sub Class Derived/Super class


Methods Base class methods
and +
Properties Additional methods

7
WHY AND WHEN TO USE INHERITANCE?
• Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount(), capacity(), applyBrakes() will
be same for all of the three classes. If we create these classes avoiding inheritance then we have to write all of these functions in each of
the three classes as shown in below figure:

Figure 4.1 Need of inheritance [1]

8
WHY AND WHEN TO USE INHERITANCE?
• If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can
simply avoid the duplication of data and increase re-usability

Figure 4.2 Need of inheritance [1]

9
IMPLEMENTING INHERITANCE IN C++
• For creating a sub-class which is inherited from the base class we have to follow the below syntax.
• Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
• Here, subclass_name is the name of the sub class, access_mode is the mode in which you want to inherit this sub class for example:
public, private etc. and base_class_name is the name of the base class from which you want to inherit the sub class.

10
IMPLEMENTING INHERITANCE IN C++
• EXAMPLE

Figure 4.3 Example inheritance made on Dev C++ 11


IMPLEMENTING INHERITANCE IN C++
• EXAMPLE

Figure 4.4 Example inheritance made on Dev C++ 12


MODES OF INHERITANCE
• Modes of Inheritance
• Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the
derived class and protected members of the base class will become protected in derived class.
• Protected mode: If we derive a sub class from a Protected base class. Then both public member and protected members of the base
class will become protected in derived class.
• Private mode: If we derive a sub class from a Private base class. Then both public member and protected members of the base class will
become Private in derived class.

Figure 4.5 Modes of inheritance[1] 13


Types of Inheritance (Overview)

14
PRE-REQUISITES (PUBLIC, PRIVATE AND PROTECTED
INHERITANCE)
1. Public Inheritance
Consider the dummy code given below for inheritance.
class B : public A
{
}:
The line class B : public A tells the compiler that we are inheriting class A in class B in public followings :
(a) All the public members of class A becomes public members of class B.
(b) All the protected members of class A becomes protected members of class B.
(c) Private members are never inherited.

15
2. PRIVATE INHERITANCE

Consider the dummy code given below for inheritance:


class B : private A
{
}:
The line class B : private A tells the compiler that we are inheriting class A in class B in private mode. In private
mode inheritance note the following points:
(a) All the public members of class A becomes private members of the class B.
(b) All the protected members of the class A becomes private members of class B.
(c) Private members are never inherited.

16
The above dummy code can be written as too.
class B : A
{
}:

As the default inheritance mode is private mode.

17
PROTECTED INHERITANCE
Consider the dummy code given below for inheritance:
class B : protected A
{
}:
The line class B : protected A tells the compiler that we are inheriting class A in class B in protected mode. In
protected mode inheritance note the following points:
(a) All the public members of class A becomes protected members of class B.
(b) All the protected members of class A becomes protected members of class B.
(c) Private members are never inherited.

18
NOTE:
• Note that: A class A is inherited in class B in public mode, all protected members of class A becomes
protected for class B. Now if this class B is inherited to some new class C, then these protected members
inherited from A will be available from A will be available to class C, in whatever mode you inherit class B to
class C.
• Initially let us assume that you inherited class A into class B in private mode, then all protected members of
class A becomes private for class B. They can be used inside the class B. But if you inherit class B into new class
C then these members won’t be available in C as private members are never inherited.

19
Assessment Questions
1. (a) Define ambiguity in inheritance.
(b) What are modes of inheritance?
(c) What is nesting of classes?
2. Can we call private constructor? If yes , then how?
3. Write a C++ program to differentiate between three modes of inheritance.
4. Write a C++ program to demonstrate hierarchical inheritance.

20
APPLICATIONS
• You use nested classes to reflect and to enforce the relationship between two classes. You should define a class within another class
when the nested class makes sense only in the context of its enclosing class or when it relies on the enclosing class for its function. For
example, a text cursor might make sense only in the context of a text component.

21
Frequently Asked Questions: -
1. Is inheritance important to C++?
2. When would I use inheritance?
3. How do you express inheritance in C++?
4. Is it okay to convert a pointer from a derived class to its base class?
5. What’s the difference between public, private, and protected?
6. Why can’t my derived class access private things from my base class?
7. How can I protect derived classes from breaking when I change the internal
parts of the base class?
8. I’ve been told to never use protected data, and instead to always use private
data with protected access functions. Is that a good rule?
9. Okay, so exactly how should I decide whether to build a “protected interface”?
22
SUMMARY
1. Inheritance is the mechanism of deriving new class from an existing class. The old class and new class is
known as in pair : super-sub, base-derived, parent-child.
2. Inheritance provides the idea of reusability. Code once written can be used again and again in several
different classes.
3. C++ provides support for: single level and multilevel inheritance, multiple inheritance, hierarchical
inheritance, hybrid inheritance.
4. Private members are never inherited.
5. Virtual base class is used to avoid duplicity of data members into the target derived class.

23
SUMMARY (Continued)
• In an inheritance hierarchy constructor of base class is called first,
then constructor of derived class is called.

• In an inheritance hierarchy destructor class is called first, then


destructor of base class is called.

• Containership allows us to use object of one class as a member of


other class. This type of containership provides the idea of has a
relationship or nesting of classes.

24
REFERENCES
Reference Website
[1] https://www.geeksforgeeks.org/inheritance-in-c/
[2] https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/
[3] https://www.geeksforgeeks.org/copy-constructor-argument-const/
[4] https://en.cppreference.com/w/cpp/language/nested_types

References on YouTubes
[1] https://www.youtube.com/watch?v=rr7HVs4d1Qo
[2] https://www.youtube.com/watch?v=5pJyKzON8Ww
[3] https://www.youtube.com/watch?v=RO1ZYW9NAzg

References books
[1] Object-Oriented Programming in C++, by Robert Lafore
[2] Object-Oriented Programming Using C++, by Joyce Farrell
[3] Object-Oriented Programming Using C++, by Pohl
25
THANK YOU

You might also like