Download presentation
Presentation is loading. Please wait.
Published byNathaniel Kelly Modified over 9 years ago
1
Kate Gregory Week 6 Lab 3 due now –Rating your group Inheritance Multiple Inheritance Polymorphism Midterm Review
2
DateWeekTopicHand OutDue BackTest 6-Sep-131Administrivia / Overview / Motivation, benefits of OO 13-Sep-132Use CasesLab 1: Use cases 20-Sep-133CRC Cards, collab graphsLab 2: CRC cardslab 1 5% 27-Sep-13 4start class diaglab 2 5% 4-Oct-135Finish class diag, AssociationsLab 3: Class Diag 11-Oct-136Inh & Polymorphism / midterm review lab 3 5% 18-Oct-137midtermMidterm 25% 25-Oct-13Reading Break 1-Nov-138Interaction diag / Design PatternsLab 4: Interaction Diag 8-Nov-139Good Design / Modules & Packages / Deployment and component diagrams /Metrics / SOLID Lab 5: Critiqueslab 4 5% 15-Nov-1310State diagrams / Activity diagrams / Summary and Conclusion / The Future 22-Nov-1311Critiquescritique lab (before class) 15% 29-Nov-1312Critiques TBDFinal ExamFinal 40%
3
Kate Gregory Inheritance Programming by extension One class is based on (is derived from) another class. Inheritance allows classes to share and reuse behaviors and attributes. –Code and design Build on existing work –Designing, coding, testing
4
Kate Gregory From the General to the Specific The base class or super class is general: animal, vehicle, bank account The derived classes or subclasses are specific: mammal, car, savings account You add functionality as you move from the general to the specific: mammals can do things that not all animals can You cannot remove functionality
5
Kate Gregory Inheritance Example
6
Kate Gregory IS A Inheritance examples should make sense with “is a”. –A mammal is an animal –An ungulate is a mammal –A savings account is a bank account –A rush order is an order –A car is a vehicle Is a car an engine with metal wrapped around it?
7
IS A Problems Square and Rectangle have obvious similarities –Location in space –Can calculate area (similar formula) –Rectangle has height and width, Square has only height Square IS A Rectangle? –Can’t suppress functionality Rectangle IS A Square? –Doesn’t make any sense Kate Gregory
8
Square and Rectangle Square Location: Point Width: int GetArea(): int Move(Point) GetWidth(): int SetWidth(int) Rectangle Location: Point Width: int Height: int GetArea(): int Move(Point) GetWidth(): int SetWidth(int) GetHeight(): int SetHeight(int)
9
Square and Rectangle Square Rectangle Square inherits all Rectangle functions including GetHeight() and SetHeight()
10
Square and Rectangle Rectangle Square Rectangle adds functions including GetHeight() and SetHeight(), adds attribute height
11
Abstract Base Class Quadrilateral –Has location in space, idea of a function for area Rectangle IS A Quadrilateral –With height and width –Implements area function Square IS A Quadrilateral –With height –Implements area function Kate Gregory
12
Abstract Base Class Square Width: int GetArea(): int GetWidth(): int SetWidth(int) Rectangle Width: int Height: int GetArea(): int GetWidth(): int SetWidth(int) GetHeight(): int SetHeight(int) Quadrilateral Location: Point GetArea(): int Move(Point)
13
Abstract Classes An abstract class can’t be used to generate an object –Can you open just a bank account? Subclasses must implement the un-implemented methods or they are also abstract You instantiate objects of the subclasses Kate Gregory
14
Not all Base Classes are Abstract Use it when no complete (working) class has all the common functionality Don’t go out of your way to invent ABC when a concrete class can be a good base class –Eg Rush Order inherits from Order, don’t need to create abstract Order with Rush Order and Regular Order as subclasses Kate Gregory
15
Multiple Inheritance Some OO systems (C++, not Java or C#) permit a class to inherit from more than one superclass. This kind of inheritance is referred to as multiple inheritance.
16
Kate Gregory Multiple Inheritance Example Utility vehicle inherits from Car and Truck classes.
17
Kate Gregory Diamond Problem: Bad M.I. Disambiguating override Appliance +turnOn() : boolean +turnOff() : boolean -poweredup : boolean Clock +setAlarm() : boolean +turnOn() : boolean -time : int Radio +turnOn() : boolean -station : int ClockRadio
18
Diamond Problem What happens here? ClockRadio cr; cr.turnOn(); Need a disambiguating override Kate Gregory
19
Good Multiple Inheritance Problem : Track RCMP transportation –Includes: Car, PowerBoat, Bike, Canoe, SeaDoo and Horse. –Track schedules for changing oil, rotating tires, feeding, checkingForHoles etc. –Use inheritance as much as possible since we only want to write the code once !
20
Kate Gregory
22
Vehicle is still base class for these three. LandSeaAir CarHorseBikePowerBoatCanoeSeaDoo Motorized +changeOil() +rotateTires()
23
What if there’s no MI allowed? No need to inherit code, just design: –Interface –Keep substitutability, IS-A, polymorphism Need to inherit (reuse, maintain) code: –Use aggregation instead –Wrapper functions delegate to helper class –Drawbacks if helper class adds a method you must add the wrapper by hand to all who use it No polymorphism
24
Kate Gregory
26
Polymorphism Motor Vehicle +colour: ? +price: ? +model: ? +go() +stop() +turn left() +turn right() BusTruckCar Ford Mustang +stop() +go() Toyota CorollaPontiac Sunfire
27
Kate Gregory Polymorphism Imagine an array of MotorVehicles holding objects of Car, Truck and Bus, as well as, Mustang, Corolla and Sunfire. All are subclasses of Motor Vehicle. Code asks each vehicle in the array to stop. When the vehicle is a Mustang, the Mustang override of Stop without the calling code having to figure out which specific subclass the vehicle belongs. The correct version of a method will be called even though the object is being referred to in more generic terms.
28
Kate Gregory Uses of Polymorphism Here are a whole pile of shapes - draw them on the screen Here are a whole pile of bank accounts - print their statements Here are a whole pile of employees - print their paycheques
29
Kate Gregory Polymorphism benefit The old way: struct shape { int type; struct point reference; int dimension1; int dimension2; int dimension3; /*...*/ };
30
Kate Gregory Using the struct struct shape circle, square; /* fill them with values somehow*/ /* put pointers to them into an array */ for (i=0; i<numshapes; i++) { switch (array[i].type) { case CIRCLE: drawcircle(array[i]); break; case SQUARE: drawsquare(array[i]); break; /* etc */ }
31
Kate Gregory Without Polymorphism What is involved in adding a new shape? –#define for type –another case statement in the switch –new drawing method –the code that created the shape and added it to the array Four different files
32
Kate Gregory With Polymorphism class Shape { protected: Point reference; public: virtual void draw(); };
33
Kate Gregory class Circle: public Shape { private: int radius; public: void draw(); }; class Square: public Shape { private: int width; public: void draw(); };
34
Kate Gregory Circle circle; Square square; /* fill them with values somehow*/ /* put pointers to them into an array */ for (i=0; i<numshapes; i++) { array[i]->draw(); } Using the classes
35
Kate Gregory With Polymorphism What is involved in adding a new shape? –New class definition –code for draw() method –the code that created the shape and added it to the array Two or three different files –can get compiler to remind you to write the function
36
Inheritance and Relationships On your class diagram, if a Person can own any kind of Vehicle, draw the line to Vehicle, not to each subclass If for some reason a Company can own any kind of Vehicle but a Person can only own a Car, draw the lines to show that. Kate Gregory
37
Next Weeks Oct 18 th - midterm –Covers till the end of this lecture Oct 25 th, no lecture –Reading Week Nov 1 st –Interaction diagrams, design patterns –Lab 4 on interaction diagrams (due Nov 8 th )
38
Midterm Location Science Complex Lecture Hall (SC 137) Not This Room! We will start shortly after 9am and run for one hour There will not be a lecture afterwards Kate Gregory
39
Midterm Worth 25% of final grade One hour Closed book Covers up to today’s class True/False – Multiple Choice 20% Short Answer -- <= One sentence 20% Long Answer – diagrams, paragraphs etc. 60%
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.