Title: Constructors and Destructors
1ConstructorsandDestructors
2Private Members
- Sets the value of a private data member
- E.g. getData (), setData (), setCount ()
- Returns the value of private data member
- Some people uses the name getData () as an
accessor routine
3Initializing Private Members
- Used mutator functions to initialize private
data members.
- E.g. x.getData ( 100, 75.4 )
- Can be invoked only via an already created
object.
- Cannot be used to initialize members while
creating an object.
- Not agree with C philosophy.
- E.g. Initializing built-in data types
- int i 24
- float x 5.25
4Constructors
- Enables an object to initialize itself when it
is created.
- Special member function whose task is to
initialize objects.
- Name is same as the class name.
- Invoked automatically whenever an object is
created.
- No need to call like normal member functions.
5Constructors
class Complex float x, y public Complex (
void ) //constructor declared
//constructor defined Complex Complex ( void )
x 0.0 y 0.0
Complex z1
6Constructors
- If the programmer does not supply a default
constructor, compiler provides a default
constructor.
- Should be in the public section.
- Can have default arguments.
7Parameterized Constructors
- Default constructor Complex () initializes
only data members to zero.
- May be needed to initialize to various values
for different objects.
- Soln Parameterized constructors.
- Pass arguments to constructors when the
objects are created.
- Can have more than one parameterize
constructors for a one class.
- However, they should have different signatures.
8Parameterized Constructors
class Complex float x, y public //parameteri
zed constructor Complex ( float a, float b
)
Complex Complex ( float a, float b ) x a
y b
9Parameterized Constructors
- We must pass the initial values as arguments
to the parameterized constructor.
- By calling the constructor explicitly.
- E.g. Complex z1 Complex ( 3.0, 4.0 )
- By calling the constructor implicitly.
- E.g. Complex z1 ( 3.0, 4.0 )
10Multiple Constructors in a class
class Complex int x, y public Complex ( )
//default constructor x 0.0 y
0.0 Complex ( float a, float b )
//parameterized constructor x a y
b Complex ( Complex z ) //copy
constructor x z.x y z.y
11Multiple Constructors
- Compiler invoke the correct version depends on
- Complex z1 //invoke default constructor
- Complex z2 ( 23.5, 34.5 ) / invoke
parameterized constructor / - Complex z3 ( z2 ) //invoke copy constructor
- Once you define a constructor include a
default constructor too.
- Cannot create objects without arguments.
12Constructors with default args
class Complex int x, y public Complex ( )
//default constructor x 0.0 y
0.0 Complex ( float a, float b 0.0 )
//parameterized constructor x a y
b Complex ( Complex z ) //copy
constructor x z.x y z.y
13Default arguments
- Complex z ( 3.4 ) // x 3.4, y 0.0
- Complex z ( 2.3, 5.7 ) // x 2.3, y 5.7
14Copy Constructor
- Used to declare and initialize an object from
another.
- Other situations where copy constructor calls
- When you pass an object by value, the
constructor initializes the corresponding
formal argument.
- When you have a function return an object, the
constructor initializes a temporary object used
to convey the objects values to the calling
program.
15Destructor
- Used to destroy the objects.
- Same as the class name but is preceded by a
tilde ().
16Destructor
- A destructor never takes any arguments nor
returns a value.
- Invoked by the compiler upon exit from the
program (or block or function as the case may
be) to clean up storage that is no longer
accessible.
- It is a good practice to declare destructors
in a program since it releases the memory space
for future use.