chap13 - Chapter 13 Chapter Inheritance Mechanism for deriving new classes from existing classes Think of a Bicycle a Bicycle Tandem Bike Racing Bike

chap13 - Chapter 13 Chapter Inheritance Mechanism for...

This preview shows page 1 - 10 out of 41 pages.

Chapter 13 Chapter 13 Inheritance Mechanism for deriving new classes from existing classes
Image of page 1
Think of a Bicycle a Bicycle Tandem Bike Racing Bike Mountain Bike
Image of page 2
Thinking About Bicycles A tandem bicycle is a kind of bicycle Bicycle with two seats A mountain bicycle is a kind of bicycle Bicycle with shocks A racing bicycle is a kind of bicycle Lightweight aerodynamic construction Tandem, mountain, and racing bicycles are specialized bicycles
Image of page 3
Inheritance Inheritance is the object-oriented programming mechanism for specialization Ability to define new classes of objects using existing classes as a basis The new class inherits the attributes and behaviors of the parent classes New class is a specialized version of the parent class is-a relationships Bicycle Mountain Bikes Racing Bikes Tandem Bikes
Image of page 4
Inheritance A natural way to reuse code Programming by extension rather than reinvention Object-oriented paradigm is well-suited for this style of programming Terminology Base class (superclass) Derived class (subclass) is-a relationships Bicycle Mountain Bikes Racing Bikes Tandem Bikes
Image of page 5
class RectangleShape { public : RectangleShape(SimpleWindow &W, float XCoord, float YCoord, const color &Color, float Width, float Height); void Draw(); color GetColor () const ; void GetPosition( float &x, float &y) const ; void GetSize( float &Width, float &Height) const ; float GetWidth () const ; float GetHeight() const ; SimpleWindow& GetWindow() const ; void SetColor( const color &Color); void SetPosition( float x, float y); void SetSize( float Width, float Height); private : SimpleWindow &Window; float XCenter; float YCenter; color Color; float Width; float Height; }; Before Inheritance
Image of page 6
class CircleShape { public: CircleShape(SimpleWindow &W, float x, float y, const color &Color, float Diameter); void Draw(); color GetColor() const; void GetPosition(float &x, float &y) const; float GetSize() const; SimpleWindow& GetWindow() const; void SetColor(const color &Color); void SetPosition(float x, float y); void SetSize(float Diameter); private: SimpleWindow &Window; float XCenter; float YCenter; color Color; float Diameter; }; Before Inheritance
Image of page 7
Before Inheritance RectangleShape Location Window Color Width Height GetPosition() GetWindow() SetPosition() GetColor() SetColor() Draw() GetWidth() GetHeight() SetSize() CircleShape Location Window Color Diameter GetPosition() GetWindow() SetPosition() GetColor() SetColor() Draw() GetSize() SetSize()
Image of page 8
Shapes Hierarchy WindowObject Location Window GetPosition() GetWindow() SetPosition() is-a TriangleShape SideLength Draw() GetSideLength() SetSize() RectangleShape Width, Height Draw() GetWidth() GetHeight() SetSize() is-a CircleShape
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 41 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes