CMSC202 Lecture: Inheritance and Object Relationships

CMSC202 Lecture: Inheritance and Object Relationships
Slide Note
Embed
Share

In this CMSC202 lecture, Dr. Katherine Gibson covers the concepts of inheritance, code reuse, and object relationships in computer science. The lecture delves into the basics of inheritance, the importance of code reuse for efficient programming, and understanding the relationships between objects. Examples are provided to illustrate how objects relate to each other, including the concepts of "is-a" and "has-a" relationships. The session also discusses the significance of inheritance in building class hierarchies and reusing code for successful coding practices.

  • CMSC202
  • Inheritance
  • Object Relationships
  • Code Reuse
  • Computer Science

Uploaded on Feb 22, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  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. Todays 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 Inheritance what we ll be covering today Any specific examples? 8 www.umbc.edu

  9. Object Relationships www.umbc.edu

  10. 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 10 www.umbc.edu

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

  12. 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 12 www.umbc.edu

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

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

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

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

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

  18. 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 18 www.umbc.edu

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

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

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

  22. 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 22 www.umbc.edu

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

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

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

  26. 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 Driver Person Owner Chassis Car Vehicle BigRig Rectangle SpaceShuttle 26 www.umbc.edu

  27. Inheritance www.umbc.edu

  28. 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 28 www.umbc.edu

  29. Hierarchy Example Vehicle 29 www.umbc.edu

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

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

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

  33. 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 33 www.umbc.edu

  34. 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 34 www.umbc.edu

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

  36. 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 36 www.umbc.edu

  37. 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 37 www.umbc.edu

  38. 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) 38 www.umbc.edu

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

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

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

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

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

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

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

  46. What is Inherited Car Class Vehicle Class public fxns&vars protected fxns&vars private variables private functions copy constructor assignment operator constructor destructor child class members (functions & variables) 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 & variables) ? child class members (functions 47 www.umbc.edu

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

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

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

More Related Content