simple classes in c n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Simple Classes in C# PowerPoint Presentation
Download Presentation
Simple Classes in C#

Loading in 2 Seconds...

play fullscreen
1 / 14

Simple Classes in C# - PowerPoint PPT Presentation


  • 89 Views
  • Uploaded on

Simple Classes in C#. CSCI 293 September 12, 2005. Today's Objectives. Terminology Scope of Variables and Methods public, private, and more Constructors, Initializers, and Destructors Instance Variables and Class (static) Variables.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Simple Classes in C#' - ranit


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
simple classes in c

Simple Classes in C#

CSCI 293

September 12, 2005

today s objectives
Today's Objectives
  • Terminology
  • Scope of Variables and Methods public, private, and more
  • Constructors, Initializers, and Destructors
  • Instance Variables and Class (static) Variables
terminology of c classes
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
limiting scope of fields
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
constructors
Constructors
  • Just like C++

public class list

{

private int size; // variable

public list () // constructor

{

size = 0;

}

  • Constructors are optional in C#
    • this constructor sets size=0, which is the default anyway
initializers
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

}

creating an object
Creating an Object
  • Don't forget to call new()

class MainProgram

{

static void Main()

{

list mylist1 = new list();

list mylist2 = new list(50);

...

passing parameters
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);

this this object
"this" = this object

public class list

{

private int size;

...

public set_size (int size)

{

this.size = size;

}

instance variables vs class variables
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.

static constructors
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;

}

destructors
Destructors
  • Not necessary to call free(), because garbage collection is automatic.
  • Example Destructor

~WarningBox()

{

boxcount--;

}

properties
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;

q u i z
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;" ?