Engineering Science 1036A/B Lecture Notes - Lecture 25: Special Functions
Initializing Objects Using Constructors
The objects in a program are initialized using a special function (constructor)
The initializations done inside the definition of the constructor function
○
•
This special public member function (can be private in special case) must have
the same name as the class itself
•
Constructors do not have a return type, not even void
•
A constructor function is automatically called when an object is created
•
Like any other function , constructors can be overloaded (multiple functions
with the same name but different parameter list), making it easy to construct
objects with different initial data values
•
No-argument constructor: a constructor without parameters
•
A class can be declared without constructors
A no-argument constructor with an empty body is declared by the
complier
○
This constructor is know as "default constructor"
○
•
A class may contain one or more constructors with parameters with different
parameter lists
These constructors are known as constructor with arguments
○
•
Example: Student class using ‘constructor with no-argument’
Example: Student class using overloaded constructors
Destructor Function
The destructor is a special function that frees up the memory (used by an
object) and releases resources (such as open files) when any object is
destroyed/released from the memory
•
A constructor is invoked when an object is created and a destructor is invoked
when the object is destroyed
•
The name of the destructor contains the character ~ (tilde) followed the class
name
~Student();
○
•
It has no return type, no argument and can not return a value•
A destructor function is automatically called when an object is released (goes
out of scope)
Generated on the last-in first-out basis
Object created last will be released first and so on
§
○
•
Every class has a default destructor if the destructor is not explicitly defined •
implement destructors to perform customized operations •
Each class can have only one destructor (although a class can have more than
one constructor)
destructors can not be overloaded
○
•
No modifier (such as const) can be used with a destructor•
It’s not mandatory to use destructor in the class•
Example: Student class with destructor
Constructors/Destructors
Initializing Objects Using Constructors
The objects in a program are initialized using a special function (constructor)
The initializations done inside the definition of the constructor function
○
•
This special public member function (can be private in special case) must have
the same name as the class itself
•
Constructors do not have a return type, not even void•
A constructor function is automatically called when an object is created•
Like any other function , constructors can be overloaded (multiple functions
with the same name but different parameter list), making it easy to construct
objects with different initial data values
•
No-argument constructor: a constructor without parameters•
A class can be declared without constructors
A no-argument constructor with an empty body is declared by the
complier
○
This constructor is know as "default constructor"
○
•
A class may contain one or more constructors with parameters with different
parameter lists
These constructors are known as constructor with arguments
○
•
Example: Student class using ‘constructor with no-argument’
Example: Student class using overloaded constructors
Destructor Function
The destructor is a special function that frees up the memory (used by an
object) and releases resources (such as open files) when any object is
destroyed/released from the memory
•
A constructor is invoked when an object is created and a destructor is invoked
when the object is destroyed
•
The name of the destructor contains the character ~ (tilde) followed the class
name
~Student();
○
•
It has no return type, no argument and can not return a value•
A destructor function is automatically called when an object is released (goes
out of scope)
Generated on the last-in first-out basis
Object created last will be released first and so on
§
○
•
Every class has a default destructor if the destructor is not explicitly defined •
implement destructors to perform customized operations •
Each class can have only one destructor (although a class can have more than
one constructor)
destructors can not be overloaded
○
•
No modifier (such as const) can be used with a destructor•
It’s not mandatory to use destructor in the class•
Example: Student class with destructor
Constructors/Destructors
Initializing Objects Using Constructors
The objects in a program are initialized using a special function (constructor)
The initializations done inside the definition of the constructor function
○
•
This special public member function (can be private in special case) must have
the same name as the class itself
•
Constructors do not have a return type, not even void•
A constructor function is automatically called when an object is created•
Like any other function , constructors can be overloaded (multiple functions
with the same name but different parameter list), making it easy to construct
objects with different initial data values
•
No-argument constructor: a constructor without parameters•
A class can be declared without constructors
A no-argument constructor with an empty body is declared by the
complier
○
This constructor is know as "default constructor"
○
•
A class may contain one or more constructors with parameters with different
parameter lists
These constructors are known as constructor with arguments
○
•
Example: Student class using ‘constructor with no-argument’
Example: Student class using overloaded constructors
Destructor Function
The destructor is a special function that frees up the memory (used by an
object) and releases resources (such as open files) when any object is
destroyed/released from the memory
•
A constructor is invoked when an object is created and a destructor is invoked
when the object is destroyed
•
The name of the destructor contains the character ~ (tilde) followed the class
name
~Student();
○
•
It has no return type, no argument and can not return a value•
A destructor function is automatically called when an object is released (goes
out of scope)
Generated on the last-in first-out basis
Object created last will be released first and so on
§
○
•
Every class has a default destructor if the destructor is not explicitly defined •
implement destructors to perform customized operations •
Each class can have only one destructor (although a class can have more than
one constructor)
destructors can not be overloaded
○
•
No modifier (such as const) can be used with a destructor•
It’s not mandatory to use destructor in the class•
Example: Student class with destructor
Constructors/Destructors
Document Summary
The objects in a program are initialized using a special function (constructor) The initializations done inside the definition of the constructor function. This special public member function (can be private in special case) must have the same name as the class itself. Constructors do not have a return type, not even void. A constructor function is automatically called when an object is created. Like any other function , constructors can be overloaded (multiple functions with the same name but different parameter list), making it easy to construct objects with different initial data values. A no-argument constructor with an empty body is declared by the complier. A class may contain one or more constructors with parameters with different parameter lists. These constructors are known as constructor with arguments. The destructor is a special function that frees up the memory (used by an object) and releases resources (such as open files) when any object is destroyed/released from the memory.