Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc.

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.
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
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Object-Oriented Programming CS 3360 Spring 2012 Sec , Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
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.
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.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
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.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
UML Basics & Access Modifier
Inheritance using Java
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Templates and Polymorphism Generic functions and classes
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Information Systems Engineering
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.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
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.
CITA 342 Section 1 Object Oriented Programming (OOP)
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
Object-Oriented Programming: Inheritance and Polymorphism.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Defining objects with attributes and behavior
Inheritance, Polymorphism, and Virtual Functions
Review: Two Programming Paradigms
One class is an extension of another.
Inheritance, Polymorphism, and Virtual Functions
C++ Interlude 1 C++ Classes
C++ Programming CLASS This pointer Static Class Friend Class
Lecture 10 Concepts of Programming Languages
Computer Science II for Majors
Presentation transcript:

Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc.

Miscellaneous Topics Inheritance more coming up.. Enumerated Types (enums) enum Color {RED, BLUE,..}; Color c = RED; pred? expr1 : expr2 (a==2)? 5 : (D*4) extern declaration extern int numEntries; // in every file where numEntries is used int numEntries; // declare this in just one file int f(); // equivalent to extern declaration for a function

Misc. Topics (Cont.) static static global variables  static int foobar; // Available only to this file static local variables  like a global variable, but accessible only to the function where it is declared  e.g. int f () { static int numCalls = 0; numCalls ++ } Coding style See CS101 home page

Ability to define new classes of objects using existing classes as a basis Inheritance 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 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

A natural way to reuse code Programming by extension rather than reinvention Object-oriented paradigm is well-suited for this style of programming 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)

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

Shapes Hierarchy

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

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

Defining a Derived Class

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

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

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 }

Inheritance Related Buzzwords Don’t worry about these for now, but read about them in your holidays if you are interested virtual function abstract base class late binding