computer science ii for majors
play

Computer Science II for Majors Lecture 10 and 11 Inheritance Dr. - PowerPoint PPT Presentation

CMSC202 Computer Science II for Majors Lecture 10 and 11 Inheritance Dr. Katherine Gibson www.umbc.edu Last Class We Covered Professor Chang substitute taught Allocation methods Static, automatic, dynamic new and delete


  1. CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance Dr. Katherine Gibson www.umbc.edu

  2. Last Class We Covered • Professor Chang substitute taught • Allocation methods – Static, automatic, dynamic – new and delete • Dynamically allocating arrays – Constructors and destructors 2 www.umbc.edu

  3. Any Questions from Last Time? www.umbc.edu

  4. Today’s Objectives • To review the exam results • To understand the relationships between objects • To begin learning about inheritance – To cover what is being inherited – To understand how inheritance and access to member variables interact 4 www.umbc.edu

  5. Exam 1 Results www.umbc.edu

  6. Code Reuse www.umbc.edu

  7. Code Reuse • Important to successful coding • Efficient – No need to reinvent the wheel • Error free – Code has been previously used/tested – (Not guaranteed, but more likely) 7 www.umbc.edu

  8. Code Reuse Examples • What are some ways we reuse code? – functions – classes • Any specific examples? – calling Insert() and a modified Delete() for Move() – calling accessor functions inside a constructor 8 www.umbc.edu

  9. Code Reuse Examples • What are some ways we reuse code? – Functions – Classes – Inheritance – what we’ll be covering today • Any specific examples? 9 www.umbc.edu

  10. Object Relationships www.umbc.edu

  11. Refresher on Objects • Objects are what we call an instance of a class • For example: – Rectangle is a class – r1 is a variable of type Rectangle – r1 is a Rectangle object 11 www.umbc.edu

  12. Object Relationships • There are two types of object relationships • is-a – inheritance • has-a – composition both are forms – aggregation of association 12 www.umbc.edu

  13. Inheritance Relationship A Car is-a Vehicle • This is called inheritance • The Car class inherits from the Vehicle class • Vehicle is the general class, or the parent class • Car is the specialized class, or child class , that inherits from Vehicle 13 www.umbc.edu

  14. Inheritance Relationship Code class Vehicle { public: // functions private: int m_numAxles; all Vehicles have int m_numWheels; axles, wheels, a int m_maxSpeed; max speed, and a double m_weight; weight // etc } ; 14 www.umbc.edu

  15. Inheritance Relationship Code class Car { } ; 15 www.umbc.edu

  16. Inheritance Relationship Code class Car: public Vehicle { Car inherits from the Vehicle class don’t forget the colon here! } ; 16 www.umbc.edu

  17. Inheritance Relationship Code class Car: public Vehicle { public: // functions private: int m_numSeats; all Cars have a double m_MPG; number of seats, a string m_color; MPG value, a color, string m_fuelType; and a fuel type // etc } ; 17 www.umbc.edu

  18. Inheritance Relationship Code class Car: public Vehicle { /*etc*/ }; class Plane: public Vehicle { /*etc*/ }; class SpaceShuttle: public Vehicle { /*etc*/ }; class BigRig: public Vehicle { /*etc*/ }; 18 www.umbc.edu

  19. Composition Relationship A Car has-a Chassis • This is called composition • The Car class contains an object of type Chassis • A Chassis object is part of the Car class • A Chassis cannot “live” out of context of a Car – If the Car is destroyed, the Chassis is also destroyed 19 www.umbc.edu

  20. Composition Relationship Code class Chassis { public: // functions private: all Chassis have a string m_material; material, a weight, double m_weight; and a maxLoad double m_maxLoad; they can hold // etc } ; 20 www.umbc.edu

  21. Composition Relationship Code class Chassis { public: // functions private: also, notice string m_material; that there is double m_weight; no inheritance for the Chassis double m_maxLoad; class // etc } ; 21 www.umbc.edu

  22. Composition Relationship Code class Car: public Vehicle { public: // functions private: // member variables, etc. // has-a (composition) Chassis m_chassis; } ; 22 www.umbc.edu

  23. Aggregation Relationship a Car has-a Driver • this is called aggregation 23 www.umbc.edu

  24. Aggregation Relationship A Car has-a Driver • This is called aggregation • The Car class is linked to an object of type Driver • Driver class is not directly related to the Car class • A Driver can live out of context of a Car • A Driver must be “contained” in the Car object via a pointer to a Driver object 24 www.umbc.edu

  25. Aggregation Relationship Code class Driver: public Person { public: Driver itself is a child // functions class of Person private: Date m_licenseExpire; string m_licenseType; // etc } ; 25 www.umbc.edu

  26. Aggregation Relationship Code class Driver: public Person { public: Driver itself is a child // functions class of Person private: Date m_licenseExpire; string m_licenseType; // etc Driver inherits all of Person’s member variables (Date m_age, string m_name, } ; etc.) so they aren’t included in the Driver child class 26 www.umbc.edu

  27. Aggregation Relationship Code class Car: public Vehicle { public: // functions private: // member variables, etc. // has-a (aggregation) Person *m_driver; } ; 27 www.umbc.edu

  28. Visualizing Object Relationships • On paper, draw a representation of how the following objects relate to each other • Make sure the type of relationship is clear • Engine • Car • Driver • Vehicle • Person • BigRig • Owner • Rectangle • Chassis • SpaceShuttle 28 www.umbc.edu

  29. Inheritance www.umbc.edu

  30. Inheritance Access Specifiers • inheritance can be done via public, private, or protected • we’re going to focus exclusively on public • you can also have multiple inheritance – where a child class has more than one parent • we won’t be covering this 30 www.umbc.edu

  31. Hierarchy Example Vehicle 31 www.umbc.edu

  32. Hierarchy Example Vehicle Car BigRig Plane etc. 32 www.umbc.edu

  33. Hierarchy Example Vehicle Car BigRig Plane etc. SUV Sedan Van Jeep 33 www.umbc.edu

  34. Hierarchy Example Vehicle Specialization Car BigRig Plane etc. SUV Sedan Van Jeep 34 www.umbc.edu

  35. Hierarchy Vocabulary • more general class (e.g., Vehicle) can be called: – parent class – base class – superclass • more specialized class (e.g., Car) can be called: – child class – derived class – subclass 35 www.umbc.edu

  36. Hierarchy Details • parent class contains all that is common among its child classes (less specialized) – Vehicle has a maximum speed, a weight, etc. because all vehicles have these • member variables and functions of the parent class are inherited by all of its child classes 36 www.umbc.edu

  37. Hierarchy Details • child classes can use, extend, or replace the parent class behaviors 37 www.umbc.edu

  38. Hierarchy Details • child classes can use , extend, or replace the parent class behaviors • use – the child class takes advantage of the parent class behaviors exactly as they are • like the mutators and accessors from the parent class 38 www.umbc.edu

  39. Hierarchy Details • child classes can use, extend , or replace the parent class behaviors • extend – the child class creates entirely new behaviors • a RepaintCar() function for the Car child class • mutators/accessors for new member variables 39 www.umbc.edu

  40. Hierarchy Details • child classes can use, extend, or replace the parent class behaviors • replace – child class overrides parent class’s behaviors • (we’ll cover this later today) 40 www.umbc.edu

  41. Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 41 www.umbc.edu

  42. What is Inherited Vehicle Class 42 www.umbc.edu

  43. What is Inherited Vehicle Class • public fxns&vars 43 www.umbc.edu

  44. What is Inherited Vehicle Class • public fxns&vars • protected fxns&vars 44 www.umbc.edu

  45. What is Inherited Vehicle Class • public fxns&vars • protected fxns&vars • private variables • private functions 45 www.umbc.edu

  46. What is Inherited Vehicle Class • public fxns&vars • protected fxns&vars • private variables • private functions • copy constructor • assignment operator • constructor • destructor 46 www.umbc.edu

  47. What is Inherited Car Class Vehicle Class • public fxns&vars • protected fxns&vars • private variables • private functions • copy constructor • assignment operator • constructor • destructor 47 www.umbc.edu

  48. What is Inherited Car Class Vehicle Class • public fxns&vars • protected fxns&vars • child class • private variables members • private functions (functions • copy constructor & variables) • assignment operator • constructor • destructor 48 www.umbc.edu

  49. What is Inherited Car Class Vehicle Class • public fxns&vars & variables) ? • protected fxns&vars • child class • private variables members • private functions (functions • copy constructor • assignment operator • constructor • destructor 49 www.umbc.edu

Recommend


More recommend