1 / 19

Class and Function Reuse

Class and Function Reuse. derived classes templates. 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.

teigra
Download Presentation

Class and Function Reuse

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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 <class T> 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 <class T> • 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 T> class className { public: private: };

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

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

More Related