I NHERITANCE Chapter 7 Department of CSE, BUET 1.

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

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
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.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
T EMPLATES Chapter 11 (11.1 and 11.2 only) Department of CSE, BUET 1.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
1 Inheritance: constructors Constructors, copy constructors and destructors are NOT inherited. Each subclass has its own constructors and destructor to.
Multiple inheritance. In C++ we may define multiple direct base classes: In C++ we may define multiple direct base classes: colourlocation colourlocation.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
CSC241 Object-Oriented Programming (OOP) Lecture No. 19.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
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.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
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.
1 Chapter 4: Another way to define a class Inheritance..!!
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
A First Book of C++ Chapter 12 Extending Your Classes.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Polymorphism &Virtual Functions
Chapter 5 Classes.
Polymorphism & Virtual Functions
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CS1201: Programming Language 2
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
By Muhammad Waris Zargar
Inheritance: Polymorphism and Virtual Functions
Multiple Base Classes Inheritance
Constructors & Destructors
Inheritance in C++ Inheritance Protected Section
Presentation transcript:

I NHERITANCE Chapter 7 Department of CSE, BUET 1

O BJECTIVES Base class access control Using protected members Visibility of base class members in derived class Constructors, destructors, and inheritance Multiple inheritance Virtual base classes 2 Department of CSE, BUET

B ASE C LASS A CCESS C ONTROL class derived-class-name : access base- class-name { … }; Here access is one of three keywords public private protected Use of access is optional It is private by default if the derived class is a class It is public by default if the derived class is a struct 3 Department of CSE, BUET

U SING P ROTECTED M EMBERS Cannot be directly accessed by non-related classes and functions But can be directly accessed by the derived classes Can also be used with structures 4 Department of CSE, BUET

V ISIBILITY OF BASE CLASS MEMBERS IN DERIVED CLASS Member access specifier in base class Member visibility in derived class Type of Inheritance PrivateProtectedPublic PrivateNot Inherited ProtectedPrivateProtected PublicPrivateProtectedPublic Department of CSE, BUET 5  When a class (derived) inherits from another (base) class, the visibility of the members of the base class in the derived class is as follows.

C ONSTRUCTORS, D ESTRUCTORS, AND I NHERITANCE Both base class and derived class can have constructors and destructors. Constructor functions are executed in the order of derivation. Destructor functions are executed in the reverse order of derivation. While working with an object of a derived class, the base class constructor and destructor are always executed no matter how the inheritance was done (private, protected or public). 6 Department of CSE, BUET

C ONSTRUCTORS, D ESTRUCTORS, AND I NHERITANCE ( CONTD.) Department of CSE, BUET 7 class base { public: base() { cout << “Constructing base class\n”; } ~base() { cout << “Destructing base class\n”; } }; class derived : public base { public: derived() { cout << “Constructing derived class\n”; } ~derived() { cout << “Destructing derived class\n”; } }; void main() { derived obj; } Output: Constructing base class Constructing derived class Destructing derived class Destructing base class

C ONSTRUCTORS, D ESTRUCTORS, AND I NHERITANCE ( CONTD.) If a base class constructor takes parameters then it is the responsibility of the derived class constructor(s) to collect them and pass them to the base class constructor using the following syntax - derived-constructor(arg-list) : base(arg-list) { … } Here “base” is the name of the base class It is permissible for both the derived class and the base class to use the same argument. It is also possible for the derived class to ignore all arguments and just pass them along to the base class. 8 Department of CSE, BUET

C ONSTRUCTORS, D ESTRUCTORS, AND I NHERITANCE ( CONTD.) Department of CSE, BUET 9 class MyBase { public: int x; MyBase(int m) { x = m; } }; class MyDerived : public MyBase { public: int y; MyDerived() : MyBase(0) { y = 0; } MyDerived(int a) : MyBase(a) { y = 0; } MyDerived(int a, int b) : MyBase(a) { y = b; } }; void main() { MyDerived o1; // x = 0, y = 0 MyDerived o2(5); // x = 5, y = 0 MyDerived o3(6, 7); // x = 6, y = 7 } As “MyBase” does not have a default (no argument) constructor, every constructor of “MyDerived” must pass the parameters required by the “MyBase” constructor.

M ULTIPLE I NHERITANCE A derived class can inherit more than one base class in two ways. Option-1: By a chain of inheritance b1 -> d1 -> dd1 -> ddd1 -> … Here b1 is an indirect base class of both dd1 and ddd1 Constructors are executed in the order of inheritance Destructors are executed in the reverse order Option-2: By directly inheriting more than one base class class d1 : access b1, access b2, …, access bN { … } Constructors are executed in the order, left to right, that the base classes are specified Destructors are executed in the reverse order 10 Department of CSE, BUET

M ULTIPLE I NHERITANCE ( CONTD.) Department of CSE, BUET 11 Option - 1Option - 2 b1 d1 ddd1 dd1 b1b2b3 d1

V IRTUAL B ASE C LASSES Consider the situation shown. Two copies of Base are included in D3. This causes ambiguity when a member of Base is directly used by D3. 12 Department of CSE, BUET Base D1D2 D3

V IRTUAL B ASE C LASSES ( CONTD.) Department of CSE, BUET 13 class Base { public: int i; }; class D1 : public Base { public: int j; }; class D2 : public Base { public: int k; }; class D3 : public D1, public D2 { // contains two copies of ‘i’ }; void main() { D3 obj; obj.i = 10; // ambiguous, compiler error obj.j = 20; // no problem obj.k = 30; // no problem obj.D1::i = 100; // no problem obj.D2::i = 200; // no problem }

V IRTUAL B ASE C LASSES ( CONTD.) Department of CSE, BUET 14 class Base { public: int i; }; class D1 : virtual public Base { public: int j; }; // activity of D1 not affected class D2 : virtual public Base { public: int k; }; // activity of D2 not affected class D3 : public D1, public D2 { // contains only one copy of ‘i’ }; // no change in this class definition void main() { D3 obj; obj.i = 10; // no problem obj.j = 20; // no problem obj.k = 30; // no problem obj.D1::i = 100; // no problem, overwrites ‘10’ obj.D2::i = 200; // no problem, overwrites ‘100’ }

L ECTURE C ONTENTS Teach Yourself C++ Chapter 7 (Full, with exercise) Study the examples from the book carefully 15 Department of CSE, BUET