ObjectOriented Programming - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

ObjectOriented Programming

Description:

TopUp = true; Methods (functions) Attributes (data) Instantiating Classes ... private bool TopUp; public void Drive() Console.WriteLine('Roadin' and Rockin' ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 28
Provided by: jonpr
Category:

less

Transcript and Presenter's Notes

Title: ObjectOriented Programming


1
Object-Oriented Programming
2
Agenda
  • Classes Objects
  • Attributes
  • Methods
  • Objects in memory
  • Visibility
  • Properties (get set)
  • Constructors

3
Classes
  • Encapsulate data and functions
  • Enable more complex programming
  • Large-scale programming
  • Requires Object think
  • Security/reliability
  • Design by contract

4
Defining a Class
  • class NAME
  • ATTRIBUTES (data)
  • METHODS (functions)

5
Class Example
class BMW_Z4 int ModelYear string
LicensePlate bool TopUp void Drive()
Console.WriteLine(Roadin and Rockin)
void OpenTop() TopUp false
6
Class Example
class BMW_Z4 int ModelYear string
LicensePlate bool TopUp void Drive()
Console.WriteLine(Roadin and Rockin)
void OpenTop() TopUp true
Attributes (data)
Methods(functions)
7
Instantiating Classes
  • Defining the class is akin to defining a type
  • An object is an instance of a class
  • ClassObjectTypeVariable
  • Use new() to instantiate the class

8
Instantiating Class Using Object
  • BMW_Z4 myCar
  • myCar new BMW_Z4()
  • myCar.LicensePlate "BMR4ME"
  • myCar.ModelYear 2004
  • myCar.Drive()
  • myCar.OpenTop()
  • myCar.Drive()

9
Memory Trace
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2

null
10
Memory Trace(declare the variables they are
dead)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2

null
d1
d2
11
Memory Trace(bring d1 to life)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2
  • // Remember, new opens
  • // up space and calls the
  • // class constructor

null
d1
d2
12
Memory Trace(bring d1 to life)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2
  • // Remember, new opens
  • // up space and calls the
  • // class constructor

null
d1
d2
13
Memory Trace(bring d1 to life)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2
  • // Remember, new opens
  • // up space and calls the
  • // class constructor

null
d1
d2
rabidfalse
weight5
namebob
14
Memory Trace(bring d2 to life)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2
  • // Remember, new opens
  • // up space and calls the
  • // class constructor

null
d1
d2
rabidfalse
weight5
namebob
15
Memory Trace(bring d2 to life)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2
  • // Remember, new opens
  • // up space and calls the
  • // class constructor

null
d1
d2
rabidfalse
rabidfalse
weight5
weight5
namebob
namebob
16
Memory Trace(comparison)
Memory
  • d1 and d2 do NOT occupy
  • the same space in memory,
  • so they are NOT
  • Example
  • d1 d2
  • evaluates to false

null
d1
d2
rabidfalse
rabidfalse
weight5
weight5
namebob
namebob
17
Memory Trace(have d1 point to d2)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2

null
d1
d2
rabidfalse
rabidfalse
weight5
weight5
namebob
namebob
18
Memory Trace(have d1 point to d2)
Memory
  • Dog d1, d2
  • d1 new Dog (5, bob)
  • d2 new Dog (5, bob)
  • d1 d2

null
d1
d2
rabidfalse
rabidfalse
weight5
weight5
namebob
namebob
19
Memory Trace(have d1 point to d2)
Memory
  • Now, d1 and d2 are
  • But no one is pointing to this
  • Because no one is pointing to
  • it, we can no longer reference
  • it. We call this piece of memory
  • garbage. The garbage collector will deallocate it.

null
d1
d2
rabidfalse
rabidfalse
weight5
weight5
namebob
namebob
20
Visibility
  • OO motivation protection/security
  • We need a way of selectively publishing parts
    of a class and hiding other parts of the class
  • Public private

21
VisibilityExample
class BMW_Z4 private int ModelYear public
string LicensePlate private bool TopUp
public void Drive() Console.WriteLine("Roa
din and Rockin") public void OpenTop()
TopUp false
Note the visibility change(most attributes will
be private)
Method aretypically public
22
Object Method Attribute Visibility
  • BMW_Z4 myCar
  • myCar new BMW_Z4()
  • myCar.LicensePlate "BMR4ME"
  • myCar.ModelYear 2004
  • myCar.Drive()
  • myCar.OpenTop()
  • myCar.Drive()

Illegal b/c private
23
Properties
  • Combines field/attribute with method
  • Standard
  • Make attributes private
  • Lower-case first letter of attribute
  • Make properties public
  • Upper-case first letter of properties
  • Define get and set for each property
    (selectively)

24
PropertiesExample
class BMW_Z4 private int modelYear private
string licensePlate private bool topUp
public int ModelYear get return
modelYear set if (value gt 2003)
modelYear value public string
LicensePlate get return licensePlate
set if (value.Length() 6) licensePlate
value ...
25
Constructors
  • So far, weve seen attributes and methods
  • Constructor is a unique method
  • Named same as the class name
  • Automatically called when class is instantiated
  • Useful for setting attributes initializing the
    instance
  • No return type (not even void)
  • Must be public
  • Default constructor is used unless you specify a
    constructor
  • For each attribute in the class, assign something
    to it in the constructor (initialize)

26
ConstructorExample
class BMW_Z4 private int modelYear private
string licensePlate private bool topUp
public BMW_Z4 () ModelYear 2004
TopUp false LicensePlate "DEALER"
...
27
FIN
Write a Comment
User Comments (0)
About PowerShow.com