Inheritance Mechanism for deriving new classes from existing classes Chapter 13.

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Object Oriented Programming
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Object-oriented Programming Concepts
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Inheritance Mechanism for deriving new classes uses existing classes as bases.
Inheritance, Polymorphism, and Virtual Functions
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Object Oriented Programming: Inheritance Chapter 9.
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
Templates and Polymorphism Generic functions and classes
Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions.
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
EzWindows API A Graphical Application Programmer Interface JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
EzWindows API A Graphical Application Programmer Interface JPC and JWD © 2002 McGraw-Hill, Inc.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
The Class Construct Defining objects with attributes and behavior JPC and JWD © 2002 McGraw-Hill, Inc.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
C++ Program Design An Introduction to Programming and Object-Oriented Design.
Object Oriented Programming: Inheritance Chapter 9.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
The Class Construct and OOD Chapter 7 : class construct information hiding encapsulation data members member functions constructors inspectors mutators.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Object-Oriented Programming: Inheritance and Polymorphism.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
VG101 RECITATION 5 By TAs. CONTENTS How to read Vg101Class.h Samples about graphics About assignment 5 Array.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Object Oriented Programming: Inheritance Chapter 9.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Object Oriented Programming: Inheritance
Defining objects with attributes and behavior
C# - Inheritance and Polymorphism
One class is an extension of another.
Inheritance, Polymorphism, and Virtual Functions
An Example of Inheritance
Inheritance in C++ Inheritance Protected Section
A Graphical Application Programmer Interface
Computer Science II for Majors
Presentation transcript:

Inheritance Mechanism for deriving new classes from existing classes Chapter 13

Think of a Bicycle Tandem Bike Racing BikeMountain Bike a Bicycle

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

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

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

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

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

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()

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 Diameter Draw() GetSize() SetSize() is-a Shape Color GetColor() SetColor() is-a Label

Class WindowObject class WindowObject { public: WindowObject(SimpleWindow &w, const Position &p); Position GetPosition() const; SimpleWindow& GetWindow() const; void SetPosition(const Position &p); private: SimpleWindow &Window; Position Location; };

Implementing of WindowObject WindowObject::WindowObject(SimpleWindow &w, const Position &p) : Window(w), Location(p) { // No body needed } Initialization list and the members are initialized in class definition order Position WindowObject::GetPosition() const { return Location; } void WindowObject::SetPosition(const Position &p) { Location = p; } SimpleWindow& WindowObject::GetWindow() const { return Window; }

Defining a Derived Class class DerivedClass : public BaseClass { public: // public section... private: // private section... }; class DerivedClass : public BaseClass { public: // public section... private: // private section... }; Derived class name Class name of base class Access specifier (usually public)

Declaring a Derived Class class Shape : public WindowObject { public: Shape(SimpleWindow &w, const Position &p, const color &c = Red); color GetColor() const; void SetColor(const color &c); private: color Color; }; Read this as Shape is a kind of WindowObject Shape inherits WindowObject members Window, Location, GetPosition(), GetWindow(), and SetPosition()

Implementing A Derived Class Constructor DClass::DClass( PList ) : BClass( BList ), DMbrList { // Body of derived class constructor... }; DClass::DClass( PList ) : BClass( BList ), DMbrList { // Body of derived class constructor... }; Derived class name Derived class constructor parameter list Base class name Base class constructor parameter list (sublist of PList) Derived class data member initialization list (sublist of PList)

Implementing of Shape Class Shape::Shape(SimpleWindow &w, const Position &p, const color &c) : WindowObject(w, p), Color(c) { // No body needed } color Shape::GetColor() const { return Color; } void Shape::SetColor(const color &c) { assert(c >= 0 && c < MaxColors); Color = c; }

Basic Shapes Width Height RectangleShape SideLength TriangleShapeCircleShape Diameter

Class TriangleShape #include "shape.h" class TriangleShape : public Shape { public: TriangleShape(SimpleWindow &w, const Position &p, const color &c = Red, float slen = 1); float GetSideLength() const; void SetSize( float slen ); void Draw(); private: float SideLength; };

Class RectangleShape #include ”shape.h” class RectangleShape : public Shape { public: RectangleShape(SimpleWindow &w, const Position &Center, const color &c = Red, float Width = 1, float Width = 2); float GetWidth () const; float GetHeight() const; void SetSize(float Width, float Height); void Draw(); private: float Width; float Height; };

Class CircleShape #include "shape.h" class CircleShape : public Shape { public: CircleShape(SimpleWindow &w, const Position &Center, const color &c = Red, float Diameter = 1); float GetSize() const; void SetSize(float Diameter); void Draw(); private: float Diameter; };

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; }; No Inheritance

TriangleShape::Draw() void TriangleShape::Draw() { const float Pi = ; const Position Center = GetPosition(); const float SLength = GetSideLength(); // 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;

TriangleShape::Draw() // Create an array containing the positions of // the vertices of the triangle vector TrianglePoints(3); TrianglePoints[0] = Center + Position(0, -c), TrianglePoints[1] = Center + Position(-0.5*SLength, a); TrianglePoints[2] = Center + Position( 0.5*SLength, a); // Draw the triangle GetWindow().RenderPolygon(TrianglePoints, 3, GetColor(), HasBorder()); }

Using Shapes #include "rect.h" #include "ellipse.h" #include "triangle.h" SimpleWindow Window("TestShapes", 17, 7, Position(4, 4)); int ApiMain() { Window.Open(); TriangleShape T(Window, Position(3.5, 3.5), Red, 3.0); T.Draw(); RectangleShape R(Window, Position(8.5, 3.5), Yellow, 3.0, 2.0); R.Draw(); EllipseShape E(Window, Position(13.5, 3.5), Green, 3.0, 2.0); E.Draw(); return 0; }

Fun with Shapes

Inheritance and Member Access class SomeClass { public: void MemberFunction(); int MyPublicData; protected: int MyProtectedData; private: int MyPrivateData; }; void SomeClass::MemberFunction() { MyPublicData = 1;// access allowed MyProtectedData = 2;// access allowed MyPrivateData = 3;// access allowed } void NonMemberFunction() { SomeClass C; C.MyPublicData = 1; // access allowed C.MyProtectedData = 2; // illegal C.MyPrivateData = 3; // illegal }

Inheritance and Member Access class BaseClass { public: int MyPublicData; protected: int MyProtectedData; private: int MyPrivateData; }; class DerivedClass : public BaseClass { public: void DerivedClassFunction(); //... }; void DerivedClass::DerivedClassFunction() { MyPublicData = 1;// access allowed MyProtectedData = 2;// access allowed MyPrivateData = 3;// illegal }

Controlling Inheritance Public inheritance class DerivedClass : public BaseClass { public: //... protected: //... private: //... };

Controlling Inheritance public: private: BaseClass protected: public: private: DerivedClass protected: Public inheritance

Controlling Inheritance Private inheritance class DerivedClass : private BaseClass { public: //... protected: //... private: //... };

Controlling Inheritance public: private: BaseClass protected: public: private: DerivedClass protected: Private inheritance

Controlling Inheritance Protected inheritance class DerivedClass : protected BaseClass { public: //... protected: //... private: //... };

Controlling Inheritance public: private: BaseClass protected: public: private: DerivedClass protected: Public inheritance

Controlling Inheritance Inheritance Type Base class member access Derived class member access publicprivate protectedprivate inaccessible publicprotected private inaccessible public protected private inaccessible

Multiple Inheritance Bank account inheritance hierarchy BasicAcount is-a Loan Interest is-a Brokerage is-a Checking is-a InterestChecking is-a BrokerageChecking is-a

Multiple Inheritance class DClass: public BClass1, public BClass2 { public: // public section... protected: // protected section... private: // private section... }; class DClass: public BClass1, public BClass2 { public: // public section... protected: // protected section... private: // private section... }; Derived class name Class name of base classes Access specifier (usually public)

Class Label class Label : public WindowObject { public: Label(SimpleWindow &w, const Position &p, const string &Text, const color &c = Red); color GetColor() const; void SetColor(const color &c); void Draw(); private: color Color; string Text; }; WindowObject is-a Label Color Text Draw() GetColor() SetColor()

Class Label #include “lable.h” Label::Lable(SimpleWindow &w, const Position &p, const string &t, const color &c = Red): WindowObject(w, p), Text(t), Color(c) { } color Label::GetColor() const { return Color; } void Label::SetColor(const color &c) { Color = c; } void Label::Draw() { Position Center = GetPosition(); Position UpperLeft = Center + Position(-2.0, -2.0); Position LowerRight= Center + Position( 2.0, 2.0); GetWindow().RenderText(UpperLeft, LowerRight, Text, Color); }

Class LabeledEllipseShape WindowObject Shape EllipseShape is-a Label is-a LabeledEllipseShape is-a

Declaring a LabeledEllipseShape class LabeledEllipseShape : public Label, public EllipseShape { public: LabeledEllipseShape(SimpleWindow &w, const Position &p, const color &c = Red, const string &Text=“R”, float Width = 1.0, float Height = 2.0); void Draw(); };

Implementing LabeledEllipseShape LabeledEllipseShape::LabeledEllipseShape(SimpleWindow &w, const Position &p, const color &c, const string &Text, float Width, float Height) :EllipseShape(w, p, c, Width, Height), Label(w, p, Text, c) { //No body needed } void LabeledEllipseShape::Draw() { EllipseShape::Draw(); Label::Draw(); }

End of Chapter 13 Exercises Exercises Exercises Read the section 13.7 of textbook (Page743~756) carefully and rewrite the program Prettier Kaleidoscope Home works