ch07-2- constructors, destructor and other tools...
TRANSCRIPT
Ch 7-2. Constructors, Destructor and Other Tools
2014-1 Programming Language
May 10, 2014
Prof. Young-Tak KimAdvanced Networking Technology Lab. (YU-ANTL)
Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA
(Tel : +82-53-810-2497; Fax : +82-53-810-4742http://antl.yu.ac.kr/; E-mail : [email protected])
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 2
Outline
ConstructorsDefinitionsCalling
Destructor
More Toolsconst parameter modifierInline functionsStatic member data
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 3
Constructor (생성자)
Initialization of objectsInitialize some or all member variablesOther actions possible as well
A special kind of member functionAutomatically called when object declared
Very useful toolKey principle of OOP
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 4
Constructor Definitions
Constructors defined like any member functionExcept:
1) Must have same name as class
2) Cannot return a value; not even void!
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 5
Constructor Definition Example
Class definition with constructor:class DayOfYear{public:
DayOfYear(int monthValue, int dayValue);//Constructor initializes month & day
void input();void output();…
private:int month;int day;
}
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 6
Constructor Notes
Notice name of constructor: DayOfYearSame name as class itself!
Constructor declaration has no return-typeNot even void!
Constructor in public sectionIt’s called when objects are declaredIf private, could never declare objects!
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 7
Calling Constructors
Declare objects:DayOfYear date1(7, 4),
date2(5, 5);
Objects are created hereConstructor is calledValues in parens passed as arguments to constructorMember variables month, day initialized:date1.month 7 date2.month 5date1.dat 4 date2.day 5
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 8
Constructor Equivalency
Consider:DayOfYear date1, date2date1.DayOfYear(7, 4); // ILLEGAL!date2.DayOfYear(5, 5); // ILLEGAL!
Seemingly OK…CANNOT call constructors like other member functions!
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 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 typeJust as in class definition
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 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
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 11
Additional Purpose of Constructor
Not just initialize data
Body doesn’t have to be emptyIn initializer version
Validate the data!Ensure only appropriate data is assigned to class private member variablesPowerful OOP principle
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 12
Overloaded Constructors
Can overload constructors just like other functions
Recall: a signature consists of:Name of functionParameter list
Provide constructors for all possible argument-lists
Particularly "how many"
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 13
Class with Constructors Example
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 14
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 15
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 16
Constructor with No Arguments
Can be confusingStandard functions with no arguments:
Called with syntax: callMyFunction();Including empty parentheses
Object declarations with no "initializers":DayOfYear date1; // This way!DayOfYear date(); // NO!
What is this really?Compiler sees a function declaration/prototype!Yes! Look closely!
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 17
Explicit Constructor Calls
Can also call constructor AGAINAfter object declared
Recall: constructor was automatically called then
Can call via object’s name; standard member function call
Convenient method of setting member variables
Method quite different from standard member function call
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 18
Explicit Constructor Call Example
Such a call returns "anonymous object"Which can then be assigned
In Action:DayOfYear holiday(7, 4);
Constructor called at object’s declarationNow to "re-initialize":holiday = DayOfYear(5, 5);– Explicit constructor call– Returns new "anonymous object"– Assigned back to current object
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 19
Default Constructor
Defined as: constructor w/ no arguments
One should always be defined
Auto-Generated?Yes & NoIf no constructors AT ALL are defined YesIf any constructors are defined No
If no default constructor:Cannot declare: MyClass myObject;
With no initializers
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 20
Destructor (소멸자)
Opposite of constructorAutomatically called when object is out-of-scopeDefault version only removes ordinary variables, not dynamic variables
Defined like constructor, just add ~MyClass::~MyClass(){
//Perform delete clean-up duties}
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 21
Destructor Need
Dynamically-allocated variablesDo not go away until "deleted"
If pointers are only private member dataThey dynamically allocate "real" data in constructorMust have means to "deallocate" when object is destroyed
Answer: destructor!
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 22
Class Type Member Variables
Class member variables can be any typeIncluding objects of other classes!
Type of class relationshipPowerful OOP principle
Need special notation for constructorsSo they can call "back" to member object’s constructor
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 23
Class Member Variable Example:
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 24
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 25
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 26
Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)
Programming LanguageProf. Young-Tak Kimch 7-2 - 27