Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
CISC/CMPE320 - Prof. McLeod
Today Finish up Inheritance in C++. Fall 2017 CISC/CMPE320 - Prof. McLeod
5
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
6
CISC/CMPE320 - Prof. McLeod
Shapes Example, Cont. A simple hierarchy: Shape colour Rectangle width, height Square Fall 2017 CISC/CMPE320 - Prof. McLeod
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.