CS 117 Spring 2002 Class and Function Reuse derived classes templates.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Classes & Data Abstraction
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
1 Lab Session-1 CSIT221 Fall 2002 b Refresher slides b Practice Problem b Lab Exercise (Demo Required)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
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.
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
CS 117 Spring 2002 April 5, Exam 3 April 10 –files, arrays, strings, classes –practice exams are up –review on Monday.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
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.
OOP Languages: Java vs C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Objected Oriented Programming & Design JAVA Shishir Gupta (704) (704)
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Data Abstraction and Object-Oriented Design Problem Solving,
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Methods.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Object Oriented Programming COP3330 / CGS5409.  Class Templates  Bitwise Operators.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Motivation for Generic Programming in C++
TK1924 Program Design & Problem Solving Session 2011/2012
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
PHP Classes and Objects
Week 13: Wrap-up Refining Class Diagram and Implementation
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
CLASSES AND OBJECTS.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Week 15: Refining Class Diagram and Implementation
Presentation transcript:

CS 117 Spring 2002 Class and Function Reuse derived classes templates

CS 117 Spring 2002 Derived Classes Hanly : Section 6.7 Friedman-Koffman Appendix E

5/3/02 Demo Appointments Next Week –graphics (?) –C++ compared to C, Java –Review for final

Yahtzee using classes Die Dice Scorecard Player Game

Die class Attributes –face value Behavior –roll –print –get value

Dice Attributes –5 Die objects Behavior –roll –print –sum values –test for desired configurations –score as particular configuration

Scorecard Attributes –array of scores –bonus chips for extra yahtzee Behavior –total score –print –score a set of dice in a particular configuration –check to see if a configuration has been scored

Player Attributes –name –scorecard Behavior –take turn –choose configuration to score –get score

Game Attributes –array of players Behavior –play –report result

Reusing classes a class can have member variables of a class type –has-a relationship a class can inherit from another class –is-a relationship

When do you need inheritance you have several classes that share attributes and behavior but differ in minor ways you have a working class that is used by many programs and you need to modify it slightly

base class class base { public: // usual public member functions private: // usual public member functions protected: //variables and functions shared ONLY with derived class }

derived class class derived : public base { public: // additional public member functions private: // additional public member functions }

Template classes and functions functions like swap and sort are needed for many different types array and list classes could be used for many classes

template functions template void swap( T & obj1, T& obj2) { T temp = obj1; obj1 = obj2; obj2 = temp; }

11.3 Template Classes Like functions use templates with classes –template Used before each class definition dummy.h is a definition file for a template class dummy.h is the header file and the definition for a template class named dummy

Template Class Syntax Form:template class className { public: private: };

Object Definitions & Templates Form: class-template-name object; –indexList intList; –type can be any defined data type –class-template-name is name of template class –object is what is created of type dummy numDepend; dummy spouseName; –DummyTest.cpp

Key Issues Notice in the next implementation file a template is a prefix to each function heading Also a dummy :: is used to let the compiler know that the function being defined is a member of a template class –class dummy with parameter T