It111 Lecture08
It111 Lecture08
LECTURE 08
2
Center for Information and Communication Technology
4
Center for Information and Communication Technology
8
Center for Information and Communication Technology
Constructors
A C++ class uses variables to define data fields and
functions to define behaviors.
Additionally, a class provides functions of a special type,
known as constructors, which are invoked when a new
object is created.
A constructor is a special kind of function. Constructors
can perform any action, but they are designed to perform
initializing actions, such as initializing the data fields of
objects.
9
Center for Information and Communication Technology
Constructors
This figure shows an
example of the class
for Circle objects
A class is a blueprint
that defines objects of
the same type
10
Center for Information and Communication Technology
Constructors
A constructor is invoked to create an object.
Constructors are a special kind of function, with three
peculiarities:
Constructors must have the same name as the class
itself.
Constructors do not have a return type—not even void.
Constructors are invoked when an object is created.
Constructors play the role of initializing objects.
The constructor has exactly the same name as the
defining class.
11
Center for Information and Communication Technology
Constructors
The constructor has exactly the same name as the defining
class. Like regular functions, constructors can be overloaded
(i.e., multiple constructors with the same name but different
signatures), making it easy to construct objects with different
sets of data values.
It is a common mistake to put the void keyword in front of a
constructor.
For example,
void Circle()
{
}
12
Center for Information and Communication Technology
Constructors
Most C++ compilers will report an error, but some will
treat this as a regular function, not as a constructor.
Constructors are for initializing data fields. The data field
radius does not have an initial value, so it must be
initialized in the constructor
Variable (local or global) can be declared and initialized
in one statement, but as a class member, a data field
cannot be initialized when it is declared
13
Center for Information and Communication Technology
14
Center for Information and Communication Technology
Local Variable
A variable defined inside a function (defined inside
function body between braces) is called a local variable or
automatic variable.
Its scope is only limited to the function where it is
defined.
Local variable exists and can be accessed only inside a
function.
The life of a local variable ends (It is destroyed) when the
function exits.
15
Center for Information and Communication Technology
16
Center for Information and Communication Technology
Global Variable
If a variable is defined outside all functions, then it is
called a global variable.
The scope of a global variable is the whole program.
It can be used and changed at any part of the program
after its declaration.
Its life ends only when the program ends.
17
Center for Information and Communication Technology
In this program, c is a
global variable.
18
Center for Information and Communication Technology
19
Center for Information and Communication Technology
Output of a program
if var was specified
as static variable
Output
1
1
Output of a program
if var was not
specified as static
variable
Output
1
2
20
Center for Information and Communication Technology
Class clockType
The following statements define the class clockType:
class clockType
{
public:
void setTime(int, int, int);
void getTime(int&, int&, int&) const;
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType&) const;
private:
int hr;
int min;
int sec;
}; 22
Center for Information and Communication Technology
23
Center for Information and Communication Technology
24
Center for Information and Communication Technology
25
Center for Information and Communication Technology
27
Center for Information and Communication Technology
30
Center for Information and Communication Technology
32