Simple Classes in C - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Simple Classes in C

Description:

class = elephant, object = Dumbo. Classes contain. methods - functions. fields ... only list objects can see size, default is private. protected int size; ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 15
Provided by: StephenD3
Category:

less

Transcript and Presenter's Notes

Title: Simple Classes in C


1
Simple Classes in C
  • CSCI 293
  • September 12, 2005

2
Today's Objectives
  • Terminology
  • Scope of Variables and Methods
    public, private, and more
  • Constructors, Initializers, and Destructors
  • Instance Variables and
    Class (static) Variables

3
Terminology of C Classes
  • an object is a particular instance of a class
  • class elephant, object Dumbo
  • Classes contain
  • methods - functions
  • fields - member variables
  • properties - methods that looks like fields to
    users of the method

4
Limiting Scope of Fields
  • Given a class named "list" with a variable named
    "size"
  • public int size
  • everyone can see size
  • private int size
  • only list objects can see size, default is
    private
  • protected int size
  • list objects and objects derived from list can
    see size
  • internal and protected internal

5
Constructors
  • Just like C
  • public class list
  • private int size // variable
  • public list () // constructor
  • size 0
  • Constructors are optional in C
  • this constructor sets size0, which is the
    default anyway

6
Initializers
  • Since constructors are optional
  • public class list
  • private int maxsize 10 // set default
  • public list ()
  • maxsize 10 // redundant
  • public list (int newmax)
  • maxsize newmax // override default

7
Creating an Object
  • Don't forget to call new()
  • class MainProgram
  • static void Main()
  • list mylist1 new list()
  • list mylist2 new list(50)
  • ...

8
Passing Parameters
  • void find_max (int size, ref int max)
  • search the array and set max
  • ...
  • // pass 10 by value and new_max by reference
  • find_max (10, ref new_max)

9
"this" this object
  • public class list
  • private int size
  • ...
  • public set_size (int size)
  • this.size size

10
Instance Variables vs Class Variables
  • Sometimes, you need a variable that belongs to
    the entire class, not one variable for each
    object.
  • public class WarningBox
  • private string message
  • private static int boxcount
  • Each WarningBox instance has its own message,
    but there is only one boxcount for all WarningBox
    objects.

11
Static Constructors
  • Operate on the class, not an individual object.
  • Only called once, even with multiple instances of
    a class.
  • Not allowed to access non-static variables.
  • public class WarningBox
  • private string message
  • private static int boxcount
  • static WarningBox()
  • boxcount 0

12
Destructors
  • Not necessary to call free(), because garbage
    collection is automatic.
  • Example Destructor
  • WarningBox()
  • boxcount--

13
Properties
  • Properties look like variables to the class user,
    but they look like methods to class programmer.
  • private int securitylevel // object variable
  • ...
  • public int SecurityLevel // property
  • get
  • //search database for level
  • return level
  • set
  • securitylevel value
  • --- inside main() ---
  • mymessage.SecurityLevel 5

14
Q u i z
  • Are variables and methods public or private by
    default?
  • Why do I need a constructor if I can initialize
    fields when I declare them?
  • How is a "class" variable declared?
  • Given "private static int BoxCount" is this
    legal "this.BoxCount 0" ?
Write a Comment
User Comments (0)
About PowerShow.com