Classes, Member Functions Constructors and Destructors - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Classes, Member Functions Constructors and Destructors

Description:

Except members in a class are 'private' by default while members in a struct are ' ... If so, prepend T:: to the variable name and use the static data member ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 8
Provided by: craigm6
Category:

less

Transcript and Presenter's Notes

Title: Classes, Member Functions Constructors and Destructors


1
Classes, Member Functions Constructors and
Destructors
  • A bullet list of the things to know

2
Class
  • class struct
  • Except members in a class are private by
    default while members in a struct are public by
    default, but who cares
  • a class is a type
  • The design philosophy of C is that you can use
    any type just like you use int

3
Member Functions
  • A function declared inside the class definition
    is a member function
  • Exception static member functions are not
    really member functions, more on that later
  • Member functions have an implicit parameter
    (typically its the first parameter)
  • The implicit parameter is handled automatically
    by the compiler (we dont declare it)
  • The name of the parameter is this
  • The type of the parameter is T where T is the
    name of the class

4
Member Functions Cont.
  • We pass the implicit argument to a member
    function using the x.doit() syntax
  • x must be a variable of type T (i.e., an object
    of the appropraite class)
  • the value of the implicit argument is x
  • The implicit parameter is used for class
    scoping
  • When the compiler is searching for a declaration
    for some variable that youre using it searches
    in this order
  • Is there a local variable with that name? If so,
    use the local variable
  • If not, is there a parameter with that name? If
    so, use the parameter
  • If not, is there a data member with that name?
    If so, prepend (this). to the variable name
    and use the data member pointed to by this
  • If not, is there a static data member with that
    name? If so, prepend T to the variable name
    and use the static data member
  • If not, is there a global variable with that
    name? If so, use the global variable.
  • What this means in practice is that you usually
    wont have to use this in your programs, it
    will be completely implicit. PLEASE DO NOT
    FORGET HOW IT REALLY WORKS!

5
Constructors
  • Constructors are functions provided to initialize
    the data members of objects.
  • A class may have many constructors
  • provided each constructors has different
    parameters
  • If the class has one or more constructors
    defined, then anyone who creates a variable of
    that class will have to call the constructor
  • i.e., the compiler enforces/ensures that class
    objects are always initialized properly. This is
    a really good thing.

6
Constructors Cont
  • The name of the constructor function must be the
    same as the name of the class
  • The constructor function must be a member
    function
  • And you can use this if you want
  • We call the constructor whenever we create a
    variable of that type
  • We put the arguments to the constructor inside
    parens after the variable name
  • e.g., T x(1, 2) // declares variable x, 1 and 2
    are arguments to the constructor for T that has
    two int parameters
  • If it matters (and it usually does not for well
    designed programs) then you can rely on the fact
    that variables are constructed in the order
    they are declared.
  • Variables defined in different files are
    constructed in an arbitrary order!

7
Destructors
  • Used to provide clean up for classes
  • This almost always means deallocate the chunks
    of memory pointed to by data members within this
    class.
  • A class may have only one destructor
  • The name of the destructor is TT
  • i.e., the same as the class name but with in
    front
  • and the destructor can have NO parameters
  • The compiler will automatically call the
    destructor when the activation record containing
    the variable is destroyed (i.e, when the function
    returns)
  • Global variables are destroyed when main ends
  • If it matters (and it shouldnt for well-written
    programs) you can rely on the fact that variables
    are destructed in the opposite order that they
    are constructed (i.e., LIFO)
Write a Comment
User Comments (0)
About PowerShow.com