ch07-2- constructors, destructor and other tools...

27
Ch 7-2. Constructors, Destructor and Other Tools 2014-1 Programming Language May 10, 2014 Prof. Young-Tak Kim Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742 http://antl.yu.ac.kr/; E-mail : [email protected])

Upload: others

Post on 31-May-2020

48 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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])

Page 2: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 3: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 4: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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!

Page 5: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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;

}

Page 6: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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!

Page 7: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 8: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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!

Page 9: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 10: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 11: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 12: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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"

Page 13: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 13

Class with Constructors Example

Page 14: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 14

Page 15: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 15

Page 16: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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!

Page 17: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 18: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 19: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 20: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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}

Page 21: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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!

Page 22: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

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

Page 23: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 23

Class Member Variable Example:

Page 24: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 24

Page 25: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 25

Page 26: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 26

Page 27: ch07-2- Constructors, Destructor and Other Tools YTK-140510contents.kocw.net/KOCW/document/2014/Yeungnam/Kim... · Constructors, Destructor and Other Tools 2014-1 Programming Language

Advanced Networking Tech. Lab.Yeungnam University (YU-ANTL)

Programming LanguageProf. Young-Tak Kimch 7-2 - 27