Ch. 17 Case Study Combining Separate Classes Timothy Budd Oregon State University.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Ch 13. Features Found Only in Java Timothy Budd Oregon State University.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Ch 7. Operator Overloading Timothy Budd. Ch 7. Operator Overloading2 Introduction Almost all operators in C++ can be overloaded with new meanings. Operators.
Chapter 12: Adding Functionality to Your 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.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Case Study - Fractions Timothy Budd Oregon State University.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming with ANSI C ++
Inheritance and Polymorphism
Object-Oriented Design (OOD) and C++
Chapter 15: Overloading and Templates
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
Java Inheritance.
Chapter 11: Inheritance and Composition
Chapter 8: Class Relationships
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
CIS 199 Final Review.
Presentation transcript:

Ch. 17 Case Study Combining Separate Classes Timothy Budd Oregon State University

2 Introduction How to combine elements from two or more classes when they are not permitted to make changes to the original class. Solving this problem is an excellent illustration of the different uses of inheritance, templates, overloaded functions, and the interactions among these mechanisms.

3 Figure 17.1 Class Description for Apple // class Apple // created 1987 by Standard Apple of Ohio class Apple { public: // constructors Apple () : variety("generic") { } Apple (string & v) : variety (v) { } Apple (const Apple & r) : variety (r.variety) { } // apple operations ostream & printOn (ostream & out) { return out << "Apple: " << variety; } private: string variety; };

4 Figure 17.2 Class Description for Orange // Orange code // written by Chris (Granny) Smith, 1992 // House of Orange class Orange : public Produce { public: // constructor Orange (); void writeTo (ostream & aStream) { aStream << "Orange"; } };

5 Combining Separate Classes Since the class description for apples and oranges are distributed only in binary form, cannot add a new member function to these classes. However, nothing prevents from writing new ordinary functions takes their respective types as arguments.

6 Combining Separate Classes Use a single name, print, for the operation of printing to a stream: void print (const Apple & a, ostream & out) { a.printOn(out); } void print (const Orange & a, ostream & out) { a.writeTo(out); }

7 Combining Separate Classes Single common function name, print, that can be used for both data types: Apple anApple("Rome"); Orange anOrange; // can print both apples and oranges print (anApple, cout); print (anOrange, cout);

8 Unlike Java containers, container in C++ are homogeneous and can hold only one type of value. To combine apples and oranges in the same container, need an adaptor that will convert the type into a more suitable data value. We get one first by defining a common parent class that will describe the behavior that we want all fruit to posses: class Fruit { public: virtual void print (ostream &) = 0; };

9 Because the specific implementation of the behavior will be different for each fruit, make the description of this function into a pure virtual method. Using a template method, create a fruit adaptor that will take either an apple or an orange, and satisfy the fruit interface: template class FruitAdaptor : public Fruit { public: FruitAdaptor (T & f) : theFruit(f) { } T & value () { return theFruit; } virtual void print (ostream & out) { print(theFruit, out); } public: T & theFruit; };

10 The template argument allows to use the adaptor with both apples and oranges, but always yields a new value that is a subclass of Fruit: Fruit * fruitOne = new FruitAdaptor (anApple); Fruit * fruitTwo = new FruitAdaptor (anOrange); Since we have a common representation for apples and oranges, it is easy to create containers that will hold fruit values: list fruitList; // make a list of fruits fruitList.insert(fruitOne); // add an apple fruitList.insert(fruitTwo); // add an orange

11 A template function can simplify the creation of the adaptor, since the template argument types are inferred from the parameter values, and need not be specified when a template function is invoked. template Fruit * newFruit (T & f) { return new FruitAdaptor (f); } Using the newFruit function the fruit types will be inferred from the function arguments, and need not be specified explicitly by the programmer: Fruit * fruitThree = newFruit (anApple); Fruit * fruitFour = newFruit (anOrange)

12 We have all the elements necessary to maintain both apples and oranges in the same collection, and to perform polymorphic operations on these values: Apple anApple("Rome"); Orange anOrange; list fruitList; // declare list of pointers to fruits fruitList.insert(newFruit(anApple)); fruitList.insert(newFruit(anOrange)); list ::iterator start = fruitList.begin(); list ::iterator stop = fruitList.end(); // loop over and print out all fruits in container for ( ; start != stop; ++start) { Fruit & aFruit = *start; // get current fruit aFruit.print(cout); }

13 Combining Separate Classes Notice how this solution has made use of all the polymorphic mechanisms discussed; –overloaded functions, – template classes –template functions –inheritance –overridding