CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.

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

M The University Of Michigan Andrew M. Morgan EECS Lecture 24 Savitch Ch. 14 Inheritance.
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Java Programing PSC 120 Jeff Schank. Let’s Create a Java Program 1.Open Eclipse 2.Create a project: File -> New -> Java Project 3.Create a package: File.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Static Members, Structures, Enumerations, Generic Classes, Namespaces Learning & Development Team Telerik Software Academy.
C++ Classes & Data Abstraction
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
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.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Learners Support Publications Inheritance: Extending Classes.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Object-Oriented PHP (1)
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Inheritance using Java
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:
Programming in Java CSCI-2220 Object Oriented Programming.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Coming up: Inheritance
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
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.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Written by: Dr. JJ Shepherd
Inheritance AKEEL AHMED.
Inheritance II CMSC 202.
PHP Classes and Objects
Polymorphism & Virtual Functions
Object Oriented Analysis and Design
Object-Oriented Programming: Inheritance
Interfaces.
Object-Oriented Programming: Inheritance
Inheritance:Concept of Re-usability
By Rajanikanth B OOP Concepts By Rajanikanth B
CIS 199 Final Review.
Inheritance in C++ Inheritance Protected Section
Object-Oriented Programming: Inheritance
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes and functions. The class you inherit from is called the base class. The class that inherits from the base class is known as a derived class. By inheriting a class: You can add additional functionality through the derived class. You can add additional data through the derived class. You can modify how a class function behaves. 1

CLASS INHERITANCE Inheritance so that it works properly in all situations requires careful management. Generic form of C++ code inheritance class DerivedClass : public BaseClass {... }; : public BaseClass is the Inheritance code; that is you are inheriting from the base class you will note that the inheritance here is public. 2

CLASS INHERITANCE You can inherit from a class Public derivation Protected derivation Private derivation Base class accessInheritance access specifier publicprotectedprivate public protectedprivate protected private 3

CLASS INHERITANCE A derived object inherits the data members of the base class. (The derived class inherits the base-class implementation.) A derived object inherits the functions of the base class. (The derived class inherits the base-class interface.) Once you have inherited the base class you are now in a position to extend its functionality through the derived class. 4

CLASS INHERITANCE What you need to add A derived class needs its own constructors. A derived class can add additional data members and member functions as needed. A derived class does not have direct access to the private members of the base class; it has to work through the base- class functions. The derived-class constructors have to use the base-class constructors. When a program constructs a derived-class object, it first constructs the base-class object. 5

CLASS INHERITANCE Conceptually, that means the base-class object should be constructed before the program enters the body of the derived-class constructor. Derived-class function can call a public base-class function. void Derived::Init() { Base::Init(); cout << "Something" << endl; } 6

CLASS INHERITANCE C++ uses the member initialiser list syntax to accomplish this. The ‘:’ starts the member initialiser list. It’s executable code, and it calls the BaseClass constructor. DerivedClass::DerivedClass( unsigned int uiR, const char * pc_cStr, bool bFlag ) : BaseClass(pc_cStr, bFlag) { m_uiRating = uiR; } 7

Constructors: Access Considerations What if you omit the member initialiser list? The base-class object must be created first, so if you omit calling a base-class constructor, the program uses the default base-class constructor. These are the key points about constructors for derived classes: The base-class object is constructed first. The derived-class constructor should pass base-class information to a base-class constructor via a member initialiser list. The derived-class constructor should initialise the data members that were added to the derived class. 8

Constructors: Access Considerations The derived-class constructor is responsible for initialising any added data members. A derived-class constructor always calls a base-class constructor. You can use the initialiser list syntax to indicate which base-class constructor to use. Otherwise, the default base-class constructor is used. When an object of a derived class expires, the program first calls the derived-class destructor and then calls the base-class destructor. 9