8 Pages

03inheritance

Course: CPS 108, Fall 2009
School: Duke
Rating:
 
 
 
 
 

Word Count: 661

Document Preview

(language Inheritance independent) q First view: exploit common interfaces in programming Different kinds of bank accounts Tapestry tmap, iterator, C++ function objects Implementation varies while interface stays the same Second view: share code, factor code into parent class Code in parent class shared by subclasses Subclasses can override inherited method Can subclasses override and call? q q...

Register Now

Unformatted Document Excerpt

Coursehero >> North Carolina >> Duke >> CPS 108

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
(language Inheritance independent) q First view: exploit common interfaces in programming Different kinds of bank accounts Tapestry tmap, iterator, C++ function objects Implementation varies while interface stays the same Second view: share code, factor code into parent class Code in parent class shared by subclasses Subclasses can override inherited method Can subclasses override and call? q q Polymorphism/late(runtime) binding (compare: static) Actual function called determined when program runs, not when program is compiled 2.1 CompSci 108 : Fall 2003 Inheritance guidelines in C++ q Inherit from Abstract Base Classes (ABC) one pure virtual function needed (=0) q q must have virtual destructor implemented can have pure virtual destructor implemented, but not normally needed Avoid protected data, but sometimes this isn't possible data is private, subclasses have it, can't access it keep protected data to a minimum Single inheritance, assume most functions are virtual multiple inheritance ok when using ABC, problem with data in super classes virtual: some overhead, but open/closed principle intact 2.2 Subclasses must implement, or they're abstract too CompSci 108 : Fall 2003 Inheritance Heuristics q A base/parent class is an interface Subclasses implement the interface The base/parent class can supply some default behavior The base/parent class can have state Derived classes can use, override, both Protected: inherited and directly accessible Private: inherited but not accessible directly Behavior changes in subclasses, but there's commonality q q Abstract base classes are a good thing Push common behavior as high up as possible in an inheritance hierarchy If the subclasses aren't used polymorphically (e.g., through a pointer to the base class) then the inheritance hierarchy is probably flawed 2.3 CompSci 108 : Fall 2003 Inheritance Heuristics in C++ q One pure virtual (aka abstract) function makes a class abstract Cannot be instantiated, but can be constructed (why?) Default in C++ is nonvirtual or monomorphic Must have virtual destructor, the base class destructor (and constructor) will be called Unreasonable emphasis on efficiency, sacrifices generality If you think subclassing will occur, all methods are virtual q We use inheritance, public models isa relationship Private inheritance means isimplementedintermsof Implementation technique, not design technique Derived class methods call baseclass methods, but no "usableas a" via polymorphism Access to protected methods, and can redefine virtual funcs 2.4 CompSci 108 : Fall 2003 Inheritance and Layering/Aggregation q Layering (or aggregation) means "uses via instance variable" Use layering/attributes if differences aren't behavioral Use inheritance when differences are behavioral Consider Student class: name, age, gender, sleeping habits Which are attributes, which might be virtual methods Lots of classes can lead to lots of problems It's hard to manage lots of classes in your head Tools help, use speedbar in emacs, other class browsers in IDEs or in comments (e.g., javadoc) Inheritance hierarchies cannot be too deep (understandable?) 2.5 q q q CompSci 108 : Fall 2003 Inheritance guidelines (see Riel) q Watch out for derived classes with only one instance/object For the CarMaker class is GeneralMotors a subclass or an object? Watch out for derived classes that override behavior with a noop Mammal class from which platypus derives, livebirth? Too much subclassing? Base class House Derived: ElectricallyCooledHouse, SolarHeatedHouse? What to do with a list of fruit that must support applecoring? Fr...

Textbooks related to the document above:
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Duke - CPS - 108
Tapestry classes -> STLqWhat's the difference between tvector and vector Safety and the kitchen sinkWhat about Java analog? What happens with t[21] on a 21element vector? Part of STL means crufty code (whose viewpoint?)qDifferences in
Duke - CPS - 108
arrays and strings: what's a char *?qWhy not rely solely on string and vector classes? how are string and vector implemented? lower level access can be more efficient (but be leery of claims that Cstyle arrays/strings required for efficiency)
Duke - CPS - 108
From C+ to JavaqJava history: Oak, toasterovens, internet language, panacea Not really a standard language like C+ Arguably proprietary (and arguably not) Precursor to C# ? What it is OO language, not a hybrid (like C+) compiled to bytecode,
Duke - CPS - 108
From STL to JavaqIn STL an iterator is a concept, there are refinements Input, output, forward, bidirectional, random access A forward iterator is an input iterator and an output iterator The iterator may be immutable (or const)read onlyRe
Duke - CPS - 108
javax.swing, events, and GUIsqGUI programming requires processing events There's no visible loop in the program Wire up/connect widgets, some generate events, some process eventsWe want to do practice "safe wiring", meaning? Pressing this
Duke - CPS - 108
From XP to javax.swing.*qWhat parts of embracing change can we embrace in 108? Evolutionary design, small releases, iterative enhancement Simple design, don't build for the future (will you need it?) Lots of testing, testing, testing Refactori
Duke - CPS - 108
testing always test empty case test for good input and bad what does it mean to test every line of code?testing levels whole app driver for sub-system function level
Syracuse - MAE - 341
Syracuse - MAE - 341
'id- ---.---tI- - - - -. -- - -33actC)~~p-~-Elbow mass, M = 10 kgCoq.wkaban4 mhss =v25PZ = 120 kPa cabs) A ~ = O . O O ~ I ~-is,Ym a . , . , ,-r(jau_ . .-P,-oy ~.+-rlne? d m + gijhi, L
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Syracuse - MAE - 341
Washington - P - 209
First In-Class Questionnaire Items study buddies: When you are studying for an exam, what is the ideal number of people you like to study with? fastfood: How many DAYS on an average WEEK will you stop by a fast food restaurant (e.g., McDonald's, Burg
W. Florida - CHM - 2211
Alkylation of m-Xylene: A PuzzleCH3 (CH3)3CCl H3 C FeCl3 C(CH3)3 H 3C CH3Place 9 mL of m-xylene (1,3-dimethylbenzene) and 7.5 mL of t-butyl chloride (2-chloro-2methylpropane) in a dry Erlenmeyer flask (50 mL). Place a cork in the flask and cool th
W. Florida - CHM - 2211
CHM 2211L - PREPARATION OF SULFANILAMIDE FROM ACETANILIDE - DATA TABLE PHYSICAL PROPERTIES OF REAGENTS AND PRODUCTSNAME STRUCTURE and FORMULA MOLAR MASS(g/mol) (1) AcetanilideDENSITY(g/mL)MELTING POINT(O C)BOILING POINT(O C)--(2) Chl
W. Florida - CHM - 2211
Enzymatic Reduction: A Chiral Alcohol from a KetoneO O O Yeast H3C HO H O OThe starting material for this experiment has two carbonyl groups, a ketone and an ester. Strong reducing agents such as lithium aluminum hydride (LiAlH4 or LAH) would caus
W. Florida - CHM - 2211
CHM 2211L COMPETITIVE REACTIONS. Equilibria and Rates in Carbonyl ReactionsTHE PROBLEM TO BE INVESTIGATED: The competitive reactions in the preparation of semicarbazone derivatives of an aldehyde and a ketone will be examined.Starting MaterialK
W. Florida - CHM - 2211
CHM 2211L-DATA TABLECOMPETITIVE REACTIONSPHYSICAL PROPERTIES OF REAGENTS MOLAR MASS(g/mol) (1)DENSITY(g/mL)NAMESTRUCTURE AND FORMULAMELTING POINT (o C)BOILING POINT (o C) xxxxxxSemicarbazide Hydrochloridexxxx(2)Cyclohexanon
Austin College - HIST - 163
SYLLABUS: History 163 U.S. HISTORY, 1877 - 1975 Jacqueline M. Moore Spring 2007 Office Hours: M&F 12-1, TTH. 1-2pm, & by appt. My office is in: 121 Sherman Hall Telephone Numbers: Office: 813-2262 (voice mail) Home: (972)547-6812 (not between 11pm an
San Jose State - ENGR - 100
Waste Management and Energy Opportunities (EDITED)Steve Cox Anaerobe Systems 3/09/2007Anaerobe Systems Founded in 1975 Microbiology supply company Primarily deal with anaerobic bacteria Make culture media for growing anaerobes Petri plates
San Jose State - ENGR - 100
Copyright 2005 McClatchy Newspapers, Inc. Sacramento Bee October 26, 2005, Wednesday METRO FINAL EDITION SECTION: MAIN NEWS; Pg. A1 Cheese maker, state settle Hilmar agrees to pay $3 million, partly for groundwater study. Chris Bowman Bee Staff Write
LSU - PHYS - 2101
SID 22 35 356 541 941 1266 1395 1418 1559 1609 1623 1623 1868 2218 2507 2836 3504 3577 3730 3747 3838 3923 3997 4237 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6417 6742 6808 7211 7231 7460 7499 7578 8316 8384 8511 8594 8595 8638 9054 9357 945
LSU - PHYS - 2101
Assignment Name Hwk #1 Totals 63 SID 22 35 356 541 941 1266 1395 1418 1559 1609 1623 1623 1868 2218 2507 2836 3504 3577 3730 3747 3838 3923 3997 4237 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6417 6742 6808 7211 7231 7460 7499 7578 8316 8384
LSU - PHYS - 2101
SID 22 35 356 541 941 1266 1395 1418 1559 1609 1623 1623 1868 2218 2507 2836 3504 3577 3730 3747 3838 3923 3997 4237 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6417 6742 6808 7211 7231 7460 7499 7578 8316 8384 8511 8594 8595 8638 9054 9357 945
LSU - PHYS - 2101
SID 0022 0035 0356 0541 0941 1266 1395 1418 1553 1559 1609 1623 1623 1682 1868 2165 2218 2410 2507 2670 2836 3324 3412 3504 3577 3730 3747 3838 3923 3997 4237 4250 4308 4425 4809 4983 5265 5332 5335 5469 5486 5490 5849 6000 6279 6402 6417 6742 6808 7
LSU - PHYS - 2101
SID 0022 0035 0356 0541 0941 1266 1395 1418 1553 1559 1609 1623 1623 1682 1868 2165 2218 2410 2507 2670 2836 3324 3412 3504 3577 3730 3747 3838 3923 3997 4237 4250 4308 4425 4809 4983 5265 5332 5335 5469 5486 5490 5849 6000 6279 6402 6417 6742 6808 7
LSU - PHYS - 2101
SID 22 35 356 541 941 1266 1395 1418 1553 1559 1609 1623 1623 1682 1868 2165 2218 2507 2670 2836 3324 3412 3504 3577 3730 3747 3838 3923 3997 4237 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6417 6742 6808 7211 7214 7231 7460 7499 7578 8316 838
LSU - PHYS - 2101
SID 0022 0035 0356 0541 0941 1266 1395 1418 1559 1609 1623 1623 1868 2218 2507 2836 3504 3577 3730 3747 3838 3923 3997 4237 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6417 6742 6808 7211 7231 7460 7499 7578 8316 8384 8511 8594 8595 8638 9054 9
LSU - PHYS - 2101
SID 22 35 356 541 941 1266 1395 1418 1559 1609 1623 1623 1868 2218 2507 2836 3504 3577 3730 3747 3838 3923 3997 4237 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6417 6742 6808 7211 7231 7460 7499 7578 8316 8384 8511 8594 8595 8638 9054 9357 945
LSU - PHYS - 2101
SID 22 35 356 541 941 1266 1395 1418 1553 1559 1609 1623 1623 1682 1868 2165 2218 2507 2670 2836 3324 3412 3504 3577 3730 3747 3838 3923 3997 4237 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6417 6742 6808 7211 7214 7231 7460 7499 7578 8316 838
LSU - PHYS - 2101
SID 22 35 356 541 941 1266 1395 1418 1553 1559 1609 1623 1623 1682 1868 2165 2218 2507 2670 2836 3324 3412 3504 3577 3730 3747 3838 3923 3997 4237 4250 4425 4809 4983 5265 5332 5469 5486 5490 5849 6000 6402 6417 6742 6808 7211 7214 7231 7460 7499 757
LSU - PHYS - 2101
SIDHwk #1 3-Sep 63Hwk # 2 11-Sep 56Hwk # 3 17-Sep 35Hwk # 4 24-Sep 44Hwk # 5 3-Oct 35Hwk #6 10-Oct 38Hwk #7 18-Oct 35Score22 35 356 541 941 1266 1395 1418 1553 1559 1609 1623 1623 1682 1868 2165 2218 2507 2670 2836 3324 3412 3504 35
LSU - PHYS - 2101
Lecture 3: Newton's lawsPhys 2101 Gabriela GonzlezNewton's lawsPhysics having predictive value! Newton's laws: two qualitative, one quantitative: 1. If no net force, no acceleration (either at rest, or moving with constant velocity) 2.r r F =ma
LSU - PHYS - 2101
Physics 2101 Spring 2006January 24, 20061Question 1 - 8 pointsThe figure shows the velocity of a particle moving along an axis. Point 1 is at the highest point in the curve; point 4 is at the lowest point; points 2 and 6 are at the same height.
LSU - PHYS - 2101
y mg dvxLecture 6: Potential EnergyPhys 2101 Gabriela GonzlezPotential energyWhen a conservative force does work W on an object, it changes the amount of potential energy of that object byU = - W If the force is constant, W= Fd and U= -
LSU - PHYS - 2101
Lecture 9: CollisionsPhys 2101 Gabriela GonzlezConservation of momentumSingle particle: if F=0, then the momentum is constant. Also, acceleration is zero, velocity is constant.r r dp r F= = ma dtSystem of particles: if the external forces are
LSU - PHYS - 2101
Lecture 11: RocketsPhys 2101 Gabriela GonzlezConservation of momentumSingle particle: if F=0, then the momentum is constant. Also, acceleration is zero, velocity is constant.r r dp r F= = ma dtSystem of particles: if the external forces are z
LSU - PHYS - 2101
Mass m1, is pushed downhill from rest at height h. When reaching the horizontal part, it collides with mass m2. After the collision, mass m2 begins traveling on a surface with kinetic coefficient of friction k, and stops in a distance D. Write an ex
LSU - PHYS - 2101
Lecture 12: RotationPhys 2101 Gabriela GonzlezTranslation and Rotation2Angular displacement, velocity, accelerationPure rotation of a reference line in a solid body around an axis is described with an angle as a function of time. =s/r ccw: po
LSU - PHYS - 2101
Lecture 15: Angular momentumPhys 2101 Gabriela GonzlezRolling = Translation + Rotation2Kinetic energyKE = I com + Mv1 2 2 1 22 com3ExampleA solid ball starts from rest at the upper end of the track shown in the figure, and rolls with
LSU - PHYS - 2101
Lecture 16: Angular momentumPhys 2101 Gabriela GonzlezNewton's law: systems of particlesr dP Fnet = dt r dL net = dttotal linear momentumtotal angular momentumNo forces, no linear momentum: conservation of linear momentum. No torques, n
LSU - PHYS - 2101
Lecture 20: Equilibrium, gravityPhys 2101 Gabriela GonzlezForcesSome special forces: Gravity always acts on the "center of gravity"="center of mass". Walls exert normal forces (in surfaces), perpendicular to the surface; friction forces, para
LSU - PHYS - 2101
Lecture 21: GravitationPhys 2101 Gabriela GonzlezThe Law of GravitationNewton's gravitation law (not F=ma!):m1 m2 F = G 2 rG = 6.67 x 10 -11 N m2/kg22Gravitational acceleration and weightThe force of a spherical planet of mass M and radi
LSU - PHYS - 2101
Lecture 22 GravitationPhys 2101 Gabriela GonzlezKepler's laws: IThree laws, but all consequences of just one: Newton's law of gravitation! LAW OF ORBITS: All planets move in elliptical orbits, with the Sun at one focus. 2 x2 + 2 =1 2 a a = semi
LSU - PHYS - 2101
Lecture 23: FluidsPhys 2101 Gabriela GonzlezDensityDensity: mass per unit volume = m/V. If uniform, = m/V SI units: kg/m3QuickTimef and a TIFF (LZW) decompressor are needed to see this picture.2PressurePressure: force per unit area p = F
LSU - PHYS - 2101
Lecture 24: FluidsPhys 2101 Gabriela GonzlezDensityDensity: mass per unit volume = m/V. If uniform, = m/V SI units: kg/m32PressurePressure: force per unit area p = F/A SI units: N/m2 = Pa Non-SI units: 1 atm = 1.01x105 Pa = 760 torr =760 mm
LSU - PHYS - 2101
OscillationsPhys 2101 Gabriela GonzlezOscillationsOscillations = anything that "swings" back and forth. Harmonic oscillations: described with trigonometric functions. Three kinds of oscillations: free, damped and forced.2Examples3Simpl
LSU - PHYS - 2101
OscillationsPhys 2101 Gabriela Gonzlez2 Force: F = m a = - 2 x = - x m kThe force law for SHMDisplacement: x(t) = xm cos( t + ) Velocity: v(t) = dx(t)/dt = - xm sin( t + ) Acceleration: a(t) = dv(t)/dt = - 2 xm cos( t + ) = - 2 x(t)Simpl
LSU - PHYS - 2101
OscillationsPhys 2101 Gabriela Gonzlez2 Force: F = m a = - 2 x = - x m kThe force law for SHMDisplacement: x(t) = xm cos( t + ) Velocity: v(t) = dx(t)/dt = - xm sin( t + ) Acceleration: a(t) = dv(t)/dt = - 2 xm cos( t + ) = - 2 x(t)2Si
LSU - PHYS - 2101
Temperature and heatPhys 2101 Gabriela GonzlezTemperatureZeroth law of thermodynamics: If bodies A and B are each in thermal equilibrium with a third body T, then they are in thermal equilibrium with each other. Or: every body in thermal equilibr
LSU - PHYS - 2101
Ludwig Boltzmann 1866- 1906Lecture 34: ThermodynamicsPhys 2101 Gabriela GonzlezWork done by an ideal gaspV=nRTW= Constant temperature: keep temperature constant, change the volume. The pressure will change, following: p = nRT/V How muc
LSU - PHYS - 2101
Ludwig Boltzmann 1866- 1906Lecture 35: ThermodynamicsPhys 2101 Gabriela GonzlezIdeal gases so farpV = n R T Eint = Q W Eint= (3/2) n R T = n CV T CP = CV + R ; CV=(3/2)R Constant volume: W=0, Q = n CV T Eint= n CV TConstant pressure: W=