Question? Leave a message!




Inheritance Relationship

Inheritance Relationship
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 • isa – inheritance • hasa – composition both are forms of association – aggregation 9 Inheritance Relationship a Car isa Vehicle • this is called inheritance 10 Inheritance Relationship a Car isa 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 mnumAxles; int mnumWheels; int mmaxSpeed; double mweight; // etc ; 12 Inheritance Relationship Code class Vehicle public: // functions private: int mnumAxles; all Vehicles have int mnumWheels; axles, wheels, a int mmaxSpeed; max speed, and a double mweight; 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 mnumSeats; all Cars have a double mMPG; number of seats, a string mcolor; MPG value, a color, string mfuelType; 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 hasa Chassis • this is called composition 20 Composition Relationship a Car hasa Chassis • 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 21 Composition Relationship Code class Chassis public: //functions private: string mmaterial; double mweight; double mmaxLoad; // etc ; 22 Composition Relationship Code class Chassis public: //functions private: all Chassis have string mmaterial; a material, a double mweight; weight, and a double mmaxLoad; maxLoad they // etc can hold ; 23 Composition Relationship Code class Chassis public: //functions private: also, notice string mmaterial; that there is double mweight; no inheritance double mmaxLoad; for the Chassis // etc class ; 24 Composition Relationship Code class Car: public Vehicle public: //functions private: // member variables, etc. ; 25 Composition Relationship Code class Car: public Vehicle public: //functions private: // member variables, etc. // hasa (composition) Chassis mchassis; ; 26 Aggregation Relationship a Car hasa Driver • this is called aggregation 27 Aggregation Relationship a Car hasa Driver • 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 28 Aggregation Relationship Code class Driver: public Person public: // functions private: Date mlicenseExpire; string mlicenseType; // etc ; 29 Aggregation Relationship Code class Driver: public Person public: Driver itself is a child // functions class of Person private: Date mlicenseExpire; string mlicenseType; // etc ; 30 Aggregation Relationship Code class Driver: public Person public: Driver itself is a child // functions class of Person private: Date mlicenseExpire; string mlicenseType; // etc Driver inherits all of Person’s member variables ; (Date mage, string mname, etc.) so they aren’t included in the Driver child class 31 Aggregation Relationship Code class Car: public Vehicle public: //functions private: // member variables, etc. ; 32 Aggregation Relationship Code class Car: public Vehicle public: //functions private: // member variables, etc. // hasa (aggregation) Person mdriver; ; 33 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 34 Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 35 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 36 Hierarchy Example Vehicle 37 Hierarchy Example Vehicle Car BigRig Plane etc. 38 Hierarchy Example Vehicle Car BigRig Plane etc. SUV Sedan Van Jeep 39 Hierarchy Example Vehicle Car BigRig Plane etc. SUV Sedan Van Jeep 40 Specialization 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 41 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 42 Hierarchy Details • child classes can use, extend, or replace the parent class behaviors 43 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 44 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 45 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) 46 Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 47 What is Inherited Vehicle Class 48 What is Inherited Vehicle Class • public fxnsvars 49 What is Inherited Vehicle Class • public fxnsvars • protected fxnsvars 50 What is Inherited Vehicle Class • public fxnsvars • protected fxnsvars • private variables • private functions 51 What is Inherited Vehicle Class • public fxnsvars • protected fxnsvars • private variables • private functions • copy constructor • assignment operator • constructor • destructor 52 What is Inherited Car Class Vehicle Class • public fxnsvars • protected fxnsvars • private variables • private functions • copy constructor • assignment operator • constructor • destructor 53 What is Inherited Car Class Vehicle Class • public fxnsvars • protected fxnsvars • child class • private variables members • private functions (functions • copy constructor variables) • assignment operator • constructor • destructor 54 What is Inherited Car Class Vehicle Class • public fxnsvars • protected fxnsvars • child class • private variables members • private functions (functions • copy constructor variables) • assignment operator • constructor • destructor 55 What is Inherited Car Class Vehicle Class • public fxnsvars • protected fxnsvars • child class • private variables members • private functions (functions • copy constructor variables) • assignment operator • constructor • destructor 56 What is Inherited Car Class Vehicle Class • public fxnsvars • child class • protected • private variables members fxnsvars • private functions (functions • copy constructor variables) • assignment operator • constructor • destructor 57 What is Inherited Car Class Vehicle Class • public fxnsvars • child class • protected members fxnsvars • private functions (functions • copy constructor variables) • private • assignment operator variables • constructor • destructor 58 What is Inherited Car Class Vehicle Class • public fxnsvars • child class • protected members fxnsvars • private functions (functions • copy constructor variables) • private • assignment operator variables • constructor • destructor 59 What is Inherited Car Class Vehicle Class • public fxnsvars • child class • protected members fxnsvars • private functions (functions • copy constructor variables) • private • assignment operator variables • constructor • destructor not (directly) accessible by Car objects 60 What is Inherited Car Class Vehicle Class • public fxnsvars • child class • protected members fxnsvars • private functions (functions • copy constructor variables) • private • assignment operator variables • constructor • destructor not (directly) accessible by Car objects 61 What is Inherited Car Class Vehicle Class • public fxnsvars • child class • protected members fxnsvars • private functions (functions • copy constructor variables) • private • assignment operator variables • constructor • destructor not (directly) accessible can access and invoke, but by Car objects 62 are not directly inherited Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 63 Handling Access • child class has access to parent class’s: – public member variables/functions – protected member variables/functions 64 Handling Access • child class has access to parent class’s: – public member variables/functions – protected member variables/functions – but not private member variables/functions 65 Handling Access • child class has access to parent class’s: – public member variables/functions – protected member variables/functions – but not private member variables/functions • how should we set the access modifier for parent member variables we want the child class to be able to access 66 Handling Access • we should not make these variables protected • leave them private 67 Handling Access • we should not make these variables protected • leave them private • instead, child class uses protected functions when interacting with parent variables – mutators – accessors 68 Livecoding • let’s look more closely at inheritance • with these classes: – Shape • Rectangle • Pentagon • Circle LIVECODING LIVECODING 69 Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 70 Specialization • child classes are meant to be more specialized than parent classes – adding new member functions – adding new member variables • child classes can also specialize by overriding parent class member functions – child class uses exact same function signature 71 Overloading vs Overriding • overloading – 72 Overloading vs Overriding • overloading – use the same function name, but with different parameters for each overloaded implementation 73 Overloading vs Overriding • overloading – use the same function name, but with different parameters for each overloaded implementation • overriding – 74 Overloading vs Overriding • overloading – use the same function name, but with different parameters for each overloaded implementation • overriding – use the same function name and parameters, but with a different implementation – child class method “hides” parent class method – only possible by using inheritance 75 Overriding Examples • for these examples, the Vehicle class now contains these public functions: void Upgrade(); void PrintSpecs(); void Move(double distance); 76 Overriding Examples • for these examples, the Vehicle class now contains these public functions: void Upgrade(); void PrintSpecs(); void Move(double distance); • Car class inherits all of these public functions – it can therefore override them 77 Basic Overriding Example • Car class overrides Upgrade() void Car::Upgrade() // entirely new Caronly code • when Upgrade() is called on a object of type Car, what happens 78 Basic Overriding Example • Car class overrides Upgrade() void Car::Upgrade() // entirely new Caronly code • when Upgrade() is called on a object of type Car, the Car::Upgrade() function is invoked 79 Overriding (and Calling) Example • Car class overrides and calls PrintSpecs() void Car::PrintSpecs() Vehicle::PrintSpecs(); // additional Caronly code • can explicitly call a parent’s original function by using the scope resolution operator 80 Attempted Overloading Example • Car class attempts to overload the function Move(double distance) with new parameters void Car::Move(double distance, double avgSpeed) // new overloaded Caronly code 81 Attempted Overloading Example • Car class attempts to overload the function Move(double distance) with new parameters void Car::Move(double distance, double avgSpeed) // new overloaded Caronly code • but this does something we weren’t expecting 82 Precedence • overriding takes precedence over overloading – instead of overloading the Move() function, the compiler assumes we are trying to override it 83 Precedence • overriding takes precedence over overloading – instead of overloading the Move() function, the compiler assumes we are trying to override it • declaring Car::Move(2 parameters) 84 Precedence • overriding takes precedence over overloading – instead of overloading the Move() function, the compiler assumes we are trying to override it • declaring Car::Move(2 parameters) • overrides Vehicle::Move(1 parameter) 85 Precedence • overriding takes precedence over overloading – instead of overloading the Move() function, the compiler assumes we are trying to override it • declaring Car::Move(2 parameters) • overrides Vehicle::Move(1 parameter) • we no longer have access to the original Move() function from the Vehicle class 86 Overloading in Child Class • to overload, we must have both original and overloaded functions in child class void Car::Move(double distance); void Car::Move(double distance, double avgSpeed); • the “original” one parameter function can then explicitly call parent function 87 Livecoding • let’s change the PrintX() functions to be an overridden member function • we’ll have one of each “type”: – basic (complete override) – override and call – override and overload LIVECODING LIVECODING 88 Outline • Code Reuse • Object Relationships • Inheritance – What is Inherited – Handling Access • Overriding • Homework and Project 89 Homework 6 • check validity of input values • acceptable does not mean guaranteed • be extra careful with following the coding standards, and making appropriate decisions – explain in your README.txt • any questions 90 Project • groups are due today on Piazza – if you haven’t posted your group by 9 PM tonight, I will assign them for you • proposal due next week in class rd • alphas are due November 23 • IMPORTANT: we will be grading the most recent submission from your group 91 Project • proposal due next week in class • alphas due 1 ½ weeks after proposal • please don’t turn in anything late • will grade last submission from group members for alpha and project 92