Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

Slides:



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

1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
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.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
C++ Inheritance Systems Programming.
Inheritance CT Introduction 2  Inheritance  Software reusability  Create new class from existing class  Absorb existing class’s data and behaviors.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
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.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 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.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CLASS INHERITANCE WEEK 7 Chapter 20 CS SENEM KUMOVA METIN1.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
計算機程式 第十三單元 Inheritance 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,
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.
Object Oriented Programming
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Inheritance CT Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
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.
Inheritance CT Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance CT1513.
Object-Oriented Programming: Inheritance
Learning Objectives Inheritance Virtual Function.
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
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
Inheritance CT1513.
Object-Oriented Programming: Inheritance
Chapter 19 - Inheritance Outline 19.1 Introduction
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I

12.1 Introduction Inheritance ◦ Software reusability ◦ Create new class from existing class  Absorb existing class ’ s data and behaviors  Enhance with new capabilities ◦ Derived class inherits from base class  Derived class (衍生類別)  More specialized group of objects than the base class  Behaviors inherited from base class  Additional behaviors

12.1 Introduction Class hierarchy ◦ Direct base class  Inherited explicitly (one level up hierarchy) ◦ Indirect base class  Inherited two or more levels up hierarchy ◦ Single inheritance  Inherits from one base class ◦ Multiple inheritance  Inherits from multiple base classes  Base classes possibly unrelated  More details in chapter 24

Fig | Inheritance hierarchy for university CommunityMember s.

12.1 Introduction Focus on commonalities among objects in system “ is-a ” vs. “ has-a ” ◦ “ is-a ”  Inheritance  Derived class object can be treated as base class object  Example: Car is a vehicle  Vehicle properties/behaviors also apply to a car ◦ “ has-a ”  Composition  Object contains one or more objects of other classes as members  Example: Car has a steering wheel

12.1 Introduction Three types of inheritance ◦ public ◦ private ◦ protected

Software Engineering Observation 12.1 Member functions of a derived class cannot directly access private members of the base class. ◦ This is for ensuring information hiding.

12.2 Base Classes and Derived Classes Base classes and derived classes ◦ Object of one class “ is an ” object of another class  Example: Rectangle is quadrilateral  Class Rectangle inherits from class Quadrilateral  Quadrilateral is the base class  Rectangle is the derived class ◦ Base class typically represents larger set of objects than derived classes  Example:  Base class: Vehicle  Includes cars, trucks, boats, bicycles, etc.  Derived class: Car  Smaller, more-specific subset of vehicles

Fig | Inheritance hierarchy for Shapes.

12.2 Base Classes and Derived Classes public inheritance ◦ Specify with:  Class TwoDimensionalShape inherits from class Shape ◦ Base class private members  not accessible directly  but still inherited  Manipulated through inherited public member functions ◦ Base class public and protected members  Inherited with original member access ◦ friend functions  Not inherited Class TwoDimensionalShape : public Shape { … };

12.3 protected Members protected access ◦ Intermediate level of protection between public and private ◦ protected members are accessible to  Base class members  Base class friend s  Derived class members  Derived class friend s Derived-class members ◦ Refer to public and protected members of base class  Simply use member names ◦ Redefined base class members can be accessed by using base-class name and binary scope resolution operator ( :: )

Example class Student { friend ostream &operator<< (ostream &out, const Student s); public: Student(string, string); ~Student(); string GetName(); protected: string name, id; private: … } class GraduateStudent : public Student { public: GraduateStudent(string, string); ~ GraduateStudent(); string GetSupervisor(); protected: string supervisor; private: … } class UndergraduateStudent : public Student { public: UndergraduateStudent(string, string); ~ UndergraduateStudent(); string GetPreceptor(); protected: string preceptor; private: … } inherit

Example void main() { UndergraduateStudent stu1(“Joe”, “00001”); string name = stu1.GetName(); cout << stu1 << endl; } Undergraduate 繼承自 Student ,因此承襲其 public 與 protected 成員變數與成 員函式 試說明此行的錯誤。