Constructor Notes Notice name of constructor:DayOfYear Same name as class itself! Constructor declaration has no return-type ◆Not even void! Constructor in public section It's called when objects are declared ◆If private,could never declare objects! Copyright6 Pearson Addison-Wesley.All rights reserved. 7-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-6 Constructor Notes ¨ Notice name of constructor: DayOfYear ¨ Same name as class itself! ¨ Constructor declaration has no return-type ¨ Not even void! ¨ Constructor in public section ¨ It’s called when objects are declared ¨ If private, could never declare objects!
Calling Constructors ◆Declare objects: DayOfYear date1(7,4), date2(5,5); Objects are created here Constructor is called Values in parents passed as arguments to constructor Member variables month,day initialized: date1.month→7date2.month→5 date1.dat→4date2.day→5 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-7 Calling Constructors ¨ Declare objects: DayOfYear date1(7, 4), date2(5, 5); ¨ Objects are created here ¨ Constructor is called ¨ Values in parents passed as arguments to constructor ¨ Member variables month, day initialized: date1.month 7 date2.month 5 date1.dat 4 date2.day 5
Constructor Equivalency ◆Consider: DayOfYear date1,date2 date1.DayOfYear(7,4);/ILLEGAL! date2.DayOfYear(5,5);/ILLEGAL! ◆Seemingly OK. CANNOT call constructors like other nember functions! Copyright006 Pearson Addison-Wesley.All rights reserved. 7-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-8 Constructor Equivalency ¨ Consider: ¨DayOfYear date1, date2 date1.DayOfYear(7, 4); // ILLEGAL! date2.DayOfYear(5, 5); // ILLEGAL! ¨ Seemingly OK. ¨CANNOT call constructors like other member functions!
Constructor Code Constructor definition is like all other member functions: DayOfYear:DayOfYear(int monthValue,int dayValue) month monthValue; day dayValue; Note same name around Clearly identifies a constructor ◆Note no return type Just as in class definition Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-9 Constructor Code ¨ Constructor definition is like all other member functions: DayOfYear::DayOfYear(int monthValue, int dayValue) { month = monthValue; day = dayValue; } ¨ Note same name around :: ¨ Clearly identifies a constructor ¨ Note no return type ¨ Just as in class definition
Alternative Definition Previous definition equivalent to: DayOfYear:DayOfYear( int monthValue, int dayValue) month(monthValue),day(dayValue)< {.} Third line called "Initialization Section" ◆Body left empty Preferable definition version Copyright 2006 Pearson Addison-Wesley.All rights reserved. 7-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-10 Alternative Definition ¨ Previous definition equivalent to: DayOfYear::DayOfYear( int monthValue, int dayValue) : month(monthValue), day(dayValue) {.} ¨ Third line called "Initialization Section" ¨ Body left empty ¨ Preferable definition version