Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers.

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

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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 Reserved word protected Reserved word super
Polymorphism, Virtual Methods and Abstract Classes.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Chapter 12: Adding Functionality to Your Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
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
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
OPERATOR OVERLOADING Understand Operator Overloading and how it is implemented in C++ ? | Website for students | VTU NOTES.
Object Oriented Software Development
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.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Chapter 9 Classes: A Deeper Look, Part I Part II.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Constructors and Destructors
| Website for students | VTU NOTES
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Constructors and Destructors
9-10 Classes: A Deeper Look.
Chapter 9 Inheritance.
9-10 Classes: A Deeper Look.
Computer Science II for Majors
Presentation transcript:

Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers

Inheritance – process of creating new class from the existing class. New class is called the Derived class / Child class /subclass Existing class is called the Base class / Parent class / Super class. Derived Class derives or inherits the features of existing class. It contains features of existing class and some of its own features. Base class is the original class that remains unchanged and features of exising class is fully or partially inherited. | Website for Students | VTU -NOTES -Question Papers

Derived class inherits all capabilities of base class and add features and refinements of its own. Base class is unchanged by the process. Advantages – Inheritance permits code reusability. Once a class is written and debugged, it need not be touched again. Reusing the existing code saves time, money and increases reliability. | Website for Students | VTU -NOTES -Question Papers

Inheritance also help in the original conceptualization of the problem and overall design of the program. | Website for Students | VTU -NOTES -Question Papers

Inheritance FA FB FC FA FB FC FD Base class Derived class | Website for Students | VTU -NOTES -Question Papers

Syntax for derivation : class : { /*definition of derived class*/ } | Website for Students | VTU -NOTES -Question Papers

Suppose class A already exists. Then a class B is derived from class A as follows Class B : public A { /*new features of class B*/ }; Public access specifier is used in the foregoing example | Website for Students | VTU -NOTES -Question Papers

A pointer from derived class to the base class diagrammatically shows derivation Diagrammatic depiction of Inheritance A B | Website for Students | VTU -NOTES -Question Papers

Effects of inheritance Inheritance affects the size and behavior of the derived class objects in 2 ways 1.An object of derived class contain all data members of the derived class. It contains data members of the base class also. 2.Hence an object of derived class will always be larger than object of base class. | Website for Students | VTU -NOTES -Question Papers

2.With respect to an object of the derived class, we can call public member functions of the derived class in any global non-member function. However we can call public member functions of the base class also with exceptions. //insize.cpp | Website for Students | VTU -NOTES -Question Papers

An object of derived class will contain the data members of the base class and data members of the derived class. Hence the size of object of derived class = sum of the sizes of data members of the base class + sum of sizes of the data members of the derived class. | Website for Students | VTU -NOTES -Question Papers

Inheritance implements an ‘is-a’ relationship. A derived class is a type of the base class like an aircraft (derived class) is a type of vehicle (base class). A class may contain an object of another class / pointer to datastructure that contain set of objects of another class. Such a class is called container class | Website for Students | VTU -NOTES -Question Papers

Eg – An aircraft has 1 engine or an array of engines. Another eg – manager class & employee class. A manager (i.e. an object of the class manager) is an employee (i.e. object of class employee). It has some features that are not possessed by an employee. | Website for Students | VTU -NOTES -Question Papers

Eg – it may have a pointer to an array of employees that report to him. Derived class object is also a base class object shown in the code. | Website for Students | VTU -NOTES -Question Papers

Class Employee { String name; Double basic; Date doj; /*rest of employee class*/ }; | Website for Students | VTU -NOTES -Question Papers

Class manager : public employee {employee *list; /*rest of the class manager*/ } | Website for Students | VTU -NOTES -Question Papers

Inheritance in actual practice In actual practice, the library programmer defines a certain class and member functions. A programmer to create his applications, then inherits from this class and adds only special data members and the code to handle these additional data members in the derived class. | Website for Students | VTU -NOTES -Question Papers

An object of base class A A1 will occupy 4 bytes containing only x. Whereas an object of class B B1 will occupy a block of 8 bytes containing both x and y x4 A1 y 4 B1 Memory layout of base class & Derived class object x Base & Derived class Objects | Website for Students | VTU -NOTES -Question Papers

Accessing members of base class in derived class Only public members of base class can be accessed in the functions of the derived class protected members of base class can also be accessed. But private members of base class cannot be accessed. – Suppose in base class B::setY() function we write x=y; Compiler will report an error stating that private members of the base class cannot be accessed. Here we are trying to access x in mf of derived class, but x is a private member of the class | Website for Students | VTU -NOTES -Question Papers

But we can access A::setX() & A::getX() functions in mfs of derived class because they are public members of the base class. Private members of the base class remain private w.r.t. members of the derived class. Following code show this Void B setY(const int q) { Y=q; setX(y);//x=Y } C++ prevents us from accessing private members of the base Class in mfs of derived class to fully implement data security. | Website for Students | VTU -NOTES -Question Papers

We may want to create a derived class that suppliments base class by containing those mfs missing in base class. Eg – Suppose a function String::addchar() is not present in class String. But here, drawback is in base class & bc should be corrected. Inheritance is not used to remove such lacuna. It provide data & additional code to work upon additional data in derived class. Inheritance is used to add facilities to an existing class without reprogramming it or recompiling it. It uses code reusability. Friendship is not inherited | Website for Students | VTU -NOTES -Question Papers

Base class and Derived class Pointers A base class pointer can safely point at an object of derived class without typecasting. However a derived class pointer cannot point at an object of base class. But a derived class pointer can point at an object of base class only forcibly by type casting with possible runtime errors. | Website for Students | VTU -NOTES -Question Papers

Exceptions – Compiler will prevent a base class pointer from pointing at an object of derived under certain circumstances Reason – program below. class A { Public: int x; }; Class B : public A { Public: int y; } A derived class & its base class | Website for Students | VTU -NOTES -Question Papers

These 2 classes have public member data & no member functions at all. And we will see this with main() void main() { A *APtr; B B1; APtr=&B1; //bc pointer points at dc’s object APtr->x=10; //ok-accessing bc member thru a bc pointer APtr->y=20; //error: y not found in class A } Base class ptr pointing at a derived class object. | Website for Students | VTU -NOTES -Question Papers

Base class pointer is supposed to point at an object of derived class. Through Aptr is able to access x because there is a member x in class A. APtr cannot access an area in memory that has not been allocated. Making a base class pointer point at an object of derived class is a very common C++ programming requirement. Its unsafe to make a derived class pointer point at objects of the base class without explicit typecasting. | Website for Students | VTU -NOTES -Question Papers

void main() { A A1; B *BPtr; BPtr -> &A1; //error: cant convert from B* to A* BPtr -> x=10; //ok : dc ptr accesses bc member BPtr -> y=20; //ok : dc ptr accesses bc member } Listing ADerived class pointer can forcibly made to point at an object of derived class by explicit typecasting | Website for Students | VTU -NOTES -Question Papers

void main() { A A1; B *BPtr; //dc ptr BPtr = (*B)&A1; //forcible typecasting to make //dc ptr point at base class object } Forcible typecasting to make dc ptr point at an object of the base class | Website for Students | VTU -NOTES -Question Papers

Explicit address manipulation like this is dangerous holding for case with classes A & B having x and y data members. class A { int x; public: void setX(const int =0); {/*rest of the function class A*/ } }; | Website for Students | VTU -NOTES -Question Papers

Class B : public A { int Y; public: void setY(const int=0); /*rest of class B*/ }; | Website for Students | VTU -NOTES -Question Papers

void main() { A *APtr; B B1; APtr=&B1; //ok bc ptr points at dclass’s object. APtr->setx(10); //ok: accessing bc member thru //bc ptr APtr->sety(20); //error: sety() not a mf of class A Bc ptr pointing at an object of derived class Mfs in listing A access private data members of their respective classes. | Website for Students | VTU -NOTES -Question Papers

Based upon our knowledge about this pointer, compiler will internally convert the statement B1.setx(10); to setx(&B1,10); The address of B1( a derived class object) is passed as a parameter to the function. But the corresponding formal argument in the A::setx() function is this pointer of type A * const. | Website for Students | VTU -NOTES -Question Papers

void setx( A* const this, const int p) { This -> x=p; } This pointer in base class mf points at the derived class invoking object. Obviously this pointer points at B1 i.e. an object of the derived class | Website for Students | VTU -NOTES -Question Papers

Function Overriding Member functions of the base class can be overridden in the derived class. Defining a member function in the derived class such that its name and signature match those of base class function is known as Function Overriding. One of them is in base class & other one is in derived class. | Website for Students | VTU -NOTES -Question Papers

Class A { Public: void show() {cout << “Show() function of class A is called\n”; } }; Class B { Public: void show() {cout << “Show() function of class B is called\n”; } };//Function Overriding | Website for Students | VTU -NOTES -Question Papers

Show() function of class B has overridden the show( ) function of class A. If show() is called w.r.t. an object of derived class B, the show() of class B will be called instead of show() of class A void main() { B B1; B1.show(); //B::Show() is called } Calling the overriding function d:\overdn.cpp Whenever a function is called w.r.t. an object of a class, the compiler first searches for the function prototype in the same class. Only if this search fails, the compiler goes up the hierarchy to look up for the function prototype. Overridden function of the base class will be called if it is called with respect to an object of the class. //overbs.cpp //overbs1.cpp | Website for Students | VTU -NOTES -Question Papers

Function overriding is a form of function overloading & this pointer make this clear. The signatures of overriding function & the overridden functions are only apparently the same. This pointer make this clear. Overridden function can be called from overriding function as follows void B::show() { A::show(); /*rest of B::show() function*/ } Sro is needed to avoid infinite recursion | Website for Students | VTU -NOTES -Question Papers

Function overriding becomes significant only when the base class function being overridden is virtual. Base Class Initialization – A derived class object is composed of data members of the derived class as well as those of the base class. We need to initialize all of these data members while creating an object of derived class. When an object of derived class is created, the compiler implicitly embeds a call to the base class constructor and then the derived class constructor with respect to the object. | Website for Students | VTU -NOTES -Question Papers

Suppose A is the base class and B is its derived class. The statement B B1; Is converted to B B1; B1.A(); Destructors are called in the reverse order. Explicitly calling the constructors & destructors, w.r.t an existing object is prohibited. Unsuccessful initialization of base class members //usi.cpp | Website for Students | VTU -NOTES -Question Papers

| Website for Students | VTU -NOTES -Question Papers