J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Inheritance Mechanism for deriving new classes uses existing classes as bases.

Slides:



Advertisements
Similar presentations
Has-a Relationships A pen “has a” or “contains a” ball.
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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
Chapter 14 Graph class design John Keyser’s Modifications of Slides by Bjarne Stroustrup
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
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.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
UML Basics & Access Modifier
Inheritance using Java
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Templates and Polymorphism Generic functions and classes
Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
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.
EzWindows API A Graphical Application Programmer Interface JPC and JWD © 2002 McGraw-Hill, Inc.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
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.
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Inheritance Mechanism for deriving new classes from existing classes Chapter 13.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
The Class Construct and OOD Chapter 7 : class construct information hiding encapsulation data members member functions constructors inspectors mutators.
© 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.
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
CHAPTER 7 Object Oriented Programming. Object-oriented program “Data and Operations go together was independently” is the main idea The promise of making.
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.
1 Chapter 4: Another way to define a class Inheritance..!!
Object-Oriented Programming: Inheritance and Polymorphism.
Object-Oriented Programming Fundamentals. Comp 241, Fall Example Colored points on the screen What data goes into making one? Coordinates Color.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
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.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Access Functions and Friend Functions
Inheritance Concept Polygon Rectangle Triangle class Rectangle{
Inheritance ITI1121 Nour El Kadri.
Advanced Program Design with C++
Defining objects with attributes and behavior
C# - Inheritance and Polymorphism
Object Oriented Analysis and Design
Object Oriented Programming
C++ Programming CLASS This pointer Static Class Friend Class
Object Oriented Programming
A Graphical Application Programmer Interface
Computer Science II for Majors
Presentation transcript:

J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Inheritance Mechanism for deriving new classes uses existing classes as bases

Ch 13 / Foil 2 l 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

Ch 13 / Foil 3 l 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 l Terminology n Base class (superclass) n Derived class (subclass) Inheritance

class RectangleShape { public: RectangleShape(SimpleWindow &W, float XCoord, float YCoord, const color &Color, float Width, float Height); void Draw(); color GetColor() const; void GetSize(float &Width, float &Height) const; void GetPosition(float &x, float &y) 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; float GetSize() const; void GetPosition(float &x, float &y) 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

Shapes Hierarchy

Ch 13 / Foil 7 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; };

Ch 13 / Foil 8 WindowObject Constructor WindowObject::WindowObject(SimpleWindow &w, const Position &p) : Window(w), Location(p) { // No body needed }

Ch 13 / Foil 9 WindowObject Inspectors Position WindowObject::GetPosition() const { return Location; } SimpleWindow& WindowObject::GetWindow() const { return Window; }

Ch 13 / Foil 10 WindowObject Mutator void WindowObject::SetPosition(const Position &p){ Location = p; }

Ch 13 / Foil 11 Defining a Derived Class

Ch 13 / Foil 12 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()

Ch 13 / Foil 13 Implementing a Derived Class

Ch 13 / Foil 14 Implementing a Derived 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; }

Ch 13 / Foil 15 Basic Shapes

Ch 13 / Foil 16 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; };

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

#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 Draw(); void SetSize(float Width, float Height); private: float Width; float Height; }; RectangleShape

Ch 13 / Foil 19 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;

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

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

Ch 13 / Foil 22 Fun with Shapes

Ch 13 / Foil 23 Cleaning Up int ApiEnd() TWindow.Close(); return 0; }

Ch 13 / Foil 24 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 }

Ch 13 / Foil 25 Inheritance and Member Access void NonMemberFunction() { SomeClass C; C.MyPublicData = 1; // access allowed C.MyProtectedData = 2; // illegal C.MyPrivateData = 3; // illegal }

Ch 13 / Foil 26 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 }

Ch 13 / Foil 27 Controlling Inheritance