Title: Constructors and Destructors
1Constructors and Destructors
ECE230 Lectures Series
- Ying Wu
- Electrical Engineering Computer Science
- Northwestern University
- yingwu_at_ece.northwestern.edu
2A Class is a blueprint
- Class is the blueprint of a class of packages
3Class vs. Object
- A blueprint can be instantiated to different
packages or objects - Example
- Class COffer
- Title
- Base_salary
- Bonus rate
- Benefit
- Allowance
- Working_load
-
-
- Different people may get different offers, so we
have - COffer John_offer, Mike_offer, Joe_offer
- Then, you may need to specify the values in the
offer for each people
4Question?
- Can we have a way to specify a default one
instead of setting them one by one? - Is there is way to initialize a package in our
need when I create it? - E.g.,
- COffer John_Offer(manager, 80K)?
- Instead of
- Coffer John_Offer
- John_Offer.SetTitle(Manager)
- John_Offer.SetBaseSalary(80K)
5What to learn today?
- Constructor
- Default constructor
- Destructor
- When constructor and destructor are called
- Copy an object
- How to pass an object to a function
6Initializing Class Objects Constructors
- Constructors
- Initialize class members
- Same name as the class
- No return type
- Member variables can be initialized by the
constructor or set afterwards - Passing arguments to a constructor
- When an object of a class is declared,
initializers can be provided - Format of declaration with initializers
- Class-type ObjectName( value1,value2,)
- Default arguments may also be specified in the
constructor prototype
7Example
- CVariableCVariable(const char name, const
double v) -
- m_dValue v
- m_sName new charstrlen(name)1
- strcpy(m_sName, name)
1.0
Void main() CVariable a(var_a,
1.0) CVariable b(v, 3.3)
3.3
8Default Constructor
- Pay more attention!
- Wed better define a default constructor for a
class - i.e., a constructor CVariable() exists
- So that we can use something like
- CVariable var
- On the other hand, if we only have a constructor
CVariable(const charname, double v) - We can only instantiate an object by using
- CVariable var(a,2.3)
- If you do not use a default constructor, the
compiler will create one for you, but it will not
guarantee it is what you want!
!! Can you guess what the compiler will do for
you?
9A safe way!
- What the compiler will do for you
- It is safer to do it yourself by have a default
constructor
- CVariableCVariable()
-
- m_dValue 0.0
- m_sName NULL
-
10Another Way member initializer
- CVariableCVariable()
- m_dValue(0.0),
- m_sName(NULL)
-
- // empty
-
11You might wonder
- In our previous lectures, we learnt DMA
- If you need memory, you new some
- If you dont want them anymore, you should delete
them, and the O/S will recycle them - We know a rule
- Always pair new and delete
- Otherwise, those allocated memory will never be
able to be used by other programs until your
program ends - Then your program might have eaten all the
memories! - Then, lets see
12Memory Leak
I have no idea
I have no idea
- void myFunc()
-
- CVariable tmp(I have no idea!, 1.0)
-
- void main()
-
- for(int k0 klt1000k)
- for(int j0jlt1000j)
- myFunc()
-
-
-
What will happen?
At each myFunc() call, you will waste 16Bytes.
Then before your program ends, you will eat 16M
in total!!!
13How can we solve it?
- Can we have an automatic mechanism
- When an object is no longer needed, if it has
some memory allocated by me, I should recycle
them. - How to do it automatically?
14Destructors
- Destructors
- Are member function of class
- Perform termination housekeeping before the
system reclaims the objects memory - Complement of the constructor
- Name is tilde () followed by the class name
- Recall that the constructors name is the class
name - Receives no parameters, returns no value
- One destructor per class
- No overloading allowed
15Housekeeping!
- CVariableCVariable()
-
- if(m_sName!NULL)
- delete m_sName
-
Note (1) Each class has only ONE destructor
(2) Generally, we dont call the destructor
explicitly. (3) It will be called
ATUOMATICALLY.
Question when constructor and destructors are
called?
16When Constructors and Destructors Are Called
- Constructors and destructors called automatically
- Order depends on scope of objects
- Global scope objects
- Constructors called before any other function
(including main) - Destructors called when main terminates (or exit
function called) - Destructors not called if program terminates with
abort - Automatic local objects
- Constructors called when objects are defined
- Destructors called when objects leave scope
- i.e., when the block in which they are defined is
exited - Destructors not called if the program ends with
exit or abort
17- Class CTest
- CTest()
- CTest()
-
- CTestCTest() cout ltlt Constructor
called!\n) - CTestCTest() cout ltlt Destructor called!\n)
- CTest tg
- void myFunc()
-
- CTest tf
-
- void main()
-
- CTest tm
constructor called! (tg) Constructor
called! (tm) Constructor called! (tf) Destructor
called! (tf) Constructor called! (tm_2) Destructo
r called! (tm_2) Destructor called! (tm) Destructo
r called! (tg)
18Can I have sth like this?
- void main()
-
- CVariable a(var_a, 1.5)
- CVariable b
- b a
19Memberwise Copy
- Assigning objects
- An object can be assigned to another object of
the same type using the assignment operator () - Member by member copy
- Objects may be
- Passed as function arguments
- Returned from functions (call-by-value default)
20Lets go deep!
CVariable a(var_a, 1.5)
CVariable b
b a
1.5
21A problem!
- void main()
-
- CVariable a(var_a, 1.5)
- CVariable b
- b a
-
- a.SetName(change)
- cout ltlt b.Name() ltlt endl
?
How to solve this problem? Keep this question
and well see it next week!
22How to pass an object to functions
- Call-by-value
- involves copying objects (memberwise)
- is good for security (safe), but bad for
performance - Call-by-reference
- passes the reference or the pointer
- does not copy anything
- is good for performance, but bad for security,
because the function can change the object - Call-by-const-reference
- passes a const reference or a const pointer
- is good for both!
- WHY? The function will not be able to change the
object, because it is a const object.
23A BIG Problem!
- void myFunc(CVariable t)
-
- cout ltlt something happen?\n)
-
- void main()
-
- CVariable a(var_a, 1.5)
-
- myFunc(a)
-
- cout ltlt a.Name() ltlt endl
24Another BIG Problem!
- CVariable Create()
-
- CVariable t(var_a, 0.0)
- return t
-
- void main()
-
- CVariable a
- a Create()
-
- cout ltlt a.Name() ltlt endl
-
25Save them for next week!
- Yes, we have big problems!
- Lets solve them next week.
26Questions for today
- Finding errors
- void Time( int )
- Definition of class Time
- class Time
- public
- // function prototypes
- private
- int hour 0
- int mintue 0
- int second 0
-