CISC/CMPE320 - Prof. McLeod

Slides:



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

Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Lecture Videos will no longer be posted. Assignment 3 is due Sunday, the 8 th, 7pm. Today: –System Design,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Overview of C++ Polymorphism
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). SDD document framework should be set up in your Wiki by now.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Today: –Two simple binding examples. –Function Hiding.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
CMSC202 Computer Science II for Majors Lecture 14 – Polymorphism Dr. Katherine Gibson.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
Object-Oriented Programming
Polymorphism.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
CISC/CMPE320 - Prof. McLeod
Object Oriented Programming
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
CMPE212 – Reminders Assignment 3 due next Friday.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
Computer Science II for Majors
Presentation transcript:

CISC/CMPE320 - Prof. McLeod Winter 2013 CISC/CMPE320 8/9/2018 CISC/CMPE320 Notices Tentative Presentation Schedule for week 12 on next slide. Let me know of conflicts. Fall 2017 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

Presentation Schedule Five teams per hour = 10 minutes per team. Monday, Nov. 27, 11:30 to 2:30pm in TBA: Hour 1: Abitibi, Buckhorn, Calabogie, Charleston, Clearwater. Hour 2: Erie, Horseshoe, Huron, Kahshe, Kawartha. Hour 3: Limerick, Madawaska, Manitou, Nipigon, Nipissing. Tuesday, Nov. 28, 1:30 to 2:30pm in WLH205: Ontario, Paudash, Rainy, Rosseau, Scugog. Thursday, Nov. 30, 12:30 to 1:30pm in WLH205: Simcoe, Skeleton, Star, StClair, Superior. Fall 2017 CISC/CMPE320 - Prof. McLeod

Week 12 Team Presentations – 10 Min! Demonstrate of the fruit of your efforts! And, summarize (in any order you wish): How work ended up being distributed and when it was completed. Difficulties faced and overcome. Difficulties not overcome. Good/bad library use. Work left to do, if any. Team techniques that worked and those that did not. What you would do differently if you had to do this again… NTD!!! Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Today Finish up Inheritance in C++. Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Shapes Example, Cont. Back to the Shapes hierarchy. The root class is no longer abstract and all bugs and errors have been removed. Look for the effects of virtual, pure virtual, overriding, inheritance, shadowing and slicing. Fall 2016 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Shapes Example, Cont. A simple hierarchy: Shape colour Rectangle width, height Square Fall 2017 CISC/CMPE320 - Prof. McLeod

Shapes Example, Summary Polymorphism works with references or pointers of a base class type as long as the function(s) of interest are declared virtual and have been overridden in the child classes. Polymorphism also works when passing pointers or references to functions. The alternative is static binding where the function invoked is determined by the type of the pointer or reference only. Fall 2016 CISC/CMPE320 - Prof. McLeod

Shapes Example, Summary Cont. An abstract class cannot be used as a parameter type when passing by value because it is necessary to invoke a copy constructor. Polymorphism will not work when passing by value into a function. Passing a child object to a parent parameter by value can result in object “slicing”. dynamic_cast<> will not work with references – partly because a reference can never be null. Fall 2016 CISC/CMPE320 - Prof. McLeod

Another Function Binding Examples Static binding is determined at compilation. Dynamic binding is determined at run time. See: TestStaticBinding.cpp Fall 2016 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Name Hiding The designers of C++ decided that overloaded functions cannot be inherited in a child class. The child class version of the overloaded function “hides” the parent class version. This is a good way to avoid some tricky ambiguities, apparently. See TestHiding.cpp Fall 2016 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Name Hiding, Cont. You can get around this by using “using” in the child class as shown in the demo. However, this is considered poor practice. It is less confusing to avoid trying to overload inherited functions and just use different function names instead. Fall 2016 CISC/CMPE320 - Prof. McLeod

Back to the Shapes Example See the Shapes hierarchy program again, using the heap. Somewhat simplified from last time. Implements the “Big Three”. Also includes the use of our own namespace just for yucks! Shape colour Rectangle fillColour, width, height Square Fall 2016 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Shapes on the Heap Not worried about testing polymorphism this time. Shape is abstract again. Both Shape and Rectangle have an attribute on the heap. Each class has full implementations of the “Big Three”. The operator= overload in the child class shadows the one in the parent class because they are not virtual. Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Shapes on the Heap, Cont. Note how the child class’ copy constructor invokes the parent class’ copy constructor. And, how the child class’ operator= invokes the parent class’ operator=. However, the child class’ destructor does not have to invoke the parent class’ destructor because they are virtual. Note the behaviour that results from deleting a pointer of a base class type that is pointing to a derived class object. Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Shapes on the Heap, Cont. Note that the order of destruction is the inverse of the order of construction, as it should be. This is also a result of the destructors being virtual, and the fact that a child class’ constructor must invoke the parent class’ constructor first thing. Fall 2017 CISC/CMPE320 - Prof. McLeod

CISC/CMPE320 - Prof. McLeod Shapes on the Heap, Cont. Test what happens when you don’t have a copy constructor in Square and Rectangle. No errors, but the program crashes. Why? What happens if you just remove the Square copy constructor but keep the Rectangle copy constructor? No crashing this time, but why not? Would it be OK to remove the Square destructor? Fall 2017 CISC/CMPE320 - Prof. McLeod

The “Big Three” and Inheritance It is important to realize that constructors, destructors and operator= functions are not inherited in derived classes. If you don’t write them you will get compiler generated versions instead. The compiler’s versions will not necessarily do what you want! Fall 2017 CISC/CMPE320 - Prof. McLeod

The “Big Three” and Inheritance, Cont. For example, the compiler will only make member wise copies of attributes which could result in aliasing and will not put attributes on the heap. If you don’t write a destructor for a derived class that has an attribute on the heap, then it will not get deleted. Once you write the “Big Three” in one class in a hierarchy it is safest to write them for all three classes. Fall 2017 CISC/CMPE320 - Prof. McLeod