Inheritance Relationship

Inheritance Relationship
Dr.MasonHanks Profile Pic
Dr.MasonHanks,Germany,Teacher
Published Date:23-07-2017
Your Website URL(Optional)
Comment
CIS 190: C/C++ Programming Lecture 10 Inheritance 1 Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 2 Code Reuse • important to successful coding • efficient – no need to reinvent the wheel • error free (more likely to be) – code has been previously used/test 3 Code Reuse Examples • What are some ways we reuse code? • Any specific examples? 4 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 5 Code Reuse Examples • What are some ways we reuse code? – functions – classes – inheritance – what we’ll be covering today • Any specific examples? – calling Insert() and a modified Delete() for Move() – calling accessor functions inside a constructor 6 Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 7 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 8 Object Relationships • two types of object relationships • is-a – inheritance • has-a – composition both are forms of association – aggregation 9 Inheritance Relationship a Car is-a Vehicle • this is called inheritance 10 Inheritance Relationship a Car is-a Vehicle • 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 11 Inheritance Relationship Code class Vehicle public: // functions private: int m_numAxles; int m_numWheels; int m_maxSpeed; double m_weight; // etc ; 12 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 ; 13 Inheritance Relationship Code class Car ; 14 Inheritance Relationship Code class Car: public Vehicle Car inherits from the Vehicle class ; 15 Inheritance Relationship Code class Car: public Vehicle Car inherits from the Vehicle class don’t forget the colon here ; 16 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 Inheritance Relationship Code class Car: public Vehicle /etc/ ; 18 Inheritance Relationship Code class Car: public Vehicle /etc/ ; class Plane: public Vehicle /etc/ ; class SpaceShuttle: public Vehicle /etc/ ; class BigRig: public Vehicle /etc/ ; 19 Composition Relationship a Car has-a Chassis • this is called composition 20