Inheritance: Extending classes Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
OOP: Inheritance By: Lamiaa Said.
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
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.
Inheritance Inheritance Reserved word protected Reserved word super
Learners Support Publications Inheritance: Extending Classes.
C++ Inheritance Systems Programming.
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.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Computer Science I Inheritance Professor Evan Korth New York University.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
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.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Inheritance - Multilevel MEENA RAWAT PGT (COMP) KV ONGC Chandkheda 1.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
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.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Learners Support Publications Constructors and Destructors.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Classes and Objects
Inheritance and Polymorphism
Inheritance & Polymorphism
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
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Constructors and Destructors
By Muhammad Waris Zargar
Programming with ANSI C ++
C++ Inheritance.
Inheritance:Concept of Re-usability
Chapter 11: Inheritance and Composition
Institute of Petroloeum Technology, Gandhinagar
Object-Oriented PHP (1)
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Inheritance: Extending classes Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND

Contents. 1)Introduction 2)Need for inheritance 3)Types of inheritance 4)Definition of derived and base classes. 5)Visibility modes 6)Inheritance and constructors and destructors. 7)Virtual base Classes 8)Nesting of classes 9)Relation between classes

Introduction. The capability of one class to inherit the properties from another class is called inheritance. The class whose properties are inherited, is called Base class or Super class and the class that inherits these properties is called Derived class or Sub class The most important advantage of inheritance is code reusability. Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time, money, and efforts and increases a program’s reliability.

Inheritance: It is the mechanism of deriving new class from existing class. It provides the idea of reusability. A B Base, super,parent class derived, sub, child class

1. CAPABILITY TO EXPRESS THE INHERITANCE RELATIONSHIP: This ensures the closeness with the real-world models i.e. it lets you generates a model that is closer to the real-world. Ex: The class car inherits from another class automobiles which itself inherits from another class vehicles. 2. REUSABILITY: The most important advantage of inheritance is reusability. Inheritance allows the addition of new features to an existing class without modifying it. A new class (derived class) can be derived from an existing class and add new features to it.

ADVANTAGES OF REUSABILITY: Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time, money and efforts and increases a program’s reliability. The Main advantages of reusability are Faster development time Easier maintenance Easy to extend

3. TRANSITIVE NATURE OF INHERITANCE: If a class B inherits properties of another class A, then all subclasses of B will automatically inherit the properties of A. This property is called transitive nature of inheritance. The benefit of this nature is that if any correction takes place in class A will also be reflected in all the classes that are inherited from it.

Types of inheritance Single level B A Multilevel Multiple Hierarchical hybrid B A C A B C A B C D E FGH I J B A C B A B C C

Types of inheritance. 1)Single Inheritance: When a subclass is inherited from only one base class it is known as Single Inheritance. X Y BASE CLASS DERIVED CLASS

2) MULTIPLE INHERITANCE: When a derived class is inherited from multiple base classes is known as Multiple Inheritance BASE CLASS DERIVED CLASS X Y BASE CLASS Z

3) HIERARCHICAL INHERITANCE: When many derived classes are inherited from a single base class it is called Hierarchical Inheritance x Y Z W BASE CLASS DERIVED CLASSES

4) MULTILEVEL INHERITANCE: When a subclass or derived class is inherited from another derived class is known as Multilevel Inheritance X Y Z Base class of Y Derived class of X Base class of Z Derived class of Y

5) HYBRID INHERITANCE: Hybrid Inheritance combines two or more forms of Inheritance. i.e. Inheritance-When a subclass inherits from multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is called hybrid inheritance W XY Z a X YZ A B C b

Definition of derived and base classes. The Visibility Mode controls the visibility and availability of inherited base class members in the derived class. 1)Single Inheritance: SYNTAX of Defining of derived class: class derived-class:<visibility mode> <base-class> Example: class Sub: public Super { }; =>The derived class has access privilege only to the non private members of the base class. =>If no visibility mode is specified, then by default the visibility mode is considered as Private.

2)Multiple Inheritance: class derived_class:vis_mode base1,vis_mode base2 {. }; Class Sub: public SuperA,private SUperB { //Members };

3)Multilevel Inheritance: SYNTAX Class derived1:visibility_mode base {.. } Class derived2:visibilty_mode base {.. };

Visibility Modes. The visibility modes basically control the access specifier to be for inheritable members of base class, in derived class. Role of access specifiers in inheritable classes. Visibility Mode Inheritable public members become Inheritable protected members become Inheritable private members become Public ProtectedPrivate Protected Private

1)Public visibility mode: The public derivation means that the derived class can access the public members of the base class but not the private members of the class. with publicly derived class, the public members of the base class become the public members of the derived class, and the protected members of the base class become the protected members of the derived class. Class Super Class Sub X  private Y  public Z  protected Y  public Z  protected

2) Private visibility mode:The private derivation means the derived class can access the public and protected members of the class privately. i.e public and protected members of the base class become private members of the derived class. They cannot be inherited further if the derived happens to be the base class of any other class. Class Super { Private: int x;void check(void); Public: int y;void display(void); Protected: int z; void getval(void); }; Class Sub: Public Super { private: int s; void init(void); pubilc:int b; readit(void); Protected: int c; writeit(void); };

3) Protected visibility mode: The protected derivation of a class means that the derived class can access the public and protected members of the base class protectedly.i.e the public and protected members of the base class become protected members of the derived class. Class Super { }; Class Sub: protected Super { }; Deriving publicly is a way of saying “ is a type of”.

=> Inheritance and the Base class 1. Member intended to be inherited and at the same time intended to be available to every function, even to non-members,should be declared as public members in the base class. 2. Member intended to be inherited but not intended to be public, should be declared as protected members in the base class. 3. Member not intended to be inherited should be declared as private members in the base class. => The significance of visibility mode:

ACCESSIBILITY OF BASE CLASS MEMBERS. ACCESS SPECIFIER Accessible from own class Accessible from derived class Accessible from objects outside class PUBLIC Yes PROTECTED Yes No PRIVATE YesNo

INHERITANCE AND CONSTRUCTORS AND DESTRUCTORS: When an object of a derived class is created, the program first calls the constructor of base class, then the constructor for the derived class. This is because the constructor of derived class may build upon data members from base class; hence base class object has to be constructed first. When an object of a derived class expires, first the derived class destructor is invoked, followed by the base class destructor.

. class Super {.. //same as before }; Class Sub: public Super { }; int main() { Sub Ob1; } While Ob1 is created, first the constructor Super:: Super( ) is invoked and then Sub::sub( ) is invoked. Also when ob1 expires, at the main ( ),firstly Sub::~Sub() is invoked and then Super:: ~Super( ) is invoked. Sometimes, the base class contractors require arguments to construct their object. In such situation, it is the responsibility of the derived class constructor to pass those arguments required by the corresponding base class.

Derived:: Derived(type x, type y,….): Base(x,y) { } derived class constructor accepts argument for itself as well as for its base class Then the derived class constructor invoked base class constructor by passing appropriate arguments that it received for base constructor

=>Passing Arguments through to a Base class Constructor Class Base { int a; Float b; Public: Base(int I, float j) { A=I; B=j; }; Class Derived: public Base { Public: Derived (int p,float q):Base(p, q) { } }; Even if the derived constructor does not need a parameter for itself, yet it accepts parameters for it base class

Class Base { int a; Float b; Public: Base(int I, float j) { A=I; B=j; }; Class Derived: public Base { int x; float y; Public: Derived (int i,float j,int p,float q):Base(p, q) { } }; the derived constructor is accepting parameters for itself as well as for its base class

Inheritance and Access Control. 1)Access control in publicly derived classes: A class is publicly derived when the keyword public precedes the base class name in the class definition. Note: The private members of the base class can be accessed indirectly using public or protected member function of the base class. Eg. Syntax: class Manager: public Employee, public Person {.. }

2)Access control in privately derived classes: A class is privately derived when the keyword private precedes the base class name in the class definition. Note: The public and protected members of the base class becomes private members of the derived class in private derivation. the object of the derived class cannot access them directly. Eg. class Manager: private Employee {.. }

Note: 1.The private members of the base class are visible in the derived class but they are not directly accessible. int test; Class Base { int test; public: void getit() { cin>>test; } }; Class Derived: public Base { public: void check() { test++;// not allow };

2. You cannot deny access to certain members of a Base class when inheritance publicly. Class Base{ public: int x,y,z; }; Class Derived: public Base { public: int a; Private: Base::x;//not allowed };

3. You can selectively allow access to some of the base class members when deriving privately. Example: Class Base{ public: int x,y,z; }; Class Derived: private Base { public: Base::x;// allowed int a; };

Abstract class: A class that serves only as a base class from which other classes are derived, but no objects of this base class type exit, is known as Abstract class.  Making a private Member Inheritable: (a). By making the visibility mode of the private member as public. (b). By making the visibility mode of the private member as protected.

=> Private Protected Public Private Protected Public Private Protected Public Private Protected Public Not Inheritance Base class

=> Shadowing/Overriding Base Class Functions in Derived Class: When a derived class function has the same name as that if its base class member function(i.e. redefined in derived class), the derived class member function shadows/hides the base’s class inherited function and this situation is known as function OVERRIDING. => Unveiling shadowed Inherited Members: A using declaration is a way or make available pre-defined members to the current scope. syntax;: using :: Using A::f1;

 Constructors in multiple inheritance: The base classes are constructed in the order in which they appear in the declaration of the derived class.  The base class constructors are called and executed before executing the statements in the body of the derived constructor.

VIRTUAL BASE CLASS: An element of ambiguity can be introduced into a c++ program when multiple base classes are inherited. Base D1 D2 D3

#include Class Base{ public:int a }; Class D1:public Base { public: int b; }; Class D2:public Base { public:int c; }; Class D3:public D1, public D2 { public: int total; }; void main() { D3 ob; ob.a=25; //ambiguous ob.b=50; ob.c=75; ob.total=ob.a+ob.b+ob.c; //ambiguous cout<<ob.a<<”\t”<<ob.b<<“\t”<< ob.c<<“\t”<<ob.total<<“\n”; }

1.Solved the above problem using scope resolution operator. void main() { D3 ob; Ob.D1::a=25; //SCOPE resolved, used D1’s a ob.b=50; ob.c=75; ob.total=ob.D1::a+ob.b+ob.c; cout<<ob.D1::a<<”\t”<<ob.b<<“\t”<<ob.c<<“\t”<<ob.total<<“\n”; }

2.Solved the above problem using virtual Base class. The dreaded diamond refers to a class structure in which a particular class appears more than once in a class’s inheritance hierarchy. Base D1 D2 D3 The use virtual keyword in the classes just below the top of the dreaded diamond and not in the join class. Base virtual D1 virtual D2 D3

#include Class Base{ public:int a }; Class D1:virtual public Base { public: int b; }; Class D2:virtual public Base { public:int c; }; Class D3:public D1, public D2 { public: int total; }; void main() { clrscr(); D3 ob; ob.a=25; //Now unambiguous ob.b=50; ob.c=75; ob.total=ob.a+ob.b+ob.c; //unambiguous cout<<ob.a<<”\t”<<ob.b<<“\t”< <ob.c<<“\t”<<ob.total<<“\n”; }

=>NESTING OF CLASSES: When a class contains objects of another class type as its member or when a class contains another class within its memory, it is known as nesting of classes. it is also known as containership or containment or aggregation. For Example: Class X{…..}; Class Y {….}; Class Z{ X Ob1;//object of X class Y Ob2; //object of Y class };

Relationship between classes 1)IS-A Relationship- When a class inherits from another class it is known as a IS-A relationship. 2)HAS-A Relationship- When a class contains object of another class type it is known as a HAS-A relationship. The class having HAS-A relationship with other class has the Ownership of the contained object. Ownership define the responsibility for the creation and the destruction of an objet. 3)HOLDS-A Relationship- It is similar to HAS-A relationship but ownership is missing in HOLDS- A relationship. A class that indirectly contains another object.i.e. via pointer or reference.

Summary. 1.Inheritance is the capability of one class to inherit the properties of another class. 2.Inheritance supports reusability of code and is able to simulate the transitive nature of real life objects. 3.A class from which another class is inheriting its properties is called base class and the class inheriting properties is known as a sub class or derived class. 4.When a class inherits from a single class it is known as single inheritance. 5.When a class inherits from multiple base classes it is known as multiple inheritance. 6.When several classes inherit from the same class it is hierarchical inheritance. 7.When a subclass is the base class of another

class it is known as multilevel inheritance. 5. When a class inherits from multiple base classes and all of its base classes are subclasses of the same class it is hybrid inheritance. 6. A class can derive itself publicly, privately and protectedly. 7. In the publicly derived class the public and protected members of the base class become private members. 8. In privately derived class the public and the protected members of the base class become private members. 9. In the protectedly derived class the public and the protected members of the class become protected members. 10. The derived class constructor is responsible for invoking (and passing arguments to) the base class constructor.

11. The derived class can directly access only the public and protected members of the base class. 12. To make the private member of a class inheritable declare it under protected section of the base class. 13. When a class inherits from more than one base class this is called multiple inheritance. 14. When a derived class and its base class have common ancestor then ambiguity may arise as the derived class contains multiple copies of common ancestor. This can be resolved either by using scope resolution operator :: or by declaring the common ancestor as virtual.