CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 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,
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Polymorphism CS351 – Programming Paradigms.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Programming Languages and Paradigms Object-Oriented Programming.
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.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object-Oriented Programming in C++ More examples of Association.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
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.
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.
ISBN Object-Oriented Programming Chapter Chapter
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Overview of C++ Polymorphism
Variations on Inheritance Object-Oriented Programming Spring
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
 Inheritance  Protected Members  Non-public Inheritance  Virtual Function Implementation  Virtual Destructors  Abstract Base Classes and Interfaces.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Names and Attributes Names are a key programming language feature
CS 3370 – C++ Object-oriented Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Object-Oriented Programming
Polymorphism.
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Polymorphism Lec
Virtual Functions Department of CSE, BUET Chapter 10.
Overview of C++ Polymorphism
C++ Object Oriented 1.
Lecture 6: Polymorphism
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.
Presentation transcript:

CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored as a struct of member variables and inherited variables are augmented with additional ones –Methods as functions with an extra this argument for object Inheritance, dynamic binding raise additional issues –E.g., use of C++ virtual function table (v-tbl) to dispatch calls Language features influence object lifetimes –E.g., C++ stack objects’ lifetimes are automatically scoped to the duration of a function call (created and destroyed with it) –Can exploit this to manage heap objects as in the common “resource allocation is initialization” (RAII) coding idiom: tie the lifetime of a heap object to that of a stack object (e.g., a smart pointer) which is in turn tied to the lifetime of a function

CSE 425: Object-Oriented Programming II Further Data Abstraction Ideas in C++ Class constructors/destructors have special properties –Destructors/destructors run in opposite orders (next slides) –Constructors can use base/member class initialization, e.g., class Baz : public Foo {int j; Baz () : Foo(0), j(0) {} }; Static binding by default, use virtual to make dynamic –Pure virtual methods (declare with =0 ) make class abstract –Multiple inheritance is allowed (e.g., “mix-ins” design style) C++ also has interface polymorphism via templates –Including parameterized/subtype polymorphism –Highly efficient if used correctly, but, watch out for by-value return semantics (pop, destructor, etc. need to be efficient) –Can improve significantly on purely object-oriented approaches, e.g., containers (Scott examples 9.8 vs. 9.45)

CSE 425: Object-Oriented Programming II C++ Class and Member Construction Order class A { public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { public: B(int i, int j) : A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} private: int m_j; }; int main (int, char *[]) { B b(2,3); return 0; }; In the main function, the B constructor is called on object b –Passes in integer values 2 and 3 B constructor calls A constructor –passes value 2 to A constructor via base/member initialization list A constructor initializes m_i –with the passed value 2 Body of A constructor runs –Outputs “A” B constructor initializes m_j –with passed value 3 Body of B constructor runs –outputs “B”

CSE 425: Object-Oriented Programming II C++ Class and Member Destruction Order class A { public: A(int i) :m_i(i) { cout << "A“ << endl;} ~A() {cout<<"~A"<<endl;} private: int m_i; }; class B : public A { public: B(int i, int j) :A(i), m_j(j) { cout << “B” << endl;} ~B() {cout << “~B” << endl;} private: int m_j; }; int main (int, char *[]) { B b(2,3); return 0; }; B destructor called on object b in main Body of B destructor runs –outputs “~B” B destructor calls “destructor” of m_j –int is a built-in type, so it’s a no-op B destructor calls A destructor Body of A destructor runs –outputs “~A” A destructor calls “destructor” of m_i –again a no-op Compare orders of construction and destruction of base, members, body –at the level of each class, order of steps is reversed in constructor vs. destructor –ctor: base class, members, body –dtor: body, members, base class

CSE 425: Object-Oriented Programming II C++ Virtual Functions class A { public: A () {cout<<" A";} virtual ~A () {cout<<" ~A";} virtual f(int); }; class B : public A { public: B () :A() {cout<<" B";} virtual ~B() {cout<<" ~B";} virtual f(int) override; //C++11 }; int main (int, char *[]) { // prints "A B" A *ap = new B; // prints "~B ~A" : would only // print "~A" if non-virtual delete ap; return 0; }; Used to support polymorphism with pointers and references Declared virtual in a base class Can overridde in derived class –Overriding only happens when signatures are the same –Otherwise it just overloads the function or operator name More about overloading next lecture Ensures derived class function definition is resolved dynamically –E.g., that destructors farther down the hierarchy get called Use final (C++11) to prevent overriding of a virtual method Use override (C++11) in derived class to ensure that the signatures match (error if not)

CSE 425: Object-Oriented Programming II C++ Virtual Functions, Continued class A { public: void x() {cout<<"A::x";}; virtual void y() {cout<<"A::y";}; }; class B : public A { public: void x() {cout<<"B::x";}; virtual void y() {cout<<"B::y";}; }; int main () { B b; A *ap = &b; B *bp = &b; b.x (); // prints "B::x" b.y (); // prints "B::y" bp->x (); // prints "B::x" bp->y (); // prints "B::y" ap->x (); // prints "A::x" ap->y (); // prints "B::y" return 0; }; Only matter with pointer or reference –Calls on object itself resolved statically –E.g., b.y(); Look first at pointer/reference type –If non-virtual there, resolve statically E.g., ap->x(); –If virtual there, resolve dynamically E.g., ap->y(); Note that virtual keyword need not be repeated in derived classes –But it’s good style to do so Caller can force static resolution of a virtual function via scope operator –E.g., ap->A::y(); prints “A::y”

CSE 425: Object-Oriented Programming II Pure Virtual Functions in C++ class A { public: virtual void x() = 0; virtual void y() = 0; }; class B : public A { public: virtual void x(); }; class C : public B { public: virtual void y(); }; int main () { A * ap = new C; ap->x (); ap->y (); delete ap; return 0; }; A is an abstract (base) class –Similar to an interface in Java –Declares pure virtual functions ( =0 ) –May also have non-virtual methods, as well as virtual methods that are not pure virtual Derived classes override pure virtual methods (and thus become concrete) –B overrides x(), C overrides y() Can’t instantiate an abstract class –class that declares pure virtual functions –or inherits ones that are not overridden –A and B are abstract, can create a C Can still have a pointer or reference to an abstract class type –Useful for polymorphism

CSE 425: Object-Oriented Programming II Design with Pure Virtual Functions Pure virtual functions let us specify interfaces appropriately –But let us defer implementation decisions until later (subclasses) As the type hierarchy is extended, pure virtual functions are replaced –By virtual functions that fill in (and may override) the implementation details –Key idea: refinement Animal move()=0 Fish swim() Mammal walk() move() Bird Penguin waddle() move() swim() Sparrow walk() move() fly()

CSE 425: Object-Oriented Programming II Today’s Studio Exercises We’ll keep working with classes (for logic programming applications) –Developing clauses, checking for type compatibility Today’s exercises are again all in C++ –Please re-use your code from the previous studio session –As always, please ask us for help as needed When done, send with “Object-Oriented Programming Studio II” in the subject line, to