Inheritance in C++ Inheritance Protected Section

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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
I NHERITANCE Chapter 7 Department of CSE, BUET 1.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
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 Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
Inheritance, Polymorphism, and Virtual Functions
OOP Languages: Java vs C++
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Inheritance using Java
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.
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
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.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
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.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Constructor in Inheritance. 2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
1 Chapter 4: Another way to define a class Inheritance..!!
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
External Scope CECS 277 Mimi Opkins.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes (Part 1) Lecture 3
Inheritance CMSC 202, Version 4/02.
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Inheritance II CMSC 202.
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Understanding Inheritance
Learning Objectives Inheritance Virtual Function.
Simple Classes in C# CSCI 293 September 12, 2005.
Abstract Classes AKEEL AHMED.
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
CLASS DEFINITION (FIELDS)
Inheritance:Concept of Re-usability
Fundaments of Game Design
Chapter 11: Inheritance and Composition
NAME 436.
Inheritance -I.
COP 3330 Object-oriented Programming in C++
C++ Polymorphism Reference and pointer implicit type casting
Constructors & Destructors
C++ Object Oriented 1.
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

Inheritance in C++ Inheritance Protected Section Constructors and Destructors with inheritance Multiple Inheritance Function overriding

Inheritance The inheriting class (commonly referred to as the derived class) automatically gets the methods and data fields that are inheritable from the inherited class (commonly referred to as the base class) in the of the class definition that is being inherited from. The private functions and data do not get inherited. Example) class savings_account : public account { more stuff }

Public and Private inheritance Public inheritance class savings_account : public account { more stuff } Public elements get inherited in the public area Private Inheritance class savings_account : private account Public elements get inherited into the private area Note: Inheritance can never promote elements, only demote.

Protected Section Suppose you wanted to have an element that needs to be private however you would like an inheriting class to be able to inherit it. That is, you would like to have a protection level that is “private but inheritable.” The section “Protected” was introduced for just that reason. Public: accessible to the outside world Protected: can be inherited but not accessible outside the class Private: can not be inherited and not accessible outside the class

Inheritance hierarchy Base class member access specifier Type of Inheritance   Public Protected Private Public Area Public in derived class. Can be accessed directly by any non-static member function, friend functions and non- member functions Protected in derived class Can be accessed directly by all non-static member functions and friend functions Private in derived class Can be accessed directly by all non-static member functions and friend functions Protected Area Can be accessed directly by all non-static member functions and friend functions Protected in derived class. Can be accessed directly by all non-static member functions and friend functions. Can be accessed directly by all non-static member functions and friend functions. Private Area Hidden in the derived class Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class Can be accessed by non- static member functions and friend functions through public or protected member functions of the base class Can be accessed by non- static member functions and friend functions through public or protected member functions of the base class

Constructors and Destructors Constructors are called in the order of Base, then Derived The first line of a derived class constructor must be a call to the base class’s constructor. A derived class constructor always calls the constructor for its base class first to initialize the derived class’ base- class members. If the derived-class constructor is omitted, the derived class’ default constructor calls the base-class’ default constructor. Destructors are called in reverse order of construction calls, so a derived class destructor is called before its base-class destructor.

Example: Constructors/Destructors class BaseClass { private: public: B b, BaseClass(); ~BaseClass(); } class DerivedClass : public BaseClass D d; DerivedClass(); ~DerivedClass();

Example continued When an object of the type DerivedClass is constructed: The order of constructors is B, BaseClass, D, DerivedClass The order of destructors is the reverse ~DerivedClass, ~D, ~BaseClass, ~B

Multiple Inheritance Structure

Multiple Inheritance Declaration Consider the following multiple inheritance class definition class clock-radio : public clock, public radio { more clock-radio stuff } Consider the following questions

Multiple Inheritance Example Questions How many copies of the base class will “Electronics” would a clock-radio get? Suppose clock has a member function void soundAlarm() which sounds a buzzer for 1 minute and Radio has a member function void play(float station, int volume) which plays the radio. Can clock-radio have a member function also with the signature void soundAlarm() which calls the Radio’s play function. Suppose clock and radio both had a function with the same exact signature, say void powerUp(), and you called clock-radio’s powerUp(), what would happen?

Order of Declaration The order in which member objects are constructed is the order in which those objects are declared within the class definition. The order in which the member initializations are listed does not affect order of constructions.

Order based on inheritance In inheritance, base-class constructors are called in the order in which inheritance is specified in the derived class definition. The order in which the base-class constructors are specified in the derived-class member initializer list does not affect the order of construction.