Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

Similar documents
JAVA MOCK TEST JAVA MOCK TEST II

Project. C++: Smart Pointers. The Plan. Announcement. Memory Leak. Pointer Ownership. Today: STL 1 Wednesday: STL 2 Thursday: Smart Pointers

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

This examination has 11 pages. Check that you have a complete paper.

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

G52CPP C++ Programming Lecture 13

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

CIS 190: C/C++ Programming. Lecture 11 Polymorphism

G52CPP C++ Programming Lecture 12

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

C++ Inheritance I. CSE 333 Spring Instructor: Justin Hsia

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CS250 Final Review Questions

VIRTUAL FUNCTIONS Chapter 10

C++ (classes) Hwansoo Han

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

University of Utah School of Computing

Chapter 5 Object-Oriented Programming

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

Extending Classes (contd.) (Chapter 15) Questions:

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

CMSC202 Computer Science II for Majors

CS 162, Lecture 25: Exam II Review. 30 May 2018

04-24/26 Discussion Notes

CS250 Final Review Questions

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOP THROUGH C++(R16) int *x; float *f; char *c;

C++ Inheritance II, Casting

PIC10B/1 Winter 2014 Exam I Study Guide

Arrays Classes & Methods, Inheritance

Data Abstraction. Hwansoo Han

G Programming Languages - Fall 2012

1. An object of a class can be treated as an object of its corresponding class.

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

Inheritance, and Polymorphism.

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

Object Oriented Programming. Solved MCQs - Part 2

PIC 10B Lecture 1 Winter 2014 Homework Assignment #1

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Lecture 15: Even more pointer stuff Virtual function table

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

CLASSES AND OBJECTS IN JAVA

CSE 12 Week Eight, Lecture One

Today s lecture. CS 314 fall 01 C++ 1, page 1

CSE 401/M501 Compilers

Data Structures and Other Objects Using C++

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

EL2310 Scientific Programming

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Java Programming Lecture 7

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

CPS 506 Comparative Programming Languages. Programming Language

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

More on Objects in JAVA TM

Common Misunderstandings from Exam 1 Material

EL2310 Scientific Programming

C++ Inheritance and Encapsulation

Objective-C. Stanford CS193p Fall 2013

Inheritance, Polymorphism and the Object Memory Model

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSE 333 Lecture 15 - inheritance

Inheritance and Interfaces

Derived and abstract data types. TDT4205 Lecture 15

Module 10 Inheritance, Virtual Functions, and Polymorphism

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Week 8: Operator overloading

OO Design with Multiple Inheritance

CSE : Python Programming

Data Structures (list, dictionary, tuples, sets, strings)

Written by John Bell for CS 342, Spring 2018

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

CSE 333 Lecture 15 - inheritance

CS11 Introduction to C++ Fall Lecture 7

EL2310 Scientific Programming

CS250 Final Review Questions

EECS 560 Lab 8: Leftist Heap as Priority Queue

Computer Programming

CS 11 java track: lecture 3

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Compaq Interview Questions And Answers

Lecture 18 Tao Wang 1

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

P2: Collaborations. CSE 335, Spring 2009

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

10. Object-oriented Programming. 7. Juli 2011

Polymorphism. Hsuan-Tien Lin. OOP Class, April 8, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/ / 26

Advanced Systems Programming

Object Oriented Programming. Java-Lecture 11 Polymorphism

Transcription:

Project C++: III Advanced Topics Othello submitted. Next submission: Team Evaluations Nov 10 th Please don t forget If solo give yourself a good evaluation! Indicate if okay to share feedback with partner Default: NO. That s today! Project Project Grading done during exam week Will send grades via e-mail. Grades will be available on mycourses after quarter is over. Plan This week in the home stretch The Ethics lecture Open Source / Free Software / GNU Final Review Questions? The final exam Other finals review CSH Review November 17 th 7pm 10pm Gracies Any questions? Before we begin Recall Final: November 18 th 12:30pm 2:30pm 70-3690

Plan for today What goes on behind the scenes What s up with the syntax: virtual void foo () = 0; Subclassing and When you define a class as a subclass: The subclass inherits all of the members and methods of the superclass. In addition, a subclass can have /methods that are it s own. is transitive: I.e. If B is a subclass of A and C is a subclass of B, then C inherits the /methods from both B and A. Behind the scenes The memory allocated for an object of a derived class consists of: An area for the base class s An area for the derived class s Subclassing Define a more general class. Both Actors and Musicians are specializations of subclasses superclass Actor isa isa Musician class { private: float ; char *; char *; (char *, char * ); virtual float calculatepay(); class Musician : public { private: Instrument *; bool ; Musician (char *); virtual float calculatepay(); virtual void setinstrument (Intrument *I); What the memory for Musician looks like Musician

Let s add a Drummer class Drummer : public Musician { private: Drum kit[]; Drummer (char *); virtual void setinstrument (Instrument *I); What the memory for drummer looks like kit Musician (includes ) Drummer What about virtual functions? C/C++ allows one to define pointers to functions. For all classes with virtual functions (defined or redefined), the compiler will create an array of pointers to virtual functions (VTABLE) Objects of such classes have a hidden member (vpt) which will point to the correct VTABLE array. Note: suggested, not required implementation Your mileage may vary. vtable created at compile time Initialized with values of Base class unless overriden by derived class. vptr set at run time. Each object (derived or otherwise) will have a SINGLE vptr. During construction Based on actual derived class.

Calling virtual functions In the original C++ interpretter: Instrument *I = new Instrument(); I->play(); Was converted to ((I-> vptr)[0]) (I); Pure virtual functions What happens if you have a pure virtual function? Virtual functions are nothing but entries in an array of function pointers. Pointers are pointers I.e. All pointers can be set to NULL. Thus, the syntax: virtual void foo () = 0; Pure virtual functions If a Base class defines a pure virtual function not overridden by the derived class: That entry in the derived class s VTABLE will be 0. Of course, the compiler will catch this. and Constructors Why you should not call virtual functions in a constructor Recall: When a member of a derived class is constructed, the constructor of it s base class is called first This fills in the memory area containing members of the base class This includes the setting of the vptr The base constructor will conclude before the derived constructor begins. and Constructors and Constructors class A { A(); ~A(); virtual void func1(); virtual void func2(); virtual void func3() = 0; class AA : public A { AA(); ~AA() void func1(); void func3(); Let s assume that A s constructor calls func1(). What happens after statement AA *myaa = new AA(); 1. AA::AA() calls A::A() 2. A::A() sets vptr to A s VTABLE 3. A::A() executes (calls A s func1()) 4. AA:AA() sets vptr to AA VTABLE 5. AA:AA() executes and returns.

and Constructors It s worse if A s constructor calls func3(). What happens after statement AA *myaa = new AA(); 1. AA::AA() calls A::A() 2. A::A() sets vptr to A s VTABLE 3. A::A() executes (calls A s func3()=0) 4. core dumped and compiler will NOT catch this. Slicing Recall that polymorphism can only be achieved using pointers rather than objects themselves. Attempts to copy a base class object with a derived class object will cause slicing. Only the base class section of the derived object will be copied. Slicing and Slicing Musician M ( Ringo ); P (M); During copy construction, The vptr will get set to the VTABLE for the class of object being copied Musician M ( Ringo ); P M Space allocated for P Copy constructor called P (M); s copy constructor called P is a vptr points to s VTABLE virtual functions are s. Correct Polymorphism and Destructors M P Musician *M (new Musician ( Ringo )); *P (M); Why define destructors to be virtual The destructor of a derived class will always call the destructor of it s Base class. M allocated on heap Pointer copy. However, even with correct polymorphism, there s no guarantee that the derived class s destructor will be called. heap M still has Musician s vptr since no copy constructor was called.

and Destructors and Destructors Base *B = new Derived();... delete B; // Base::~Base() called // Derived part of B never gets cleaned up If Base s destructor was declared as virtual. class Base { Base(); virtual ~Base(); A pointer to the destructor will be placed in the VTABLE. This pointer will get overridden by derived class s destructor and Destructors Remainder of course Base *B = new Derived();... delete B; // Destructor of B is found in the VTABLE pointed to // by B s vptr (ly ~Derived) // Derived destructor is called // which in turn will call Base s destructor. Tomorrow: Open Source / GNU Thursday: Final Exam Review. Questions? Questions?