cs360 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS360 PowerPoint Presentation
Download Presentation
CS360

Loading in 2 Seconds...

play fullscreen
1 / 21

CS360 - PowerPoint PPT Presentation


  • 122 Views
  • Uploaded on

CS360. Windows Programming. Last Week. We Covered the principles behind web services Introduced the .NET framework Reviewed object-oriented programming Started learning about C#. C# Statements and Expressions. Very similar to C++, with some changes to increase robustness

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 'CS360' - jiro


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
cs360

CS360

Windows Programming

CS360 Windows Programming

last week
Last Week
  • We
    • Covered the principles behind web services
    • Introduced the .NET framework
    • Reviewed object-oriented programming
    • Started learning about C#

CS360 Windows Programming

c statements and expressions
C# Statements and Expressions
  • Very similar to C++, with some changes to increase robustness
    • No ‘->’ or ‘::’; all qualifications use ‘.’
    • Local variables must be initialized before use
    • if, while, do require bool condition
    • goto can’t jump into blocks
    • switch statement – no fall through (empty case OK)
    • case labels can be strings

CS360 Windows Programming

types
Types
  • FCL is a library of types
    • Classes – today
    • Structs – today
    • Interfaces – next time
    • Enumerations – next time
    • Delegates – next time

CS360 Windows Programming

classes
Classes
  • Classes contain
    • Fields - data members in C++
    • Methods - member functions in C++
    • Properties - expose data using get and set
    • Events - define notifications that class can hire

CS360 Windows Programming

example
Example

class Rectangle

{

// Fields

protected int width = 1;

protected int height = 1;

// Methods (constructors)

public Rectangle () {}

public Rectangle (int cx, int cy)

{

Width = cx;

Height = cy;

}

}

CS360 Windows Programming

example cont
Example (cont.)

// Properties

public int Width

{

get { return width; }

set

{

if (value > 0)

width = value;

else

throw new ArgumentOutOfRangeException (

"Width must be 1 or higher");

}

}

CS360 Windows Programming

example cont1
Example (cont.)

public int Height

{

get { return height; }

set

{

if (value > 0)

height = value;

else

throw new ArgumentOutOfRangeException (

"Height must be 1 or higher");

}

}

public int Area

{

get { return width * height; }

}

CS360 Windows Programming

example cont2
Example (cont.)

Rectangle rect = new Rectangle ();

Rectangle rect = new Rectangle (3, 4);

rect.Width *= 2;

int area = rect.Area;

CS360 Windows Programming

structs
Structs

struct Point

{

public int x;

public int y;

public Point (int x, int y)

{

this.x = x;

this.y = y;

}

}

CS360 Windows Programming

reference and value types
Reference and Value Types
  • Classes are reference types
  • Structs are value types

CS360 Windows Programming

common type system
Common Type System

Object

Value

Reference

Built-in (primitive) data types

Examples: int, double, …

Size is specific

Allocated on Stack

Assignment copies value

Cannot derive from other types

User defined types: structs

Deallocated when defining block exits

Examples: Classes, Arrays,

Interfaces, Delegates

Allocated on managed heap

Assignment copies reference

Garbage collected

CS360 Windows Programming

stack and heap
Stack and Heap
  • Memory is a collection of bytes
  • Imagine each box as one byte long
  • Each byte can hold a number between 0-255. Why?
  • Each byte has an address

CS360 Windows Programming

stack and heap1
Stack and Heap
  • Whenever a variable is created it is added on to the stack
  • Variables are added sequentially (i.e. l-r, t-b)
  • No value for an empty byte
  • No holes allowed in memory. Why?
  • Stack pointer pointsto end of usedmemory

A

B

C

CS360 Windows Programming

stack and heap2
Stack and Heap
  • Problem: what do we do with dynamic variables?
  • Solution: use a heap
  • Stack is used forfixed size variables only
  • Stack pointer infirst four bytes ofstack

Stack pointer

A

B

B

B

C

CS360 Windows Programming

stack and heap3
Stack and Heap
  • Variables created on the heap are scattered throughout the heap
  • Heap contains holes
  • Each variable has a header listing
    • Size of variable
    • Address of nextvariable

stack

heap

CS360 Windows Programming

example1
Example

Heap

Stack

Static void Main()

{

Ref r1 = new Ref();

Val v1 = new Val();

}

Class Ref

{

public int x;

}

struct Val

{

public int x;

}

CS360 Windows Programming

example2
Example

Heap

Stack

Static void Main()

{

Ref r1 = new Ref();

Val v1 = new Val();

}

Class Ref

{

public int x;

}

struct Val

{

public int x;

}

r1

x = 0

v1

x = 0

CS360 Windows Programming

example3
Example

Heap

Stack

Static void Main()

{

Ref r1 = new Ref();

Val v1 = new Val();

r1.x = 5;

v1.x = 5;

}

Class Ref

{

public int x;

}

struct Val

{

public int x;

}

r1

x = 5

v1

x = 5

CS360 Windows Programming

example4
Example

Heap

Stack

Static void Main()

{

Ref r1 = new Ref();

Val v1 = new Val();

r1.x = 5;

v1.x = 5;

Ref r2 = r1;

Val v2 = v1;

}

Class Ref

{

public int x;

}

struct Val

{

public int x;

}

r1

x = 5

v1

x = 5

r2

v2

x = 5

CS360 Windows Programming

summary
Summary
  • Completed p. 27 - 38
  • Next time we will complete chapter 2

CS360 Windows Programming