Learners Support Publications www.lsp4you.com Inheritance: Extending Classes.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
Need for Inheritance Capability of inheriting features. Transitive relationship. Reusablity of class.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance Inheritance is the capability of one class of things to inherit the capabilities or properties from another class. Consider the example of.
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
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Inheritance: Extending classes Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Chapter 12: Adding Functionality to Your Classes.
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.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)
1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.
Inheritance - Multilevel MEENA RAWAT PGT (COMP) KV ONGC Chandkheda 1.
Learners Support Publications Object Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Chapter 9. Inheritance - Basics Inheritance is a mechanism that allows you to base a new class upon the definition of a pre-existing class Subclass inherits.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Types of Inheritance in C++. In C++ we have 5 different types of inheritance: – Single Inheritance – Multiple Inheritance – Hierarchical Inheritance –
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Object-Oriented Programming: Inheritance and Polymorphism.
1.  Inheritance Inheritance  Define a Class Hierarchy Define a Class Hierarchy  Visibility Mode Visibility Mode  Access Rights of Derived Classes.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Chapter 3: Using Methods, Classes, and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Understanding Inheritance
Ambiguity Resolution in Inheritance
CS1201: Programming Language 2
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Packages and Interfaces
Classes and Objects.
Java – Inheritance.
C++ Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
Inheritance:Concept of Re-usability
Institute of Petroloeum Technology, Gandhinagar
Final and Abstract Classes
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Learners Support Publications Inheritance: Extending Classes

Learners Support Publications Introduction Reusability is an important feature of OOP. Reusability is an important feature of OOP. C++ strongly supports the concept of reusability. C++ strongly supports the concept of reusability.

Learners Support Publications Introduction The mechanism of deriving a new class from an old one is called inheritance (or derivation). The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as base class. The old class is referred to as base class. The new class is called the derived class or subclass. The new class is called the derived class or subclass. continue …

Learners Support Publications Introduction The derived class inherits some or all of the traits from the base class. The derived class inherits some or all of the traits from the base class. A class can also inherit properties from more than one class or from more than one level. A class can also inherit properties from more than one class or from more than one level. continue …

Learners Support Publications Single Inheritance A derived class with only one base class. A derived class with only one base class. A B

Learners Support Publications Multiple Inheritance A derived class with several base classes. A derived class with several base classes. A C B

Learners Support Publications Hierarchical Inheritance A traits of one class may be inherited by more than one class. A traits of one class may be inherited by more than one class. A BCD

Learners Support Publications Multilevel Inheritance The mechanism of deriving a class from another derived class. The mechanism of deriving a class from another derived class. A C B

Learners Support Publications Hybrid Inheritance The mechanism of deriving a class by using a mixture of different methods. The mechanism of deriving a class by using a mixture of different methods. A D BC

Learners Support Publications Derived Classes A derived class can be defined by specifying its relationship with the base class in addition to its own details. A derived class can be defined by specifying its relationship with the base class in addition to its own details. class derived-class-name : visibility-mode base-class-name {………// ………// members of derived class ………//};

Learners Support Publications Derived Classes class derived-class-name : visibility-mode base-class-name {………// ………// members of derived class ………//}; The colon indicates that the derived-class-name is derived from the base-class-name The visibility mode is optional and, if present, may be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are derived privately or publicly. continue …

Learners Support Publications Derived Classes When a base class is privately derived by a derived class, “public members” of the base class become “private members” of the derived class. When a base class is privately derived by a derived class, “public members” of the base class become “private members” of the derived class. Therefore the members of the derived class can only access the public members of the base class. Therefore the members of the derived class can only access the public members of the base class. They are inaccessible to the objects of the derived class. They are inaccessible to the objects of the derived class. No member of the base class is accessible to the objects of the derived class. No member of the base class is accessible to the objects of the derived class. continue …

Learners Support Publications Derived Classes When a base class is publicly inherited, ”public members” of the base class become the “public members” of the derived class. When a base class is publicly inherited, ”public members” of the base class become the “public members” of the derived class. They are accessible to the objects of the derived class. They are accessible to the objects of the derived class. continue …

Learners Support Publications Derived Classes The private members of the base class are not inherited in both the cases (publicly/privately inherited). The private members of the base class are not inherited in both the cases (publicly/privately inherited). The private members of a base class will never become the members of its derived class. The private members of a base class will never become the members of its derived class. continue …

Learners Support Publications Inheritance In inheritance, some of the base class data elements and member functions are inherited into the derived class. In inheritance, some of the base class data elements and member functions are inherited into the derived class. We can add our own data and member functions for extending the functionality of the base class. We can add our own data and member functions for extending the functionality of the base class. It is a powerful tool for incremental program development. It is a powerful tool for incremental program development. Can increase the capabilities of an existing class without modifying it. Can increase the capabilities of an existing class without modifying it.

Learners Support Publications Single Inheritance A B

Learners Support Publications Making a Private Member Inheritable By making the visibility limit of the private members to the public. By making the visibility limit of the private members to the public. The visibility modifier “protected” can be used for this purpose. The visibility modifier “protected” can be used for this purpose. A member declared as “protected” is accessible by the member functions within its class and any class immediately derived from it. A member declared as “protected” is accessible by the member functions within its class and any class immediately derived from it. It can not be accessed by the functions outside these two classes. It can not be accessed by the functions outside these two classes.

Learners Support Publications Making a Private Member Inheritable class alpha { private : // optional ……… // visible to the member within its class ……… protected : ……… // visible to member functions ……… // of its own and immediate derived class public : ……… // visible to all functions ……… // in the program }; continue …

Learners Support Publications Protected Member When a protected member is inherited in public mode, it becomes protected in the derived class. When a protected member is inherited in public mode, it becomes protected in the derived class. They are accessible by the member functions of the derived class. They are accessible by the member functions of the derived class. And they are ready for further inheritance. And they are ready for further inheritance. When a protected member is inherited in private mode, it becomes private in the derived class. When a protected member is inherited in private mode, it becomes private in the derived class. They are accessible by the member functions of the derived class. They are accessible by the member functions of the derived class. But, they are not available for further inheritance. But, they are not available for further inheritance. continue …

Learners Support Publications Protected Derivation It is possible to inherit a base class in protected mode – protected derivation. It is possible to inherit a base class in protected mode – protected derivation. In protected derivation, both the public and protected members of the base class become protected members of the derived class. In protected derivation, both the public and protected members of the base class become protected members of the derived class.

Learners Support Publications Effect of Inheritance on the visibility of Members Private Protected Public Private Protected Public Private Protected Private Protected Public Not inheritable Class B Class D1 : public B Class D2 : private B Class X : public D1, protected D2

Learners Support Publications Visibility Base class visibility Derived class visibility Public Derivation Private Derivation Protected Derivation Private  Not Inherited Protected  ProtectedPrivateProtected Public  PublicPrivateProtected

Learners Support Publications Access Control to Data Members Functions that can have access to the private and protected members of a class: Functions that can have access to the private and protected members of a class: A function that is a friend of the class.A function that is a friend of the class. A member function of a class that is a friend of the class.A member function of a class that is a friend of the class. A member function of a derived class.A member function of a derived class.

Learners Support Publications Access mechanism in classes fx1 fx2 fy1 fy2 fz1 fz2 data class X class Y class Z friend of X Inherited from X friend class Y: private protected Function 1

Learners Support Publications Multilevel Inheritance The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. The class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. Class B provides a link for the inheritance between A and C. Class B provides a link for the inheritance between A and C. The chain ABC is known as inheritance path. The chain ABC is known as inheritance path. C Base Class Derived Class A B Intermediate Base Class

Learners Support Publications Multilevel Inheritance class A { ………} ; // Base Class class B : public A { ……… } ; // B derived from A class C : public B { ……… } ; // C derived from B continue …

Learners Support Publications Multiple Inheritance A class can inherit the attributes of two or more classes. A class can inherit the attributes of two or more classes. Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the intelligence of another. It is like a child inheriting the physical features of one parent and the intelligence of another. B-1 D B-2

Learners Support Publications Multiple Inheritance class D: visibility B-1, visibility B-2, …… {……… ……… (Body of D) ………}; Where, visibility may be either public or private. Where, visibility may be either public or private. The base classes are separated by comma. The base classes are separated by comma. continue …

Learners Support Publications Ambiguity Resolution in Inheritance class M { public: public: void display (void) void display (void) { cout << “Class M \n“;} { cout << “Class M \n“;}}; class N {public: void display (void) void display (void) { cout << “Class N \n“;} { cout << “Class N \n“;}}; class P : public M, public N { public: public: void display (void) void display (void) { M :: display();} { M :: display();}}; void main( ) { P p; p.display( ); } In Multiple Inheritance

Learners Support Publications Ambiguity Resolution in Inheritance class A { public: public: void display (void) void display (void) { cout << “Class A \n“;} { cout << “Class A \n“;}}; class B : public A {public: void display (void) void display (void) { cout << “Class B \n“;} { cout << “Class B \n“;}}; void main( ) { B b; B b; b.display( );// in B b.display( );// in B b.A::display( );// in A b.A::display( );// in A b.B::display( );// in B b.B::display( );// in B} Ambiguity can be resolved by specifying the function with class name and scope resolution operator to invoke. continue … In Single Inheritance

Learners Support Publications Hierarchical Inheritance Inheritance support hierarchical design of a program. Inheritance support hierarchical design of a program. Additional members are added through inheritance to extend the capabilities of a class. Additional members are added through inheritance to extend the capabilities of a class. Programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level Programming problems can be cast into a hierarchy where certain features of one level are shared by many others below that level FD STD MTD LTD Account SBCA

Learners Support Publications Hybrid Inheritance Applying Two or more types of inheritance together. Applying Two or more types of inheritance together. test result student sports

Learners Support Publications Virtual Base Classes Here the result class has two direct base classes test and sports which themselves have a common base class student. Here the result class has two direct base classes test and sports which themselves have a common base class student. The result inherits the traits of student via two separate paths. The result inherits the traits of student via two separate paths. test result student sports

Learners Support Publications Virtual Base Classes It can also inherit directly as shown by the broken line. It can also inherit directly as shown by the broken line. The student class is referred to as indirect base class. The student class is referred to as indirect base class. continue … test result student sports

Learners Support Publications Virtual Base Classes All the public and protected members of student are inherited into result twice, first via test and again via sports. All the public and protected members of student are inherited into result twice, first via test and again via sports. This means result class have duplicate set of members inherited from student. This means result class have duplicate set of members inherited from student. continue … test result student sports

Learners Support Publications Virtual Base Classes class student {………}; class test : virtual public student {………}; class sports : public virtual student {………}; class result : public test, public sports {………\}; continue … test result student sports

Learners Support Publications Abstract Classes An abstract class is one that is not used to create objects. An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class. An abstract class is designed only to act as a base class. It is a design concept in program development and provides a base upon which other classes may be built. It is a design concept in program development and provides a base upon which other classes may be built.

Learners Support Publications Constructors in Derived Classes If no base class constructor takes any arguments, the derived class need not have a constructor function. If no base class constructor takes any arguments, the derived class need not have a constructor function. If any base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructors. If any base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructors.

Learners Support Publications Constructors in Derived Classes When both the derived and base class contain constructors, the base constructor is executed first and then the constructor in the derived class is executed. When both the derived and base class contain constructors, the base constructor is executed first and then the constructor in the derived class is executed. In case of multiple inheritance, the base class constructors are executed in the order in which they appear in the declaration of the derived class. In case of multiple inheritance, the base class constructors are executed in the order in which they appear in the declaration of the derived class. continue …

Learners Support Publications Constructors in Derived Classes In a multilevel inheritance, the constructors will be executed in the order of inheritance. In a multilevel inheritance, the constructors will be executed in the order of inheritance. Since the derived class takes the responsibility of supplying initial values to its base classes, we supply the initial values that are required by all the classes together, when a derived class object is declared. Since the derived class takes the responsibility of supplying initial values to its base classes, we supply the initial values that are required by all the classes together, when a derived class object is declared. continue …

Learners Support Publications Constructors in Derived Classes The constructor of the derived class receives the entire list of values as its arguments and passes them on to the base constructors in the order in which they are declared in the derived class. The constructor of the derived class receives the entire list of values as its arguments and passes them on to the base constructors in the order in which they are declared in the derived class. The base constructors are called and executed before executing the statements in the body of the derived constructor. The base constructors are called and executed before executing the statements in the body of the derived constructor. continue …

Learners Support Publications Constructors in Derived Classes The header line of derived-constructor function contains two parts separated by a colon (:). The header line of derived-constructor function contains two parts separated by a colon (:). The first part provides the declaration of the arguments that are passed to the derived constructor.The first part provides the declaration of the arguments that are passed to the derived constructor. The second part lists the function calls to the base constructors.The second part lists the function calls to the base constructors. continue …

Learners Support Publications Defining Derived Constructors Derived-constructor(Arglist1, Arglist2, … ArglistN, ArglistD) : base1(arglist1),base2(arglist2),…baseN(arglistN){} continue …

Learners Support Publications Member Classes : Nesting of Classes Inheritance is the mechanism of deriving certain properties of one class into another. Inheritance is the mechanism of deriving certain properties of one class into another. C++ supports a new way of inheriting classes: C++ supports a new way of inheriting classes: An object can be collection of many other objects.An object can be collection of many other objects. A class can contain objects of other classes as its members.A class can contain objects of other classes as its members.

Learners Support Publications Member Classes : Nesting of Classes class alpha { ……… }; class beta { ……… }; class gamma { alpha a;// an object of class alpha beta b;// an object of class beta ………}; continue …

Learners Support Publications Member Classes : Nesting of Classes class alpha { ……… }; class beta { ……… }; class gamma { alpha a; beta b; ………}; All objects of gamma class will contain the objects a and b. All objects of gamma class will contain the objects a and b. This is called containership or nesting. This is called containership or nesting. continue …

Learners Support Publications Member Classes : Nesting of Classes An independent object is created by its constructor when it is declared with arguments. An independent object is created by its constructor when it is declared with arguments. A nested object is created in two stages: A nested object is created in two stages: The member objects are created using their respective constructors.The member objects are created using their respective constructors. Then ordinary members are created.Then ordinary members are created. Constructors of all the member objects should be called before its own constructor body is executed. Constructors of all the member objects should be called before its own constructor body is executed. continue …

Learners Support Publications Member Classes : Nesting of Classes class gamma { ……… ……… alpha a; alpha a; beta b; beta b;public: gamma(arglist): alpha(arglist1), beta(arglist2) gamma(arglist): alpha(arglist1), beta(arglist2) { body of the constructor } }; continue …

Learners Support Publications Thank You Learners Support Publications