1 9/6/05cs360 windows programming cs360 windows programming
TRANSCRIPT
![Page 1: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/1.jpg)
9/6/05 CS360 Windows Programming 1
CS360
Windows Programming
![Page 2: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/2.jpg)
9/6/05 CS360 Windows Programming 2
Last Week
We o Covered the principles behind web services
o Introduced the .NET framework
o Reviewed object-oriented programming
o Started learning about C#
![Page 3: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/3.jpg)
9/6/05 CS360 Windows Programming 3
C# Statements and Expressions
Very similar to C++, with some changes to increase robustnesso No ‘->’ or ‘::’; all qualifications use ‘.’
o Local variables must be initialized before use
o if, while, do require bool condition
o goto can’t jump into blocks
o switch statement – no fall through (empty case OK)
o case labels can be strings
![Page 4: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/4.jpg)
9/6/05 CS360 Windows Programming 4
Types
FCL is a library of typeso Classes – today
o Structs – today
o Interfaces – next time
o Enumerations – next time
o Delegates – next time
![Page 5: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/5.jpg)
9/6/05 CS360 Windows Programming 5
Classes
Classes containo Fields - data members in C++
o Methods - member functions in C++
o Properties - expose data using get and set
o Events - define notifications that class can hire
![Page 6: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/6.jpg)
9/6/05 CS360 Windows Programming 6
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;
}
}
![Page 7: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/7.jpg)
9/6/05 CS360 Windows Programming 7
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"); } }
![Page 8: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/8.jpg)
9/6/05 CS360 Windows Programming 8
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; } }
![Page 9: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/9.jpg)
9/6/05 CS360 Windows Programming 9
Example (cont.)
Rectangle rect = new Rectangle ();
Rectangle rect = new Rectangle (3, 4);
rect.Width *= 2;
int area = rect.Area;
![Page 10: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/10.jpg)
9/6/05 CS360 Windows Programming 10
Structs
struct Point
{
public int x;
public int y;
public Point (int x, int y)
{
this.x = x;
this.y = y;
}
}
![Page 11: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/11.jpg)
9/6/05 CS360 Windows Programming 11
Reference and Value Types
Classes are reference types
Structs are value types
![Page 12: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/12.jpg)
9/6/05 CS360 Windows Programming 12
Common Type System
Object
Value Reference
Built-in (primitive) data typesExamples: int, double, …Size is specificAllocated on StackAssignment copies valueCannot derive from other typesUser defined types: structsDeallocated when defining block exits
Examples: Classes, Arrays,Interfaces, DelegatesAllocated on managed heapAssignment copies referenceGarbage collected
![Page 13: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/13.jpg)
9/6/05 CS360 Windows Programming 13
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
![Page 14: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/14.jpg)
9/6/05 CS360 Windows Programming 14
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
![Page 15: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/15.jpg)
9/6/05 CS360 Windows Programming 15
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
A B B B C
Stack pointer
![Page 16: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/16.jpg)
9/6/05 CS360 Windows Programming 16
Stack and Heap
Variables created on the heap are scattered throughout the heap
Heap contains holes
Each variable has a header listingo Size of variable
o Address of nextvariable
stack
heap
![Page 17: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/17.jpg)
9/6/05 CS360 Windows Programming 17
ExampleStatic void Main(){ Ref r1 = new Ref(); Val v1 = new Val();}
Class Ref{ public int x;}
struct Val{ public int x;}
Stack Heap
![Page 18: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/18.jpg)
9/6/05 CS360 Windows Programming 18
ExampleStatic void Main(){ Ref r1 = new Ref(); Val v1 = new Val();}
Class Ref{ public int x;}
struct Val{ public int x;}
Stack Heap
r1
x = 0v1
x = 0
![Page 19: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/19.jpg)
9/6/05 CS360 Windows Programming 19
ExampleStatic 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;}
Stack Heap
r1
x = 5v1
x = 5
![Page 20: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/20.jpg)
9/6/05 CS360 Windows Programming 20
ExampleStatic 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;}
Stack Heap
r1
x = 5v1
x = 5
r2
x = 5v2
![Page 21: 1 9/6/05CS360 Windows Programming CS360 Windows Programming](https://reader035.vdocuments.net/reader035/viewer/2022062423/5697bfbf1a28abf838ca3670/html5/thumbnails/21.jpg)
9/6/05 CS360 Windows Programming 21
Summary
Completed p. 27 - 38
Next time we will complete chapter 2