Download presentation
Presentation is loading. Please wait.
1
CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 1/12/2019 CISC/CMPE320 Note: our 8:30 lecture on Nov. 13 (next Tuesday) has been moved to MacDonald Hall Rm 1. Assignment 4 on using the heap is due next Friday at 7pm. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod
2
Proposed Presentation Schedule
Four teams per timeslot. Plan on 10 minutes each. Let me know of any conflicts. Friday, Nov. 23 (two weeks away!): Aberdeen, Albert, Arch, Bagot. Tuesday, Nov. 27 (in Humphrey and then Ellis): Barrie, Beverley, Brock, Clergy, Collingwood, Division, Earl, Frontenac, George, Johnson, King. Wednesday, Nov. 28: Nelson, Princess, Queen, Stuart. Friday, Nov. 30: Union, University, Victoria, William. Fall 2018 CISC/CMPE320 - Prof. McLeod
3
Week 11 & 12 Team Presentations, Cont.
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 2018 CISC/CMPE320 - Prof. McLeod
4
CISC/CMPE320 - Prof. McLeod
Presentation, Cont. Normally your audience would be your clients, and you would want to impress them. In this case, it is your peers and we want to know how things actually went. Problems faced and overcome! You should still try to give a polished and practiced presentation. Do a dry run. Figure out the hookups in the room you will present in. NTD! All team members should be present, but you don’t all have to stand up in front. Minimum of one person presenting… Fall 2018 CISC/CMPE320 - Prof. McLeod
5
CISC/CMPE320 - Prof. McLeod
Peer Evaluations In onQ. Short, but: You will fill it out 22 times, once for each team (but not your own team!) to get full marks. 5% of your final grade. You will rate the RAD, the SDD and the Presentation of each team. There is one quiz for the RAD and SDD that you will fill out 22 times. There will be one quiz each for Friday, Tuesday, Tuesday in Ellis, Wednesday and then Friday again for presentations. Fall 2018 CISC/CMPE320 - Prof. McLeod
6
CISC/CMPE320 - Prof. McLeod
Peer Evaluations Your presentation evaluations will not count unless you have signed the attendance sheet for that particular day. You will provide a rank for the team, not a grade, and a relevant comment. Your rankings will be used in coming up with the team’s final grade. Comments will be collected and sent to each team. Fall 2018 CISC/CMPE320 - Prof. McLeod
7
CISC/CMPE320 - Prof. McLeod
Today Start C++ Inheritance. Fall 2018 CISC/CMPE320 - Prof. McLeod
8
Inheritance and Software Engineering
Encapsulation helps to maintain the integrity of an object’s data. Inheritance avoids code repetition and allows you to lessen the impact that making changes in one class has on other classes. The only way to get polymorphism, which allows you to ensure common behaviour at runtime to any object that is part of the hierarchy (late binding). Software that uses polymorphism does not have to worry about the specifics of objects it is using, which makes extensibility of those objects easier. Fall 2018 CISC/CMPE320 - Prof. McLeod
9
Inheritance & Software Engineering, Cont.
Polymorphism usually removes the need for switch statement structures and leads to code that is shorter and easier to read. The use of abstract classes with pure virtual functions imposes design specifications on child classes. Use private attributes whenever possible! Fall 2018 CISC/CMPE320 - Prof. McLeod
10
Inheritance & Polymorphism
You are already familiar with this OOP concept from Java course(s). (Right?) The extension syntax: class Child : public Parent { … } C++ does not have a single base class that works like the Object class in Java. Fall 2018 CISC/CMPE320 - Prof. McLeod
11
Aside - Protected Access
Until now, we have only used private and public specifiers inside the class’ definition. In addition, protected means the member is only accessible to a child class – so this specifier is only relevant in hierarchies. protected members are private outside the hierarchy. Otherwise the child class only inherits the public members of the parent class. Note that friend functions (and friend classes too!) can access both private and protected members from arguments. Fall 2018 CISC/CMPE320 - Prof. McLeod
12
Base Class Access Specifier
class Child : public Parent { … } public means no change to inherited access. protected changes access of public parent members to protected in child class. private changes both protected and public parent members to private in child class. private in parent is never accessible to child. this thing Fall 2018 CISC/CMPE320 - Prof. McLeod
13
CISC/CMPE320 - Prof. McLeod
Inheritance, Cont. The child class must invoke the constructor of the parent class. You can do this in the initializer list: Child::Child(const double aNum, const int aVal) : Parent(aNum), myNum(aVal) {} Fall 2018 CISC/CMPE320 - Prof. McLeod
14
Aside - C++11 Inheriting Constructors
If the constructor in the Child class is the same as the parent class’ constructor (it “mimics” it), then you can inherit the constructor from the parent class, too. Then you don’t have to write one in the child! Do this by using the “using” keyword anywhere in the Child class: using Parent::Parent; C++11 Fall 2018 CISC/CMPE320 - Prof. McLeod
15
CISC/CMPE320 - Prof. McLeod
Multiple Inheritance Multiple Inheritance: You can extend more than one class (separate them with commas), if you really want to… How to deal with ambiguities? Fall 2018 CISC/CMPE320 - Prof. McLeod
16
Polymorphism and virtual Prototypes
To allow the polymorphic process of dynamic binding to work, name the function to be shared in parent and child classes as virtual (before the return type in the prototype). Note that once a function is declared virtual in a parent class all child class versions of the same function are automatically virtual – but it is considered good form to still name them as virtual. Fall 2018 CISC/CMPE320 - Prof. McLeod
17
Aside – C++11 override Keyword
You can add this to the end of a function prototype that is overriding a virtual parent prototype. Then the compiler will check to make sure you have done the overriding properly. (Like annotation in Java). C++11 Fall 2018 CISC/CMPE320 - Prof. McLeod
18
Aside - Shadowing or “Hiding”
This is what you get if you don’t specify virtual for the function that is in both parent and child classes. The child class version shadows or hides the parent class version. At run-time you will get static binding instead of dynamic binding. The function invoked is based on the pointer type, not the run-time object type. Fall 2018 CISC/CMPE320 - Prof. McLeod
19
Aside - Shadowing or “Hiding”, Cont.
If you change the parameter list of a function in the child that has the same name as a function in the parent class, you do not get overloading (as we would have in Java). Instead the child version shadows or “hides” the parent class version. Later: Use the using keyword to construct inherited overloading. Fall 2018 CISC/CMPE320 - Prof. McLeod
20
Aside – C++ final Keyword
Used at the end of the prototype to prevent a virtual function from being overridden in a child class. A class can also be declared final, in which case it cannot be used as a parent class. (Same as Java). Fall 2018 CISC/CMPE320 - Prof. McLeod
21
CISC/CMPE320 - Prof. McLeod
Overriding, Cont. If you don’t use a virtual function in the Parent class, then the Child class just redefines the function when it uses the same signature. If you make the Parent class’ function virtual then you are overriding the function in the Child class, and you can use polymorphism. With a virtual function ownership is determined by the object’s actual type not the type of the pointer used to refer to the object. Fall 2018 CISC/CMPE320 - Prof. McLeod
22
CISC/CMPE320 - Prof. McLeod
Overriding, Cont. Polymorphism only works with pointers and references, not the actual objects. Invoking a function from the actual objects only gives you static binding. If the child class does not override the Parent’s virtual function it just inherits it. Fall 2018 CISC/CMPE320 - Prof. McLeod
23
Inheritance, Cont. – virtual Destructors
The parent class should declare a virtual destructor even if it does nothing. This will make sure that all destructors get invoked. They will get invoked as a “chain reaction”. Fall 2018 CISC/CMPE320 - Prof. McLeod
24
More Notes on the Use of virtual
Don’t use this keyword in the implementation file. You do not *have* to override a virtual function in a child class, but then you won’t have polymorphism. (As we will see shortly – you *must* implement a pure virtual function if your child class is to be non-abstract.) Fall 2018 CISC/CMPE320 - Prof. McLeod
25
Pure Virtual Member Functions
(This is just like abstract in Java.) This is when you have a virtual prototype in the parent class, but cannot implement it in the parent class. You avoid implementation by setting the function prototype to zero: virtual double getArea() const = 0; getArea() might be in a Shape parent class, but cannot calculate an area, for example. Fall 2018 CISC/CMPE320 - Prof. McLeod
26
Pure Virtual Member Functions, Cont.
If a class has at least one pure virtual member then it is termed an abstract class. An abstract class can contain normal members and other virtual members, as usual. You cannot instantiate an abstract class. But you can use the abstract class as a type to enable polymorphism. If a child class is not to be abstract, as well, then it must implement the pure virtual member function. Fall 2018 CISC/CMPE320 - Prof. McLeod
27
CISC/CMPE320 - Prof. McLeod
Shapes Example Look at the Shapes hierarchy. (This hierarchy has very little to do with the one in assignment 5. In fact, really doesn’t matter that it is called “Shapes” – maybe I’m in a rut…) The heap is not used. (Yet.) Look for the effects of virtual, pure virtual, overriding, inheritance, shadowing and slicing. Fall 2018 CISC/CMPE320 - Prof. McLeod
28
CISC/CMPE320 - Prof. McLeod
Shapes Example, Cont. A simple hierarchy: Shape colour Rectangle width, height Square Fall 2018 CISC/CMPE320 - Prof. McLeod
29
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 2018 CISC/CMPE320 - Prof. McLeod
30
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 2018 CISC/CMPE320 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.