CMPUT 201: Lab 6 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CMPUT 201: Lab 6

Description:

Creates a new hobbit and initializes its health and stamina ... stamina = -1; The Destructor is called at the end of a function ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 13
Provided by: nla5
Category:
Tags: cmput | lab

less

Transcript and Presenter's Notes

Title: CMPUT 201: Lab 6


1
CMPUT 201 Lab 6
Constructors Destructors
October 24th, 2002
2
Constructors
  • When using C-style structs, you must remember
    to initialize every field in a dedicated function
    after declaration
  • This is error-prone you might forget to do
    this
  • When using C classes, you can write a special
    function called a Constructor that gets called
    every time an object of that class is created
  • In the Constructor, you can initialize the
    objects variables in the appropriate manner so
    declaration and initialization are done in one
    line

3
Declaration of Constructors
  • Constructors always have the same name as the
    class and must not return anything
  • You can have multiple constructors for a class,
    each of which initializes a new objects
    variables in a different way
  • Constructors that take arguments are called
    Value Constructors
  • A constructor that takes no arguments is called
    the Default Constructor (only one per class)

4
Declaration of Constructors
  • Consider the following class in hobbit.h
  • class Hobbit public // Default
    constructor Hobbit() // Value
    constructors Hobbit(char hname)
    Hobbit(int hp, int st) Hobbit(char hname,
    int hp, int st)private char name16
    int health_points int stamina

5
Implementation of Constructors
  • Constructors are implemented in hobbit.cpp
  • // Creates a new hobbit and initializes its
    nameHobbitHobbit(char hname) name new
    char strlen(hname) 1 strcpy(name,
    hname)// Creates a new hobbit and
    initializes its health and stamina// Uses an
    alternative mechanism for doing
    soHobbitHobbit(int hp, int st)
    health_points(hp), stamina(st)

6
Constructor Invocation
  • Constructors are invoked in three cases
  • 1. When you declare an object
  • // calls first value constructorHobbit
    hob1(frodo) // calls default
    constructorHobbit hobPtr new Hobbit
  • When you create a new object and assign it to an
    existing variable
  • Creature c1...// calls third value
    constructorc1 Hobbit(samwise,40,50)

7
Constructor Invocation
  • When you pass an object to a function by value
  • promoteLevel(hob1)
  • Passing an object by value to a function invokes
    a special constructor called the Copy Constructor
    (only one per class)
  • The Copy Constructor takes one reference argument
    that is the same type as the class
  • HobbitHobbit( const Hobbit source )
  • name source.name
  • health_points source.health_points
  • stamina source.stamina

8
Passing User-Defined Objects by Value
  • When an argument is passed by value, a local
    variable of the given type is created in the
    invoked function
  • The variable is initialized with the value of the
    corresponding argument in the function call
  • // in main function
  • Hobbit hob1(frodo)
  • ...
  • // frodo is passed by value
  • promoteLevel( hob1 )

9
Passing User-Defined Objects by Value
  • // in another part of the code
  • promoteLevel( Hobbit hobToPromote ) ...
  • A new Hobbit object hobToPromote is created
  • and initialized with the object hob1
  • The argument binding in the above function is
    equivalent to
  • Hobbit hobToPromote hob1
  • The particular constructor called depends on what
    the local variable is initialized with

10
The Assignment Operator
  • Useful to overload the assignment operator
    when you wish to assign one instance of a class
    to another
  • The assignment statement has the form hob3
    hob1
  • Where hob3 and hob1 are existing objects of
    type Hobbit
  • Initialization is not the same as assignment
    since a new object is being created in the first
    case and an existing redefined in the second case

11
Destructors
  • In the example class, there was a private
    instance variable of type char, which means the
    programmer cannot access it and do a delete
  • The Destructor function handles this
  • A Destructor is a special function that gets
    invokes when an object leaves scope
  • Destructors have the same name as the class
    preceded by a tilda and must not return
    anything

12
Declaration Implementation of Destructor
  • In hobbit.h
  • Class Hobbit public // Default
    constructor Hobbit() ...
  • // Destructor Hobbit() ...
  • In hobbit.cpp
  • HobbitHobbit() // return dynamic memory
    associated // with the name field
    delete(name) health_points -1 stamina
    -1

The Destructor is called at the end of a
functionwith a local variable of type Hobbit
or when the programmer issues a delete hob1
command
Write a Comment
User Comments (0)
About PowerShow.com