Constructors and Destructors - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Constructors and Destructors

Description:

Some people uses the name getData () as an accessor routine ... Same as the class name but is preceded by a tilde (~). E.g. ~Complex ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 17
Provided by: amandaso
Category:

less

Transcript and Presenter's Notes

Title: Constructors and Destructors


1
ConstructorsandDestructors
2
Private Members
  • Mutator routines
  • Sets the value of a private data member
  • E.g. getData (), setData (), setCount ()
  • Accessor routines
  • Returns the value of private data member
  • E.g. putData ()
  • Some people uses the name getData () as an
    accessor routine

3
Initializing 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

4
Constructors
  • 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.

5
Constructors
class Complex float x, y public Complex (
void ) //constructor declared
//constructor defined Complex Complex ( void )
x 0.0 y 0.0
Complex z1
6
Constructors
  • If the programmer does not supply a default
    constructor, compiler provides a default
    constructor.
  • Should be in the public section.
  • No return types.
  • Cannot be inherited.
  • Can have default arguments.

7
Parameterized 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.

8
Parameterized 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
9
Parameterized Constructors
  • Complex z1 //Illegal
  • We must pass the initial values as arguments
    to the parameterized constructor.
  • Two ways
  • 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 )

10
Multiple 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
11
Multiple Constructors
  • Compiler invoke the correct version depends on
  • number of arguments.
  • types of the arguments.
  • Complex z1 //invoke default constructor
  • Complex z2 ( 23.5, 34.5 ) / invoke
    parameterized constructor /
  • Complex z3 ( z2 ) //invoke copy constructor
  • Constructor overloading
  • Once you define a constructor include a
    default constructor too.
  • Cannot create objects without arguments.

12
Constructors 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
13
Default arguments
  • Complex z ( 3.4 ) // x 3.4, y 0.0
  • Complex z ( 2.3, 5.7 ) // x 2.3, y 5.7

14
Copy Constructor
  • Used to declare and initialize an object from
    another.
  • Complex z3 ( z2 )
  • 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.

15
Destructor
  • Used to destroy the objects.
  • Same as the class name but is preceded by a
    tilde ().
  • E.g.
  • Complex ()

16
Destructor
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com