LOGO Lecturer: Abdullahi Salad Abdi May 1, 2015 1 Object Oriented Programming in C++

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Learners Support Publications Inheritance: Extending Classes.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
CS-2135 Object Oriented Programming
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Understanding Inheritance Object-Oriented Programming Using C++ Second Edition 9.
Inheritance, Polymorphism, and Virtual Functions
Computer Science I Inheritance Professor Evan Korth New York University.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
LECTURE 07 Programming using C# Inheritance
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Inheritance Chapter 9. 2 What You Will Learn Software reusability (Recycling) Inheriting data members and functions from previously defined classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
BY:- TOPS Technologies
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Inheritance, Polymorphism, and Virtual Functions
Inheritance in Java.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Road Map Inheritance Class hierarchy Overriding methods Constructors
Understanding Inheritance
Learning Objectives Inheritance Virtual Function.
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
Java Programming, Second Edition
C++ Inheritance.
Inheritance:Concept of Re-usability
Fundaments of Game Design
B.FathimaMary Dept. of Commerce CA SJC Trichy-02
COP 3330 Object-oriented Programming in C++
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++

LOGO Chapter 9 Inheritance & Polymorphism Class: BIT02

LOGO Objectives  Inheritance  categories of Inheritance  Protected Access Specifier  Types of Inheritance Public Private Protected  Derived Class Constructors  Multiple Inheritance 3

LOGO Inheritance  Inheritance is the second most important feature of the Object Oriented Programming. In inheritance, the code of existing classes is used for making new classes. This saves time for writing and debugging the entire code for a new class. 4

LOGO Cont….  To inherit means to receive. In inheritance a new class is written such that it can access or use the members of an existing class.  The new class that can access the members of an existing class is called the derived class or child class. The existing class is called the base class or parent class. 5

LOGO Defining a Class  The derived class can use the data members and member functions of the base class.  It can also have its own data members and member functions. Thus a derived class can be larger than a base class. 6

LOGO Figure 7 Member 1 Member 2 Member 1 Member 3

LOGO Cont…  The figure shows the relationship between a derived class and the base class. The arrow is drawn from the derived class to the base class.  The direction of the arrow indicates that the derived class can access members of the base class but the base class cannot access member of its derived class. 8

LOGO Cont…  The idea of inheritance implements the is a relationship.  For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on. 9

LOGO Types of Inheritance 1.Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class. 10

LOGO Real life example All Cars are Vehicles. All Motorcycles are Vehicles. All Sedans are Vehicles. Vehicles is the base class. Car is a derived class. Truck derives from Vehicle. Everything about being a Vehicle is inherited by Car s and Truck s and SUV s.

LOGO Types of Inheritance 2. Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es) 12

LOGO Types of Inheritance 3. Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. 13

LOGO Types of Inheritance 4. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes. 14

LOGO Types of Inheritance 5. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance. 15

LOGO Protected Access Specifier  The public members of a class are accessible by all functions in the program and the private members of a class are accessible only by member functions and friend functions of that class.  Similarly, the protected members of a class are accessible by the member functions and friend functions of that class. 16

LOGO Cont….  The protected members of a base class are, however, accessible by members of its derived classes  but the private members of the base class are not accessible directly by members of its derived classes.This is the main difference between the protected and the private access specifiers. 17

LOGO Cont…  The protected members of a base class fall between private and public member. These members are public for the derived class but for the rest of the program, these are treated as private. 18

LOGO Defining Derived Classes  The syntax for defining a derived class is highly different from the syntax of the base class definition. The declaration of a derived class also includes the name of the base class from which it is derived. 19

LOGO Syntax Class sub_class_name : specifier base_class_name { Members of derived class }; 20

LOGO Example  A class “student” is defined as Class student { Private: Char name[15], address[15]; Public: Void input(void) Void show(void) }; 21

LOGO Example  The class student has two data members and to member functions.  Suppose the marks obtained by a student in three subjects and the total marks of these subjects are to be included as new data members in the above class. This is done by adding new members in the class. There are two ways in which these new members can be added to the class. These are: 22

LOGO Example  Add new member in the original class OR  Define a new class that has the new members and that also uses members of the existing “student” class. Using the members of an existing class is the principle of inheritance.  The new class is the derived class. The existing class serves as the base class for the derived class. 23

LOGO Example  Driving a new class from the existing class reduces the size of the program. It also eliminates duplication of code within the program.  For example, let the name of the new class be marks. This class uses the members of the existing student class. 24

LOGO Cont… Class marks : public student { Private: Int s1,s2,s3,total; Public: Void inputmarks(void) Void show_details(void) }; 25 Example

LOGO Types of Inheritance  There are three kinds of inheritance  Public  Private  Protected 26

LOGO Public inheritance  In Publilc Inheritance, the public members of the base class become the public members of the derived class. Thus the objects of the derived class can access public members (both data and functions) of the base class. Similarly, the protected data members of the base class also become the protected members of derived class. 27

LOGO Public inheritance Syntax  The general syntax for deriving a public class from base class is: Class sub_class_name : public base_class_name { }; 28 Example

LOGO Private Inheritance  In private inheritance, the objects of the derived class cannot access the public members of the base class. Its objects can only access the protected data members of the base class. Class sub_class_name : private base_class_name { }; 29 Example

LOGO Protected Inheritance  The object of the base class that is derived as protected can access only the protected member of the base class. Class sub_class_name : protected base_class_name { }; 30

LOGO Protected Inheritance Where  Protectedspecifies the protected inheritance  Sub_class_namerepresents name of the derived class  Base_class_namerepresents name of the base class 31 Example

LOGO 32

LOGO Derived Class Constructor  In inheritance, a constructor of the derived class as well the constructor functions of the base class are automatically executed when an object of the derived class is created.  The following example explains the concept of execution of constructor functions in single inheritance. 33 Example

LOGO Constructor in Single Inheritance with Arguments  In case of constructor functions with arguments, the syntax to define constructor of the derived class is different. To execute a constructor of the base class that has arguments through the derived class constructor is defined as:  Write the name of the constructor function of the derived class with parameters  Pace a colon immediately after this and then write the name of the constructor function of thee base class with parameters 34 Example

LOGO Multiple Inheritance  In multiple inheritance, a class is derived by using more than one classes. In multiple inheritance the derived class receives the members of two or more base classes.  Multiple inheritance is a powerful feature of Object Oriented Programming. This technique reduces the program size and saves programmer’s time. The programmer uses the existing base classes in the program coding to solve problems. 35

LOGO Multiple Inheritance  The figure sown below illustrates the relation of derived class with base class. 36 Base Class A Base Class B Derived Class C

LOGO Syntax  The syntax of multiple inheritance is similar to that single inheritance class. The names of base classes are written separated by commas (,).  The general Syntax is: 37

LOGO Syntax Class sub_class: sp1 b_class_1, sp2 bclass_2, …….. { Members of derived class }; Where:  sp1specifies the access specifier of the first base class  Sp2specifies the access specifier of the second base class 38

LOGO Syntax Where:  sp1specifies the access specifier of the first base class  Sp2specifies the access specifier of the second base class 39 Example

LOGO Constructors in Multiple Inheritance  When a class is derived from more than one base classes, the constructor of the derived class as well as of all its base classes are executed when an object of the derived class is created.  If constructor function have no parameters than first the constructor functions of the base classes are executed and then the constructor functions of the derived class is executed. 40 Example

LOGO Constructors in Multiple Inheritance with Arguments  To execute constructors of base classes that have arguments through the derived constructor functions, the derived class constructor is defined as:  Write a constructor function of the derived class with parameters  Place a colon (:) immediately after this and then write the constructor function names of base classes with parameters separated by commas. 41 Example

LOGO Cont…