CISC/CMPE320 - Prof. McLeod

Slides:



Advertisements
Similar presentations
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advertisements

1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object Oriented Programming
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 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
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.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance and Polymorphism
CISC/CMPE320 - Prof. McLeod
Polymorphism & Virtual Functions
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
CISC/CMPE320 - Prof. McLeod
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Inheritance Basics Programming with Inheritance
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
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
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
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
Chapter 8: Class Relationships
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.
Final and Abstract Classes
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
C++ Object Oriented 1.
SPL – PS3 C++ Classes.
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.
Computer Science II for Majors
Presentation transcript:

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

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

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

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

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

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

CISC/CMPE320 - Prof. McLeod Today Start C++ Inheritance. Fall 2018 CISC/CMPE320 - Prof. McLeod

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

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

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

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

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

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

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

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

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

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 the @override annotation in Java). C++11 Fall 2018 CISC/CMPE320 - Prof. McLeod

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

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

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

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

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

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

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

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

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

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

CISC/CMPE320 - Prof. McLeod Shapes Example, Cont. A simple hierarchy: Shape colour Rectangle width, height Square Fall 2018 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 2018 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 2018 CISC/CMPE320 - Prof. McLeod