Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Object-Oriented PHP (1)
7M701 1 Software Engineering Object-oriented Design Sommerville, Ian (2001) Software Engineering, 6 th edition: Chapter 12 )
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Inheritance.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved OOP and.
Object-oriented Programming Concepts
K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Foundations: Language Mechanisms and Primitive OO Concepts Lecture 3: Introduction to OO Modeling E. Kraemer adapted from K. Stirewalt.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Object Oriented Software Development
Inheritance using Java
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
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.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
OO as a language for acm l OO phrase l Mental model of key concepts.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Object Oriented Programming
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Lecture 12 Inheritance.
7. Inheritance and Polymorphism
Review: Two Programming Paradigms
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Week 6 Object-Oriented Programming (2): Polymorphism
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Inheritance -I.
Object-Oriented PHP (1)
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Computer Science II for Majors
Presentation transcript:

Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes adapted from Prof. K Stirewalt

CSE335 2 Topics Software engineering principles and support for them in programming languages. Derived classes and inheritance Advantages of inheritance: Reusing functions Member function overriding

CSE335 3 Software design “body of knowledge” Organized around a set of core principles –Rigor and formality –Separation of concerns –Abstraction –Anticipation of change –Modularity –Generalization –Incrementality Goal: At end of this course, you should be able to apply these correctly in real design contexts. Source: Fundamentals of Software Engineering by Ghezzi, Jazayeri, and Mandrioli

CSE335 4 Core principles and programming languages Core principles heavily influenced the evolution of programming languages –E.g., good support for data abstraction did not exist in older languages, such as Pascal and Fortran –Now it is a staple of all general-purpose languages What is new with object-oriented languages? –More direct support for generalization, abstraction (behavioral), separation of concerns, and incrementality We begin to explore these principles by first looking at new language features that support them...

CSE335 5 Motivation: Employee records Question: Do you see any problems with this design? class Employee { public: string first_name; string last_name; Date hiring_date; short department; }; class Manager { public: Employee emp; list group; short level; }; Example from [Stroustrup’00]

CSE335 6 Motivation: Employee records Questions: –Is it possible for a Manager to have another Manager in his/her group? How would you have to write this code? class Employee { public: string first_name; string last_name; Date hiring_date; short department; }; class Manager { public: Employee emp; list group; short level; }; Example from [Stroustrup’00]

CSE335 7 Motivation: Employee records Questions: –How would you have to write code that prints the first and last names of every employees in a company, including those who are managers? class Employee { public: string first_name; string last_name; Date hiring_date; short department; }; class Manager { public: Employee emp; list group; short level; }; Example from [Stroustrup’00]

CSE335 8 Derived classes Defines new class by extending existing class –Base class Class that is extended Defines function and data members that are inherited by the derived class –Derived class Inherits function and data members from base class May add additional function/data members May also override inherited function members with new methods

CSE335 9 How to declare a derived class class DerivedClassName : kind BaseClassName { public: // New function/data members. protected: // … }; Where: kind  { public, private, protected }

CSE Better design of class Manager class Manager : public Employee { public: list group; short level; }; Terminology: –Manager called the derived class (also subclass) –Employee called the base class (also superclass) –Manager inherits from Employee

CSE Pointers/references & inheritance Pointer to an instance of a derived class can be used as pointer to an instance of its base class Reference to an instance of a derived class can be used as reference to an instance of its base class void foo() { Manager m, *mPtr; Employee e, *ePtr; … ePtr = &m; // OK mPtr = &e; // Error. }

CSE Advantages of inheritance Factor out code that is common in multiple classes –Less code to maintain! –Fix errors once. Reuse functions that operate on base-class instances –Invoke function whose formal parameter is of (reference or pointer) to class C with actual parameter of (reference or pointer to) class derived from C. Represent domain relationships explicitly in code

CSE Example: Class Figure class Figure { public: Figure (unsigned x, unsigned y) : xLocation(x), yLocation(y) {} unsigned getXLocation() const { return xLocation; } unsigned getYLocation() const { return yLocation; } protected: unsigned xLocation; unsigned yLocation; };

CSE Example: Class Figure class Figure { public: Figure (unsigned x, unsigned y) : xLocation(x), yLocation(y) {} unsigned getXLocation() const { return xLocation; } unsigned getYLocation() const { return yLocation; } protected: unsigned xLocation; unsigned yLocation; }; Note: New syntax for initializing data members

CSE Example: Continued class Rectangle : public Figure { public: Rectangle (unsigned x, unsigned y, unsigned length, unsigned height) : Figure(x,y), lengthDimension(length), heightDimension(height) {} unsigned getLength() const { return lengthDimension; } unsigned getHeight() const { return heightDimension; } protected: unsigned lengthDimension; unsigned heightDimension; };

CSE Example: Continued class Rectangle : public Figure { public: Rectangle (unsigned x, unsigned y, unsigned length, unsigned height) : Figure(x,y), lengthDimension(length), heightDimension(height) {} unsigned getLength() const { return lengthDimension; } unsigned getHeight() const { return heightDimension; } protected: unsigned lengthDimension; unsigned heightDimension; }; Note: Base-class constructor initializer

CSE Example: Reusing functions bool nearerOrigin( const Figure& f1, const Figure& f2 ) { unsigned f1X = f1.getXLocation(); unsigned f1Y = f1.getYLocation(); unsigned f2X = f2.getXLocation(); unsigned f2Y = f2.getYLocation(); return sqrt(f1X*f1X + f1Y*f1Y) < sqrt(f2X*f2X + f2Y*f2Y); }

CSE Example: Reusing functions (cont) int main(void) { Figure fig1(20,30), fig2(30,50); Rectangle rect1(10,40,13,28); Rectangle rect2(5,48, 101, 50); … if( nearerOrigin(fig1, fig2) ) { … } … if( nearerOrigin(rect1, rect2) ) { … } … if( nearerOrigin(fig2, rect1) ) { … } }

CSE Question Thinking in terms of bits, bytes, and the layout of data in a computer’s memory: –How is it possible to pass a reference to a Rectangle to a function that expects a reference to a Figure?

CSE Question Thinking in terms of bits, bytes, and the layout of data in a computer’s memory: –How is it possible to pass a reference to a Rectangle to a function that expects a reference to a Figure? Answer: Instances of a derived class are implemented as instances of the base class with additional data appended at the end.

CSE Derived class (implementation) xLocation yLocation lengthDimension heightDimension fig1fig2rect1rect2

CSE Visibility of data/function members Given: –Base class A with (data or function) member m –Derived class B, which inherits from A –Function fa that uses an object of class A –Function fb that uses an object of class B ABfafbfb public  protected  private  m visible

CSE Member-function over-riding class Manager : public Employee { private: list group; short level; public: void print( ostream& ) const; }; class Employee { private: string first_name, last_name; short department; public: void print( ostream& ) const; }; Observe: Derived class declares method for the print operation, despite inheriting a method from its base class

CSE Member function over-riding (cont) void Employee::print( ostream& os ) const { os << “Name:” << first_name << “ “ << last_name << endl << “Dept: “ << department; } void Manager::print( ostream& os ) const { Employee::print(os); os << “Level: “ << level; } Observe: Explicit invocation of base-class method

CSE Member function over-riding (cont) int main(void) { Employee doe(“John”, “Doe”, 235); Manager howell(“Charles”, “Howell”, 235, 3); doe.print(cout); // invokes Employee::print() howell.print(cout); // invokes Manager::print() Employee* ePtr = &howell; ePtr->print(cout); // invokes Employee::print() }

CSE Terminology C++ terminology [Stroustrup] : –Member function: A function declared or defined within a class definition OO modeling terminology [Blaha & Rumbaugh] : –Operation: Function or procedure that may be applied to or by objects in a class E.g., “ print ” One may invoke “the print operation” on any kind of Employee, including on a manager –Method: Implementation of an operation for a specific class E.g., “ Employee::print ” E.g., “ Manager::print ” –An operation that is implemented using multiple methods (defined in separate classes) is said to be polymorphic

CSE Polymorphic operations Polymorphism is a key defining characteristic of object- oriented methods and languages Extremely beneficial when used with classification and inheritance –E.g., allows to write reusable code that invokes an operation over a large variety of objects, without having to know which method is being used to implement the operation Unfortunately, to reap these benefits in C++ requires a little extra work when designing classes

CSE Reuse benefits of polymorphism Suppose we want to write a function that generates reports that include information about specific employees The code: void generateReport( Employee& empl ) {... empl.print(cout);... } ought to “do the right thing” when the actual parameter (empl) is an instance of class Manager

CSE However... void generateReport( Employee& empl ) {... empl.print(cout);... } int main(void) { Employee doe(“John”, “Doe”, 235); Manager howell(“Charles”, “Howell”, 235, 3); generateReport(howell); } Question: Which method invoked to carry out this operation when empl is actually an instance of Manager?