1 Lab Session-5 CSIT221 Spring 2003 Default and Parameterized Constructors Destructors Programming Exercise for building a template based class (demo required)
2 Constructors Please do not attempt anything within the constructor except for initializing the private data There are two types of constructors: Default (Takes no arguments) Parameterized(Accepts the user data through actual parameters passed)
3 Parameterized The parameterized constructor would initialize the private data with the actual parameters passed at the time an object is defined in the client code As an example, we modify the stack class to introduce a parameterized constructor Code Walk-Through
4 Destructors Destructors perform the opposite function of constructors A destructor is a member functions of a class and it has the same name as the class Destructor runs automatically when an object belonging to its class is destroyed. It ensures “clean up” operations Code Walk-Through
5 Programming Exercise (Demo Required Sec1 3/11 Sec2 3/13 ) Implement the HW2 class as a template based generic class. Define an object in the client code that builds a list of customer names and another object that builds a corresponding list of amounts stored as floating point numbers. Display the names and amounts sorted in descending order by the amounts. Following is an example display from your program:
6 Example Display PayGo Mart: Following is the list of our customers, sorted by amounts spent in last one year: NameAmount Dave4500 Nell3501 Chris3500 Jacob2800 Alice2100
7 Hints for using templates Make sure that you use the line template Above the class definition and the header of every member function Declare the array with the data type template_name Initialize the actual data types “string” and “double” when defining objects for the demo