lecture3 - PolymorphisminC+ Classesthatcanbe...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 1 Polymorphism in C++ Classes that can be  know as other classes Virtual functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 2 Polymorphism The ability of an object of  one type  to  appear as and be used as  another  type . CSquare *square = new CSquare(); CShape *shape = square; The variable square is pointing to an object of type  CSquare The variable shape is pointing to the same object, but  only knows it as CShape    (it just knows that part of the  object)
Background image of page 2
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 3 Difference between CShape and CSquare mX mY mX mY mWidth CShape: CSquare: Deriving CSquare from CShape just adds more stuff onto  the end.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 4 Polymorphism CSquare *square = new CSquare(); CShape *shape = square; The variable square is pointing to an object of type  CSquare The variable shape is pointing to the same object, but  only knows it as CShape We can do:   shape->SetX(15); We can’t do:   shape->SetWidth(22);  // Why?  A square is a shape, but a  shape is not necessarily a  square! 3
Background image of page 4
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 5 The Key Idea An object can be know by its class or  any class it is derived from. Example: CScreen – A screen in a computer game. Can be 2D or 3D.  CScene – A scene in a computer game.  3D only.  Derived from  CScreen. CCityScene – A scene set in a city.  Derived from CScene. We create an object for a  city scene . We can pass that object to  functions that expect a pointer or reference to a CScene or  CScreen. Think of it this way:   A CCityScene is a CScence.   A CScene is a CScreen;  A CCityScence is  a CScreen;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 6 We can use a base class for what is common to  multiple different classes Classes Common Square, circle, picture Location on screen Button, text entry, list box Location, size, enabled Bookkeeper, custodian,  engineer Employee information RGB, CMKY, Monochrome  images Width, height
Background image of page 6
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 7 This could be operations as well as data Classes Common Square, circle, picture Location on screen,  draw Button, text entry, list box Location, size, enabled,  draw,  mousetest Bookkeeper, custodian,  engineer Employee information,  page RGB, CMKY, Monochrome  images Width, height,  load, save Clarinet, trumpet, snare drum Make sound
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 8 An example: An ATM Withdrawal Get   Amount Get   Account Send to  Bank Wait for  Completion Print  Receipt Display  Failure Dispense  Funds Acknowledg e to Bank Done rejected failed
Background image of page 8
CSE 335 Dr. Charles B. Owen
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

lecture3 - PolymorphisminC+ Classesthatcanbe...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online