190 likes | 280 Views
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.
E N D
Class and Function Reuse derived classes templates CS 117 Spring 2002
Derived Classes Hanly : Section 6.7 Friedman-Koffman Appendix E CS 117 Spring 2002
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 <class T> void swap( T & obj1, T& obj2) { T temp = obj1; obj1 = obj2; obj2 = temp; }
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
Template Class Syntax Form: template <class T> class className { public: private: };
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
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