Constructor Additional Purpose .Not just initialize data Body doesn't have to be empty In initializer version ◆Validate the data! Ensure only appropriate data is assigned to class private member variables Powerful OOP principle Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-11 Constructor Additional Purpose ¨ Not just initialize data ¨ Body doesn’t have to be empty ¨In initializer version ¨ Validate the data! ¨Ensure only appropriate data is assigned to class private member variables ¨Powerful OOP principle
Overloaded Constructors Can overload constructors just like other functions Recall:a signature consists of: ◆Name of function ◆Parameter list Provide constructors for all possible argument-lists Particularly "how many" Copyright006 Pearson Addison-Wesley.All rights reserved. 7-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-12 Overloaded Constructors ¨ Can overload constructors just like other functions ¨ Recall: a signature consists of: ¨Name of function ¨Parameter list ¨ Provide constructors for all possible argument-lists ¨Particularly "how many
Class with Constructors Example: Display 7.1 Class with Constructors (1 of 3) Display 7.1 Class with Constructors 1 #include <iostream> This definition of DayofYear is an improved #include <cstdlib>//for exit version of the class DayofYear given in Display 3 using namespace std; 6.4 4 class DayofYear 5 6 public: 7 DayofYear(int monthValue,int dayValue); //Initializes the month and day to arguments. 9 DayofYear(int monthValue); 10 //Initializes the date to the first of the given month 11 DayofYear() default constructor 12 //Initializes the date to January 1. 13 void input(); 14 void output(); 1 int getMonthNumber(); 6 //Returns 1 for January,2 for February,etc. Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-13 Class with Constructors Example: Display 7.1 Class with Constructors (1 of 3)
Class with Constructors Example: Display 7.1 Class with Constructors(2 of 3) 17 int getDay(); private: 19 int month; This causes a call to the default 20 int day; constructor.Notice that there void testDate(); 22 are no parentheses. }; 23 int main() 24{ 25 DayOfYear datel(2,21),date2(5),date3; 2 cout <"Initialized dates:\n"; 27 datel.output()cout <endl; date2.output()cout <endl; 29 date3.output();cout <endl; an explicit call to the constructor 30 date1 DayofYear(10,31); DayOfYear:DayOfYear 3 cout <<"datel reset to the following:\n"; 32 datel.output()cout <endl; return 0; 3 35 3 DayofYear:DayOfYear(int monthValue,int dayValue) 37 month(monthValue),day(dayValue) 38 39 testDate() 40 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-14 Class with Constructors Example: Display 7.1 Class with Constructors (2 of 3)
Class with Constructors Example: Display 7.1 Class with Constructors(3 of 3) Display 7.1 Class with Constructors 41 DayOfYear:DayofYear(int monthValue):month(monthValue),day(1) 42 testDate(); 44 DayOfYear:DayOfYear():month(1),day(1) {} 47 //uses iostream and cstdlib: 48 void DayofYear:testDate() 49 50 if ((month 1)(month 12)) cout <"Illegal month value!\n"; 3456 exit(1); if ((day <1)(day 31)) <Definitions of the other member 76859 cout <"Illegal day value!\n"; functions are the same as in Display exit(1); 6.4. 0 SAMPLE DIALOGUE Initialized dates: February 21 May 1 January 1 datel reset to the following: October 31 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-15
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-15 Class with Constructors Example: Display 7.1 Class with Constructors (3 of 3)