DOC PREVIEW
UD CISC 181 - Introduction to Computer Science

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CISC181 Introduction to Computer ScienceDr McCoy1Dr. McCoyLecture 27December 8, 2009Object Oriented Programming• Classes categorize entities that occur in applications.• Class teacher captures commonalities of teachers2teachers.•See• ~/Class/cisc181/examples/stu-teach.h• ~/Class/cisc181/examples/stu-teach.cc• ~/Class/cisc181/examples/stu-teach-drive.ccWhat is a class?• Class is similar to a category – the notion of categorization is common in conventional knowledge representation capturing is-a relationships between3capturing isa relationships between levels.ShapeFig. 9.3 Inheritance hierarchy for Shapes.4TwoDimensionalShape ThreeDimensionalShapeCircle Square Triangle Sphere Cube TetrahedronSingle inheritanceCommunityMemberEmployeeStudent AlumnusFig. 9.2 Inheritance hierarchy for university CommunityMembers.5Administrator TeacherAdministratorTeacherStaffFacultySingle inheritanceSingle inheritanceMultiple inheritanceExtending a Hierarchy• Once a hierarchy is established, it is easy to extend.• To describe a new concept, not necessary to describe all of its features, it suffices to 6,describe it differences from a concept existing in the hierarchy.• Note: new cat, already know quite a bit!• Same concept can be used with programming.2Inheritance Basics• New class inherited from another class• Base class– "General" class from which others derive• Derived class–New class– Automatically has base class’s:• Member variables• Member functions– Can then add additional member functionsand variables14‐7Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Commonality Abstracted• What is common about student and teacher? • Can we describe that, and then make a student and a teacher under that concept8student and a teacher under that concept (inheriting some behavior from the “base” concept)Example -- Person• ~/Class/cisc181/examples/person-stu-teach.h• ~/Class/cisc181/examples/person-stu-teach cc9teach.cc• ~/Class/cisc181/examples/person-stu-teach-drive.cc109.1 Introduction• Inheritance– Software reusability– Create new class from existing class• Absorb existing class’s data and behaviors• Enhance with new capabilitiesDi dl ihitf b l 2003 Prentice Hall, Inc. All rights reserved.–Derived class inherits from base class• Derived class– More specialized group of objects– Behaviors inherited from base class• Can customize– Additional behaviors119.1 Introduction• Three types of inheritance– public• Every object of derived class also object of base class– Base-class objects not objects of derived classes– Example: All cars vehicles, but not all vehicles cars• Can access non-private members of base class 2003 Prentice Hall, Inc. All rights reserved.– Derived class can effect change to privatebase-class members• Through inherited non-private member functions– private• Alternative to composition• Chapter 17– protected • Rarely usedDerived Classes• Consider example:Class of "Employees"• Composed of:–Salaried employees– Hourly employees• Each is "subset" of employees– Another might be those paid fixed wage eachmonth or week14‐12Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.3Derived Classes• Don’t "need" type of generic "employee"– Since no one’s just an "employee"• General concept of employee helpful!–All have names– All have social security numbers– Associated functions for these "basics" aresame among all employees• So "general" class can contain all these"things" about employees14‐13Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Employee Class• Many members of "employee" class applyto all types of employees– Accessor functions– Mutator functionsMost data items:–Most data items:• SSN• Name• Pay• We won’t have "objects" of this class, however14‐14Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Employee Class• Consider printCheck() function:– Will always be "redefined" in derived classes– So diff erent employee types can havedifferent checks–Makes no sense really for "undifferentiated"employee– So function printCheck() in Employee classsays just that• Error messag e stating "printCheck called forundifferentiated employee!! Aborting…"14‐15Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Display 14.3 Interface for the Derived Class HourlyEmployee (1 of 2)14‐16Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Display 14.3 Interface for the Derived Class HourlyEmployee (2 of 2)14‐17Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.HourlyEmployee Class Interface• Note definition begins same as any other– #ifndef structure– Includes required libraries– Also includes employee.h!• And, the heading:class HourlyEmployee : public Employee{ …– Specifies "publicly inherited" from Employeeclass14‐18Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.4HourlyEmployee Class Additions• Derived class interface only lists new or"to be redefined" members– Since all others inherited are already defined– i.e.: "all" employees have ssn, name, etc.• HourlyEmployee adds:– Constructors– wageRate, hours member variables– setRate(), getRate(), setHours(), getHours()member functions14‐19Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.HourlyEmployee Class Redefinitions• HourlyEmployee redefines:– printCheck() member function– This "overrides" the printCheck() functionimplementation from Employee class• It’s definition must be in HourlyEmployeeclass’s implementation– As do other member functions declared inHourlyEmployee’s interface• New and "to be redefined"14‐20Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Inheritance Terminology• Common to simulate family relationships• Parent class– Refers to base class• Child class–Refers to derived class• Ancestor class– Class that’s a parent of a parent …• Descendant class– Opposite of ancestor14‐21Copyright © 2010 Pearson Addison‐Wesley. All rights reserved.Constructors in Derived Classes• Base class constructors are NOT inherited in derived classes!– But they can be invoked within derived classconstructor• Which is all we need!• Base class constructor must initialize allbase class member variables– Those inherited by derived class– So derived class


View Full Document
Download Introduction to Computer Science
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Computer Science and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Computer Science 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?