Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance, Polymorphism, and Virtual Functions
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
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 in C++ CS-1030 Dr. Mark L. Hornick.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Object Oriented Programming
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
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,
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Run time Polymorphism
CS212: Object Oriented Analysis and Design
Polymorphism & Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Programming
Polymorphism Lec
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Virtual Functions Department of CSE, BUET Chapter 10.
Advanced Java Topics Chapter 9
Inheritance, Polymorphism, and Virtual Functions
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance and Polymorphism:
Advanced Programming Behnam Hatami Fall 2017.
Polymorphism Polymorphism
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Inheritance.
Fundaments of Game Design
Adapter Design Pattern
Chapter 8: Class Relationships
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Object-Oriented Programming when inheritance is needed

Terminology class D may inherit the features of another class B, or extend class B B – base class, superclass – provides generalization of class D D – derived class, subclass – provides specialization of class B example: Figure class may have three derived classes: Triangle, Box and Diamond inheritance relation forms inheritance hierarchy inheritance in UML denoted as a triangle pointing to base class

Example Figures class Square is derived from class Figure class Figure { // base class public: Figure(int); protected: int size_; // will be accessible to derived methods }; class Square: public Figure{ // derived class Square(int); void draw();

Access Methods base class features: public protected private access modifiers derived class features public: public protected inaccessible protected: protected protected inaccessible private: private private inaccessible put another way public inheritance – provides is-a relationship; or derived class inherits interface of base class: all public methods of base class available for derived class private inheritance – provides implemented in terms of relationship; or derived class inherits implementation of base class: functions of base class available only for derived class member functions

Overloading vs. Overriding function overloading – using multiple function names with different signatures in the same scope (compiler resolves invocations) function overloading does not work across inheritance: attempt to define a function with the same name in derived class makes base class function inaccessible (can still be accessed with scope resolution operator), or shadows the base class function function overriding – replacing base-class functions with derived class functions, signatures must match, resolution is done at run-time done with virtual functions

Virtual Functions what if necessary to manipulate objects regardless of specifics of derived class? need to draw figures regardless whether square or triangle can be done through pointers (or references) to objects Figure *fig1 = new Square(3); Figure *fig2 = new Triangle(5); fig1 -> draw(); fig2 -> draw(); which function (base or derived class) is invoked? early (compile-time) binding – resolving function by compiler Figure::draw() is invoked late (run-time) binding – resolving function by program itself on the basis of object class pointed-to Square::draw() and Triangle::draw() are invoked to enable late binding, declare function as virtual in base class: virtual void draw(); function so used is called polymorphic, technique – polymorphism virtual functions remain virtual in all derived classes even if virtual keyword is omitted some programmers put virtual in derived classes for stylistic reasons

Abstract Functions and Interfaces abstract operation (method/function) - defines only signature but not implementation pure virtual function – abstract function, prototype followed by =0 virtual void draw() =0; // pure virtual to be implemented in derived class concrete operation (method/function) – function whose implementation is provided abstract class – class that has at least one abstract function concrete class – otherwise abstract interface – a class whose interface has only abstract functions

Implementation of Virtual Functions herarchy vtable – a per-class table of function pointers vptr – a per-object pointer to vtable code every constructor has internal code to set up vptr every polymorphic function invocation adds a function pointer lookup

Construction and Destruction with Inheritance constructors default base-class constructor is invoked before derived class constructor derived class constructor must explicitly invoke non-default base class constructor done using a construct similar to member initialization list // base class constructor Figure::Figure(int size): size_(size){} // derived class constructor Square::Square(int size): Figure(size){} destructors destructors are executed in reverse order of constructors (derived first) always declare destructors virtual or destructors bound at compile-time and derived constructor may not be executed leaking memory default base class constructor idiom virtual ~Figure() {}

Constructor Delegation C++11 feature one constructor invokes another in initialization list avoids code duplication class Figure { public: Figure():Figure(0){} // delegates // to regular constructor Figure(int size): size_(size){} protected: int size_; };

Template Method Pattern specify an abstract class that implements most of functionality primitive operations – functions to be implemented (overridden) in concrete classes hook operations – may be overridden by concrete classes, provide default behavior template method – function of the base class that uses primitive operations (in effect, concrete implementations) to accomplish a certain task is a behavioral pattern – deals with class behavior what kind was singleton pattern? template method is used to design frameworks – the base class invokes subclass methods

Template Method UML Diagram in UML abstract functions are shown in italics

Review Questions what is base/derived class? how is inheritance indicate in UML? how is private inheritance different from public inheritance? what is a virtual function? how is it denoted in UML? what is an abstract function? abstract class? why is destructor always declared virtual? what is constructor delegation template method pattern, purpose? what is template method? what is a hook/primitive function?