Inheritance Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.1-7.2.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
PZ06A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06A - Inheritance Programming Language Design and Implementation.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
OOP Languages: Java vs C++
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 12: Adding Functionality to Your Classes.
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.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 CS2104- ADT/Inheritance Lecturer: Dr. Abhik Roychoudhury School of Computing Reading : Chapter 7.1, 7.2 of textbook.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 10 Inheritance and Polymorphism
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Structure of programming languages OOP. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Structure of programming languages OOP. Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Abstract Data Types and Encapsulation Concepts
7. Inheritance and Polymorphism
Review: Two Programming Paradigms
Inheritance Programming Language Design and Implementation
PZ05A - Abstract data types
Java Programming Language
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inheritance Programming Language Design and Implementation
Abstract data types Programming Language Design and Implementation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inheritance Programming Language Design and Implementation
Inheritance Programming Language Design and Implementation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
C++ Object Oriented 1.
Presentation transcript:

Inheritance Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section

Inheritance 2 Encapsulated data types Remember from previously: An ADT with: Type with set of values Set of operations with signatures Representation - component structure of data type Only the type name and operations are visible outside of the defining object. Example: StudentRecord is type Externally visible: void SetName(StudentRecord, Name) name GetName(StudentRecord) Internal to module: char Name[20]; float GPA; char Address[50]; CourseType Schedule[10];

Inheritance 3 Implementation of encapsulation Usual Implementation in Ada: package RationalNumber is type rational is record -- User defined type num, den: integer end record; procedure mult(x in rational; -- Abstract operation y in rational; z out rational); end package; package body RationalNumber is -- Encapsulation procedure mult(x in rational; y in rational; z out rational) begin z.num := x.num * y.num; z.den := x.den * y.den; end; end package;

Inheritance 4 Use of encapsulated data Usual use of encapsulated RationalNumber: Example: In main procedure do: var A, B, C: rational; A.num := 7;  Should be illegal A.den := 1; Mult(A, B, C); No enforcement of encapsulation Any procedure has access to components of type rational. Can manipulate A.num and A.den without using procedures in package RationalNumber. Let's look at alternative model to enforce encapsulation.

Inheritance 5 Private types package RationalNumber is type rational is private; -- User defined type procedure mult(x in rational; -- Abstract operation y in rational; z out rational); private type rational is record -- User defined type num, den: integer end record; end package; package body RationalNumber is -- Same as before procedure mult(x in rational; y in rational; z out rational) begin z.num := x.num * y.num; z.den := x.den * y.den; end; end package;

Inheritance 6 Private types add protection But now: var A: rational; A.num := 7; -- Now illegal. Private blocks use of num and den outside of package RationalNumber. What is role of private?  Any declarations in private part is not visible outside of package What is difference in semantics of rational? What is difference in implementation of rational? This solution encapsulates and hides implementation details of rational.

Inheritance 7 C++ RationalNumber example C++ creates objects of a user defined class.  Data storage  Set of operations Type rational can be specified in C++ as: class rational{ public: void mult( rational x; rational y) { num = x.num * y.num; den = x.den * y.den;} protected: int num; int den } rational A, B, C; A.mult(B,C)  invoke encapsulated function A.num = B.num * C.num  Illegal. No access to num and den

Inheritance 8 Storage for C++ classes Visibility of objects:  public: globally known  private: locally known only  protected -- provides for inheritance

Inheritance 9 Inheritance provides for passing information from one data object to another automatically It provides a form of data scope similar to syntactic scope. Inheritance through data in object oriented languages is explicit through derived types. Static scope (above) - Names are known implicitly through nested procedure names

Inheritance 10 C++ derived classes Consider C++ class rational discussed earlier: class complex: rational { public: void mult( complex x; complex y); { realpt.mult(x.realpt,y.realpt)- imagpt.mult(x.imagpt,y.imagpt)... void initial(complex x) {x.realpt.num = 0; x.realpt.den = 1 } // complex inherits rational components. private: rational realpt; rational imagpt }... complex M, N, P; M.mult(N,P)

Inheritance 11 Power of inheritance class rational { public: mult(...) {... } protected: error(...) {... }... private:... } class complex:rational { public: mult(...) {... } private:... } complex X; Function error is passed (inherited) to class complex, so X.error is a valid function call. Any derived class can invoke error and a legal function will be executed. But what if we want error to print out the type of its argument? (i.e., want to know if error occurred in a rational or complex data?)

Inheritance 12 Power of inheritance (continued) Inheritance is normally a static property: Function error in class complex is known by compiler to be within class rational. x.error  compiler knows where the error function is. So how can rational::error know where it was invoked, as either rational::error or complex::error? One way - Use function argument: error('rational') or error('complex') Alternative: Use of virtual functions

Inheritance 13 Virtual functions Base class: class rational { error() { cout << name() << endl; } string name() { return “Rational”;}... } Derived class: class complex: rational { string name() { return “Complex”;}... } But if error is called, Rational is always printed since the call rational::name is compiled into class rational for the call in the error function. But if name is defined as: virtual string name() { return “Rational”;} then name() is defined as a virtual function and the function name in the current object is invoked when name() is called in rational::error.

Inheritance 14 Implementing virtual functions Virtual functions imply a runtime descriptor with a location of object rational A; complex B; A.error()  error will call name() in rational B.error()  error will call name() in complex

Inheritance 15 Example of Dynamic Binding class shape { public: virtual void area() = 0; …} class triangle: shape{area() {…}} class square: shape{area() {…}} class circle: shape{area() {…}} void foo(shape polygon){ … data = polygon.area; …} What implementation of area is actually called?

Inheritance 16 Additional C++ inheritance attributes Problem: Want to access data from the class object. That is, in rational::error, print values for num and den of each component of class object  Can use additional virtual functions  this pointers: *this.counter Accesses counter object in actual object passed to error.  A.error(string X) compiled as: rational::error(&A, X) That is, passes class object A as well as string X.

Inheritance 17 Friend classes Friend classes: Strict inheritance sometimes difficult to do (i.e., Too hard to do it right so find a way around it!) class thing {... MyFcn(complex A) {... A.realpt } But A.realpt is private data Fudge solution by adding: friend class thing in complex class Allows thing access to hidden components and avoid strict inheritance hierarchy of C++ Studies have shown this is most error prone feature of C++; redesign class hierarchy instead.

Inheritance 18 Mixin inheritance Assume want to add feature X to both class A and B: Usual way is to redefine both classes. Mixin inheritance: Have definition which is addition to base class (Not part of C++) For example, the following is possible syntax: featureX mixin {int valcounter}  Add field to object newclassA class A mod featureX; newclassB class B mod featureX; Can get similar effect with multiple inheritance: class newclassA:A,featureX {... } class newclassB:B,featureX {... }

Inheritance 19 Inheritance principles 1. Specialization: Usual form of inheritance: Checking inherits properties of Account. Opposite is generalization: Account is more general than Checking. 2. Decomposition: Breaking an encapsulated object into parts. A rational object is a num and a den. Opposite concept is aggregation. 3. Instantiation: Creation of instances of an object: rational A, B, C;  Represents 3 instantiations of object rational. 4. Individualization: Related to specialization. Separate objects by function, not structure. A stack and a set can both be an array and and an index pointer, but functionality different. Opposite is grouping.

Inheritance 20 Overloading Two operators with the same name but different signatures are said to be overloaded. Older languages allowed overloading on built-in functions, e.g., real+real is different from integer+integer is different from integer+real is different from real+integer C++ overloading; cout << the << operator is given arguments cout and 123. cout << ``abc'' - the << operator is given arguments cout and ``abc''.

Inheritance 21 Overloading (continued) Non-primitive overloading: myfunction (int a, int b) {... } myfunction (int a) {... } Each signature differs, so a different function is called. Note that this is a static property determined by the compiler. What about the following: myfunction (int a, int b=7) {... } myfunction (char a) {... }  myfunction(3)?  myfunction('a')?

Inheritance 22 Overload resolution In C++: Given X(args) {...} 1. If signatures match exactly, second is a redeclaration of the first. 2. If the arguments match exactly, but the return types do not, the second is an error. 3. If the arguments do not match exactly, the second is an overloaded definition. Related concepts: Coercion: Often implemented as part of built-in overloaded operators (already discussed) Polymorphism: Types are defined by a parameterized argument. (e.g., type mystring = string(N: integer);) proc MyFunction(X: mystring(N)) {...} call Myfunction(Mydata(17)); We will discuss polymorphism in more detail later.