Derived Classes in C++CS-2303, C-Term 20101 Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Slide 1 Inheritance: Part I. Slide 2 Structuring ‘classes’ class Employee { string firstName; string familyName; … } class Manager { Employee e; list.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Computer Science I Inheritance Professor Evan Korth New York University.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
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.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
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.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 8 More Object Concepts
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
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.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Object Oriented Programming: Inheritance Chapter 9.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
Understanding Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Learning Objectives Inheritance Virtual Function.
MSIS 670 Object-Oriented Software Engineering
Object-Oriented Programming: Inheritance
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming: Inheritance
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Object-Oriented Programming: Inheritance
A Deeper Look at Classes
C++ Object Oriented 1.
Introduction to Classes and Objects
Presentation transcript:

Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel, and from The C++ Programming Language, 3 rd edition, by Bjarne Stroustrup)

Derived Classes in C++CS-2303, C-Term Outline IntroductionIntroduction Base Classes and Derived ClassesBase Classes and Derived Classes Some Examples of Base Class and Derived Class Relationships Constructors and Destructors in Derived Classes Accessing Members of Base and Derived Classes

Derived Classes in C++CS-2303, C-Term Reading Assignment Deitel & Deitel, Chapter 23 Note:– Like most of the rest of the book, this chapter is written mostly for someone who has never heard of classes, subclasses, and object-oriented programming Especially someone who has never seen Java

Derived Classes in C++CS-2303, C-Term History The whole notion of classes, subclasses, and inheritance came from Simula 67 A generalization of notion of records (now known as structs ) from Algol- and Pascal-like languages in 1960s and early 70s –A (very nice) programming language developed in Norway for implementing large simulation applications

Derived Classes in C++CS-2303, C-Term Terminology Inheritance is a form of software reuse where a new class is created to –absorb an existing class’s data and behaviors, and –enhance them with new capabilities The new class, the derived class, inherits the members of the existing class, known as the base class

Derived Classes in C++CS-2303, C-Term Terminology (continued) C++ –Derived Class –Base Class Java –Subclass –Superclass

Derived Classes in C++CS-2303, C-Term Introduction (continued) A direct base class is the base class from which a derived class explicitly inherits. An indirect base class is inherited from two or more levels up in the class hierarchy. In single inheritance, a class is derived from one base class. With multiple inheritance, a derived class inherits from multiple base classes.

Derived Classes in C++CS-2303, C-Term Class Hierarchy Deitel & Deitel, Fig Members of a university community

Derived Classes in C++CS-2303, C-Term Three Types of Inheritance in C++ public:: every object of a derived class is also an object of its base class Note, base-class objects are NOT objects of their derived classes. private:: is essentially an alternative to composition I.e., derived class members not accessible from outside protected:: is rarely used

Derived Classes in C++CS-2303, C-Term Example — public class class Employee { string givenName, familyName; date hiringDate; short department;... }; class Manager: public Employee { set group; short level;... } All Managers are Employees but not all Employees are Managers

Derived Classes in C++CS-2303, C-Term Example — public class class Employee { string givenName, familyName; date hiringDate; short department;... }; class Manager: public Employee { set group; short level;... } Note: this is an example of the use of a container class

Derived Classes in C++CS-2303, C-Term Important Note Member functions of derived class cannot directly access private members of base class Example:– –Manager member functions in previous example cannot read manager’s own name! –Because data members of a class are by default private

Derived Classes in C++CS-2303, C-Term protected – New Access Specifier class Employee { protected: string givenName, familyName; date hiringDate; short department;... }; class Manager: public Employee { set group; short level;... }

Derived Classes in C++CS-2303, C-Term protected (continued) A base class’s protected members can be accessed by –members and friends of the base class, and –members and friends of any class derived from the base class. Derived-class member functions can refer to public and protected members of the base class. –By simply using their names

Derived Classes in C++CS-2303, C-Term Difference between Inheritance and Composition is-a relationship:: inheritance –e.g., derived class object, car, is an object of the base class vehicle –e.g., derived class object, Manager, is an object of the base class Employee has-a relationship:: composition –e.g., a TreeNode object has (i.e., contains) a member object of type string

Derived Classes in C++CS-2303, C-Term Base Classes and Derived Classes Base classes typically represent larger sets of objects than derived classes Example –Base class: vehicle Includes cars, trucks, boats, bicycles, etc. –Derived class: car a smaller, more-specific subset of vehicles

Derived Classes in C++CS-2303, C-Term Base Classes and Derived Classes (continued) I.e., base classes have more objects But fewer data and function members Derived classes have only subsets of the objects Hence the term subclass But a derived class has more data and function members

Derived Classes in C++CS-2303, C-Term Questions?

Derived Classes in C++CS-2303, C-Term Deitel & Deitel §23.4 — Five Examples CommissionEmployee 1.Create a CommissionEmployee class with private data members:– First name, last name, SSN, commission rate, gross sale amount. BasePlusCommissionEmployee 2.Create a BasePlusCommissionEmployee class without inheritance but with private data members:–.First name, last name, SSN, commission rate, gross sale amount, and base salary. Copying code is not code re-use!

Derived Classes in C++CS-2303, C-Term Digression – Code Re-use Fundamental principle of software engineering A body of code is a living, evolving thing –In response to bug fixes, changes in requirements, additional features, new environments, etc. As a practical matter, copies of bodies of code cannot keep up with each other –Divergent bug fixes, different evolving requirements, different development teams, etc. If you really want your hard work to support multiple purposes, applications, requirements, etc. –You really need a way for those purposes to inherit your code rather than copy it.

Derived Classes in C++CS-2303, C-Term Deitel & Deitel §23.4 — Five Examples CommissionEmployee 1.Create a CommissionEmployee class with private data members:– First name, last name, SSN, commission rate, gross sale amount. BasePlusCommissionEmployee 2.Create a BasePlusCommissionEmployee class without inheritance but with private data members:–.First name, last name, SSN, commission rate, gross sale amount, and base salary. Copying code is not code re-use! CommissionEmployee As a one-off project, it is easier to copy and modify CommissionEmployee than to build a derived class! HashTablePA5 Likewise, it is easier to copy and modify the HashTable class of PA5 than it is to build a derived class! PA5 However, PA5 requires a derived class!

Derived Classes in C++CS-2303, C-Term Deitel & Deitel §23.4 — Five Examples (cont.) CommissionEmployee BasePlusCommissionEmployee private 3.Create a CommissionEmployee base class and BasePlusCommissionEmployee inheritance hierarchy with private members. CommissionEmployee BasePlusCommissionEmployee protected 4.Create a CommissionEmployee base class and BasePlusCommissionEmployee inheritance hierarchy with protected members. CommissionEmployee BasePlusCommissionEmployee private public 5.Create a CommissionEmployee base class and BasePlusCommissionEmployee inheritance hierarchy with private members but access through public member functions. If you need to see a detailed example, refer to one of these.

Derived Classes in C++CS-2303, C-Term Constructors and Destructors Constructor:– –Derived class constructor is called to create derived class object –Invokes base class constructor before derived class initializer list & constructor body –… and so on up class hierarchy Destructor:– –Derived class destructor body is executed first –Then destructors of derived class members –And then destructor of base class –… and so on up class hierarchy

Derived Classes in C++CS-2303, C-Term Instantiating Derived-class Object Derived-class constructor invokes base class constructor either –implicitly (via a base-class member initializer) or –explicitly (by calling the base classes default constructor). Base of inheritance hierarchy –The last constructor called in an inheritance chain is at the base of the hierarchy –This constructor is the first constructor body to finish executing

Derived Classes in C++CS-2303, C-Term Software Engineering Observation 23.7 When a program creates a derived-class object, –the derived-class constructor immediately calls the base-class constructor, –the base-class constructor’s body executes, –then the derived class’s member initializers execute, and –finally the derived-class constructor’s body executes This process cascades up the hierarchy if the hierarchy contains more than two levels.

Derived Classes in C++CS-2303, C-Term Constructors and Destructors in Derived Classes Destroying derived-class objects –Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first. Destructor of next base class up hierarchy is called next. This continues up hierarchy until the final base class is reached. –After final base-class destructor, the object is removed from memory. Base-class constructors, destructors, and overloaded assignment operators are not inherited by derived classes.

Derived Classes in C++CS-2303, C-Term Software Engineering Observation 23.8 Suppose that we create an object of a derived class where both the base class and the derived class contain objects of other classes. When an object of that derived class is created, first the constructors for the base class’s member objects execute, then the base-class constructor executes, then the constructors for the derived class’s member objects execute, then the derived class’s constructor executes.

Derived Classes in C++CS-2303, C-Term Software Engineering Observation 23.8 Destructors for derived-class objects are called in the reverse of the order in which their corresponding constructors are called.

Derived Classes in C++CS-2303, C-Term Questions?

Derived Classes in C++CS-2303, C-Term Redefinition of Base Class Members Suppose that base class has a member E.g. void print() Knows only how to print information from base class Define same member in derived class E.g. void print() Knows how to print info for derived class Needs to call base class print() function to print base class info

Derived Classes in C++CS-2303, C-Term Redefinition of Base Class Members (continued) Derived class void print() { // print derived class info BaseClass::print();//prints base info // more derived class stuff } See Deitel & Deitel, Fig Lines General principle: when in derived class scope, if you need to access anything in base class and there is a naming conflict, use '::' scope resolution operator

Derived Classes in C++CS-2303, C-Term Accessing Members of Base and Derived Classes Let B be a base class with public members m and n Let D be a derived class with public members m and p I.e., D redefines member m E.g., print() function of previous example Consider the following:– B objB; D objD; B *ptrB; D *ptrD;

Derived Classes in C++CS-2303, C-Term Accessing Members (continued) objB.m and objB.n are both legal –access members of base class objD.m and objD.p are both legal –access members of derived class objD.n is also legal –accesses member of base class! objB.p is not legal –Class B has no member named p !

Derived Classes in C++CS-2303, C-Term Accessing Members (continued) ptrB = new B(); ptrB -> m and ptrB -> n are both legal –access members of base class ptrD = new D(); ptrD -> m and ptrD -> p are both legal –access members of derived class

Derived Classes in C++CS-2303, C-Term Accessing Members (continued) ptrB = ptrD; –ptrB now points to a member of the derived class –which by definition is a member of the base class! ptrB -> m and ptrB -> n are both legal –access members of base class object –Even though object is a member of derived class, with its own redefined member m !

Derived Classes in C++CS-2303, C-Term Accessing Members (continued) ptrB = ptrD; ptrB -> p is not legal –Because ptrB only knows about B ’s members! Rule:– –So far, which member to access depends entirely on the type of the accessing pointer (or accessing object) –To bend that rule, need polymorphism and virtual members.

Derived Classes in C++CS-2303, C-Term Questions?

Derived Classes in C++CS-2303, C-Term Loose End:– Three Types of Inheritance in C++ public:: every object of a derived class is also an object of its base class Note, base-class objects are NOT objects of their derived classes. private:: is essentially an alternative to composition I.e., derived class members not accessible from outside protected:: is rarely used

Derived Classes in C++CS-2303, C-Term Let D be derived from B If B is a private base class:– I.e., class D: private B {} Public and protected members of B can only be used by member and friend functions of D. Only members and friends of D can convert D* into B* I.e., outside of member and friend functions of D Dptr -> p is not allowed (where p is a member of B ) Bptr = Dptr is not allowed

Derived Classes in C++CS-2303, C-Term Let D be derived from B (continued) If B is a protected base:– I.e., class D: protected B {} Public and protected members of B can only be used by member and friend functions of D and also by member and friend functions of classes derived from D Only members and friends of D and its derived classes can convert D* into B* I.e., outside of member and friend functions of D or its derived classes Dptr -> p is not allowed (where p is a member of B ) Bptr = Dptr is not allowed

Derived Classes in C++CS-2303, C-Term Let D be derived from B (continued) If B is a public base:– I.e., class D: public B {} Public members of B can be used by any function Protected members of B can be used by member and friend functions of D and also by member and friend functions of classes derived from D Any function can convert D* into B* I.e., Dptr -> p is allowed (where p is a member of B ) Bptr = Dptr is allowed

Derived Classes in C++CS-2303, C-Term Summary – Inheritance This topic covered the basics of inheritance in C++ There is much, much more to say about inheritance after we cover polymorphism

Derived Classes in C++CS-2303, C-Term Questions?