C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 ©2002-07 McQuain Generalization versus Abstraction Abstraction:simplify.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Polymorphism CS351 – Programming Paradigms.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
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.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Inheritance OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Generalization versus Abstraction.
Object-Oriented Programming Chapter Chapter
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCE 240 – Intro to Software Engineering Lecture 3.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Lecture 12 Inheritance.
Final and Abstract Classes
Inheritance and Polymorphism
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Object Oriented Analysis and Design
Understanding Inheritance
Extending Classes.
Java Programming Language
Java Programming Language
Object-Oriented PHP (1)
Final and Abstract Classes
Presentation transcript:

C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify the description of something to those aspects that are relevant to the problem at hand. Generalization:find and exploit the common properties in a set of abstractions. hierarchy polymorphism genericity patterns

C++ Inheritance Data Structures & OO Development I 2 Computer Science Dept Va Tech June 2007 © McQuain Four-Fold Path to Generalization Hierarchy Exploitation of an “is-a-kind-of” relationship among kinds of entities to allow related kinds to share properties and implementation. Polymorphism Exploitation of logical or structural similarities of organization to allow related kinds to exhibit similar behaviors via similar interfaces. Genericity Exploitation of logical or structural similarities of organization to produce generic objects. Patterns Exploitation of common relationship scenarios among objects. (e.g., client/server system)

C++ Inheritance Data Structures & OO Development I 3 Computer Science Dept Va Tech June 2007 © McQuain Hierarchy Represented by generalize/specialize graph Based on “is-a-kind-of” relationship E.g., a Manager is an Employee; a robin is a bird, and so is an ostrich. Is a form of knowledge representation – a “taxonomy” structures knowledge about nearby entities. Extendable without redefining everything E.g., knowing a robin is a bird tells me that a robin has certain properties and behaviors, assuming I know what a “bird” is. Specialization can be added to proper subset of hierarchy

C++ Inheritance Data Structures & OO Development I 4 Computer Science Dept Va Tech June 2007 © McQuain Taxonomy A generalization/specialization hierarchy based on “is-a-kind-of” relationships: Person Name Address Employee Name Address ID Department Student Name Address ID Major Level more general less details more specialized more details

C++ Inheritance Data Structures & OO Development I 5 Computer Science Dept Va Tech June 2007 © McQuain Inheritance Terminology – Base type or class (a.k.a. superclass, parent type) – Derived type or class (a.k.a. subclass, subtype, child type) Important Aspects – Programming: implement efficiently a set of related classes (mechanical) – Design: organize coherently the concepts in an application domain (conceptual) – Software Engineering: design for flexibility and extensibility in software systems (logical)

C++ Inheritance Data Structures & OO Development I 6 Computer Science Dept Va Tech June 2007 © McQuain andGate Class w/o Inheritance class andGate { private: string mName; vector In; Wire* Out; unsigned int mFanIn; unsigned int mInputsConnected; virtual bool Evaluate() const; public: andGate(const string& ID = "anon", unsigned int fanIn = 0); unsigned int fanIn() const; string Name() const; virtual bool Valid() const; virtual bool addIn(Wire* const Input = NULL); virtual bool addOut(Wire* const Output = NULL); virtual void Act(Wire* const Source); virtual ~andGate(); };

C++ Inheritance Data Structures & OO Development I 7 Computer Science Dept Va Tech June 2007 © McQuain orGate Class w/o Inheritance class orGate { private: string mName; vector In; Wire* Out; unsigned int mFanIn; unsigned int mInputsConnected; virtual bool Evaluate() const; public: orGate(const string& ID = "anon", unsigned int fanIn = 0); unsigned int fanIn() const; string Name() const; virtual bool Valid() const; virtual bool addIn(Wire* const Input = NULL); virtual bool addOut(Wire* const Output = NULL); virtual void Act(Wire* const Source); virtual ~orGate(); };

C++ Inheritance Data Structures & OO Development I 8 Computer Science Dept Va Tech June 2007 © McQuain What is Common? Both classes contain the data members string mName; vector In; Wire* Out; unsigned int mFanIn; unsigned int mInputsConnected; and member functions which will have identical implementations unsigned int fanIn() const; string Name() const; virtual bool Valid() const; virtual bool addIn(Wire* const Input = NULL); virtual bool addOut(Wire* const Output = NULL); virtual void Act(Wire* const Source); From a coding perspective, this is somewhat wasteful because we must duplicate the declarations and implementations in each class. From a S/E perspective, this is undesirable since we must effectively maintain two copies of (logically) identical code.

C++ Inheritance Data Structures & OO Development I 9 Computer Science Dept Va Tech June 2007 © McQuain What is Different? The following member function would have different implementations in the two classes virtual bool Evaluate() const; This would evidently be the only difference between the two classes!

C++ Inheritance Data Structures & OO Development I 10 Computer Science Dept Va Tech June 2007 © McQuain What Do We Want? Simply put, we want to exploit the fact that andGate and orGate both are "gates". That is, each shares certain data and function members which logically belong to a more general (more basic) type which we will call a Gate. We would prefer to NOT duplicate implementation but rather to specify that each of the more specific types will automatically have certain features (data and functions) that are derived from (or inherited from) the general type. Question: are there any attributes or operations in the overlap that we don't want to include in the base type Gate ?

C++ Inheritance Data Structures & OO Development I 11 Computer Science Dept Va Tech June 2007 © McQuain How Do We Get It? By employing the C++ inheritance mechanism… Inheritance in C++ is NOT simple, either syntactically or semantically. We will examine a simple case first (based on the previous discussion) and defer explicit coverage of many specifics until later. Inheritance in C++ involves specifying in the declaration of one class that it is derived from (or inherits from) another class. Inheritance may be public or private (or protected). At this time we will consider only public inheritance. It is also possible for a class to be derived from more than one (unrelated) base class. Such multiple inheritance will be discussed later…

C++ Inheritance Data Structures & OO Development I 12 Computer Science Dept Va Tech June 2007 © McQuain The Base Class: Gate class Gate { private: string mName; vector In; Wire* Out; unsigned int mFanIn; unsigned int mInputsConnected; virtual bool Evaluate() const = 0; public: Gate(const string& ID = "anon", unsigned int fanIn = 0); unsigned int fanIn() const; string Name() const; virtual bool Valid() const; virtual bool addIn(Wire* const Input = NULL); virtual bool addOut(Wire* const Output = NULL); virtual void Act(Wire* const Source); virtual ~Gate(); }; Having identified the common elements shared by both classes, we specify a suitable base class:

C++ Inheritance Data Structures & OO Development I 13 Computer Science Dept Va Tech June 2007 © McQuain A Derived Class: andGate class andGate : public Gate { protected: virtual bool Evaluate() const; public: andGate(const string& ID = "anon", unsigned int fanIn = 0); virtual ~andGate(); }; Specify base classSpecify public inheritance

C++ Inheritance Data Structures & OO Development I 14 Computer Science Dept Va Tech June 2007 © McQuain Constructing a Derived Type Object When an object of a derived type is declared, the default constructor for the base type will be invoked BEFORE the body of the constructor for the derived type is executed (unless an alternative action is specified…). Alternatively: andGate::andGate(const string& ID, unsigned int fanIn) : Gate(ID, fanIn) { } Alternatively, we can also implement an entirely specialized constructor in the derived type.

C++ Inheritance Data Structures & OO Development I 15 Computer Science Dept Va Tech June 2007 © McQuain Derived Class Member Access Problem bool andGate::Evaluate() const { bool Value = In[0]->Output(); for (unsigned int pos = 1; pos < mFanIn; pos++) { Value = Value && In[pos]->Output(); } return Value; } Error: cannot access private member declared in class Gate. Objects of a derived type inherit the data members and function members of the base type. However, the derived object may not directly access the private members of the base type:

C++ Inheritance Data Structures & OO Development I 16 Computer Science Dept Va Tech June 2007 © McQuain Protected Access The restriction on a derived type's access seems to pose a dilemma: -Having the base type use only public members is certainly unacceptable. -Having the derived class use the public interface of the base class to access and/or modify private base class data members is clumsy. C++ provides a middle-ground level of access control that allows derived types to access base members which are still restricted from access by unrelated types. The keyword protected may be used to specify the access restrictions for a class member: class Gate { protected: string mName; vector In; Wire* Out; unsigned int mFanIn; unsigned int mInputsConnected;... };

C++ Inheritance Data Structures & OO Development I 17 Computer Science Dept Va Tech June 2007 © McQuain A Sibling Class class orGate : public Gate { protected: virtual bool Evaluate() const; public: orGate(const string& ID = "anon", unsigned int fanIn = 0); virtual ~orGate(); }; Note that, so far as the language is concerned, andGate and orGate enjoy no special relationship as a result of sharing the same base class.

C++ Inheritance Data Structures & OO Development I 18 Computer Science Dept Va Tech June 2007 © McQuain Using Objects of Derived Classes NEED AN EXAMPLE HERE!! Objects of a derived class may be declared and used in the usual way:

C++ Inheritance Data Structures & OO Development I 19 Computer Science Dept Va Tech June 2007 © McQuain Extending the Hierarchy There's no restriction on how many levels of inheritance can be designed, nor is there any reason we can't mix inheritance with association and/or aggregation. Actually, we would probably want a more complex hierarchy: GateOneInputGateMultiInputGateandGateorGateSplitternotGate

C++ Inheritance Data Structures & OO Development I 20 Computer Science Dept Va Tech June 2007 © McQuain Overriding a Base Class Member Function string Gate::Name() const { return mName; } In some cases, the base class may contain an implemented member function which is not entirely adequate: A derived type may want a more specialized version: string andGate::Name() const { return "AND" + mName; } When Name() is called on an andGate object, this version, not the base version will be executed.

C++ Inheritance Data Structures & OO Development I 21 Computer Science Dept Va Tech June 2007 © McQuain Copying a Derived Object to a Base Object It is legal to assign a derived type object to a base type object, however: When a derived object is assigned to a base target, only the data members appropriate to the target type are copied. This is known as slicing. Actually, this would seem to be inevitable since the base object would not have any place to store data members that were added in the derived type.

C++ Inheritance Data Structures & OO Development I 22 Computer Science Dept Va Tech June 2007 © McQuain Assigning Base Type to Derived Type By default, a base type object may not be assigned to a derived type object. After all, the derived type may declare "extra" data members that would not receive appropriate values. It's possible to legalize this with the right constructor overloading (later).

C++ Inheritance Data Structures & OO Development I 23 Computer Science Dept Va Tech June 2007 © McQuain Parameter Passing Issues The rules are essentially the same in four situations: -when passing a derived object by value as a parameter. -when returning a derived object by value -when initializing a base object with a derived object -when assigning a derived object to a base object A derived type may be copied when a base type is targeted — however, slicing will occur. A base type may not be copied when a derived type is targeted — unless a suitable derived type constructor is provided to legalize the conversion.