Inheritance Mechanism for deriving new classes from existing


































- Slides: 34

Inheritance Mechanism for deriving new classes from existing classes JPC and JWD © 2002 Mc. Graw-Hill, Inc.

Think of a Bicycle

Think of a Tandem Bike

Think of a Racing Bike

Think of a Mountain Bike

Thinking About Bicycles A tandem bicycle is a kind of bicycle n Bicycle with two seats A mountain bicycle is a kind of bicycle n Bicycle with shocks A racing bicycle is a kind of bicycle n Lightweight aerodynamic construction Tandem, mountain, and racing bicycles are specialized bicycles

Wouldn’t It Be Nice Be able to create specialized program objects without starting from scratch n Blinking rectangles n Moving bitmaps n Arbitrary precision numbers Inheritance is the object-oriented programming mechanism for specialization

Inheritance Ability to define new classes of objects using existing classes as a basis n The new class inherits the attributes and behaviors of the parent classes n New class is a specialized version of the parent class

Inheritance A natural way to reuse code n Programming by extension rather than reinvention n Object-oriented paradigm is well-suited for this style of programming Terminology n Base class (superclass) n Derived class (subclass)

Before Inheritance class Rectangle. Shape { public: Rectangle. Shape(Simple. Window &W, float XCoord, float YCoord, const color &Color, float Width, float Height); void Draw(); color Get. Color() const; void Get. Size(float &Width, float &Height) const; void Get. Position(float &x, float &y) const; float Get. Width() const; float Get. Height() const; Simple. Window& Get. Window() const; void Set. Color(const color &Color); void Set. Position(float x, float y); void Set. Size(float Width, float Height); private: Simple. Window &Window; float XCenter; float YCenter; color Color; float Width; float Height; };

Before Inheritance class Circle. Shape { public: Circle. Shape(Simple. Window &W, float x, float y, const color &Color, float Diameter); void Draw(); color Get. Color() const; float Get. Size() const; void Get. Position(float &x, float &y) const; Simple. Window& Get. Window() const; void Set. Color(const color &Color); void Set. Position(float x, float y); void Set. Size(float Diameter); private: Simple. Window &Window; float XCenter; float YCenter; color Color; float Diameter; };

Shapes Hierarchy

Class Window. Object class Window. Object { public: Window. Object(Simple. Window &w, const Position &p); Position Get. Position() const; Simple. Window& Get. Window() const; void Set. Position(const Position &p); private: Simple. Window &Window; Position Location; };

Window. Object Constructor Window. Object: : Window. Object(Simple. Window &w, const Position &p) : Window(w), Location(p) { // No body needed } Members are initialized in class definition order

Window. Object Inspectors Position Window. Object: : Get. Position() const { return Location; } Simple. Window& Window. Object: : Get. Window() const { return Window; }

Window. Object Mutator void Window. Object: : Set. Position(const Position &p) { Location = p; }

Defining a Derived Class

Declaring a Derived Class Read this as Shape is a kind of Window. Object class Shape : public Window. Object { public: Shape(Simple. Window &w, Shape inherits Window. Object const Position &p, members Window, Location, const color &c = Red); Get. Position(), Get. Window(), color Get. Color() const; and Set. Position() void Set. Color(const color &c); private: color Color; };

Implementing A Derived Class Constructor

Implementing a Derived Class Shape: : Shape(Simple. Window &w, const Position &p, const color &c) : Window. Object(w, p), Color(c) { // No body needed } color Shape: : Get. Color() const { return Color; } void Shape: : Set. Color(const color &c) { assert(c >= 0 && c < Max. Colors); Color = c; }

Basic Shapes

Triangle. Shape #include "shape. h" class Triangle. Shape : public Shape { public: Triangle. Shape(Simple. Window &w, const Position &p, const color &c = Red, float slen = 1); float Get. Side. Length() const; void Set. Size(float slen); void Draw(); private: float Side. Length; };

Ellipse. Shape #include "shape. h" class Ellipse. Shape : public Shape { public: Ellipse. Shape(Simple. Window &w, const Position &Center, const color &c = Red, float Width = 1, float Height = 2); float Get. Width() const; float Get. Height() const; void Draw(); void Set. Size(float Width, float Height); private: float Width; float Height; };

Rectangle. Shape #include "shape. h" class Rectangle. Shape : public Shape { public: Rectangle. Shape(Simple. Window &w, const Position &Center, const color &c = Red, float Width = 1, float Width = 2); float Get. Width() const; float Get. Height() const; void Draw(); void Set. Size(float Width, float Height); private: float Width; float Height; };

Triangle. Shape: : Draw() void Triangle. Shape: : Draw() { const float Pi = 3. 1415; const Position Center = Get. Position(); const float SLength = Get. Side. Length(); // Compute c, distance from center of triangle // to the top vertex, and a, the distance from // the center to the base of the triangle float c = SLength / (2. 0 * cos(30 * Pi / 180. 0)); float a = tan(30 * Pi / 180. 0) *. 5 * SLength;

Triangle. Shape: : Draw() // Create an array containing the positions of // the vertices of the triangle vector Position Triangle. Points[3]; Triangle. Points[0] = Center + Position(0, -c), Triangle. Points[1] = Center + Position(-. 5 * SLength, a); Triangle. Points[2] = Center + Position(. 5 * SLength, a); // Draw the triangle Get. Window(). Render. Polygon(Triangle. Points, 3, Get. Color(), Has. Border()); }

#include "rect. h" #include "ellipse. h" #include "triangle. h" Simple. Window("Test. Shapes", 17. 0, Position(4. 0, 4. 0)); int Api. Main() { Window. Open(); Triangle. Shape T(Window, Position(3. 5, 3. 5), Red, 3. 0); T. Draw(); Rectangle. Shape R(Window, Position(8. 5, 3. 5), Yellow, 3. 0, 2. 0); R. Draw(); Ellipse. Shape E(Window, Position(13. 5, 3. 5), Green, 3. 0, 2. 0); E. Draw(); return 0; } Using Shapes

Fun with Shapes

Cleaning Up int Api. End() TWindow. Close(); return 0; }

Inheritance and Member Access class Some. Class { public: void Member. Function(); int My. Public. Data; protected: int My. Protected. Data; private: int My. Private. Data; }; void Some. Class: : Member. Function() { My. Public. Data = 1; // access allowed My. Protected. Data = 2; // access allowed My. Private. Data = 3; // access allowed }

Inheritance and Member Access void Non. Member. Function() { Some. Class C; C. My. Public. Data = 1; // access allowed C. My. Protected. Data = 2; // illegal C. My. Private. Data = 3; // illegal }

Inheritance and Member Access class Base. Class { public: int My. Public. Data; protected: int My. Protected. Data; private: int My. Private. Data; }; class Derived. Class : public Base. Class { public: void Derived. Class. Function(); //. . . }; void Derived. Class: : Derived. Class. Function() { My. Public. Data = 1; // access allowed My. Protected. Data = 2; // access allowed My. Private. Data = 3; // illegal }

Controlling Inheritance
