Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 117 Spring 2002 Class and Function Reuse derived classes templates."— Presentation transcript:

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

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

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

4 Yahtzee using classes Die Dice Scorecard Player Game

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

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

7 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

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

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

10 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

11 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

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

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

14 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

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

16 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

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

18 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

19 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


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

Similar presentations


Ads by Google