object-oriented programming. agenda classes & objects attributes methods objects in memory...
TRANSCRIPT
![Page 1: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/1.jpg)
Object-Oriented Programming
![Page 2: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/2.jpg)
Agenda• Classes & Objects• Attributes• Methods• Objects in memory• Visibility• Properties (get & set)• Constructors
![Page 3: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/3.jpg)
Classes
• Encapsulate data and functions
• Enable more complex programming– Large-scale programming
• Requires “Object think”– Security/reliability– Design by contract
![Page 4: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/4.jpg)
Defining a Class
class NAME {
ATTRIBUTES (data)
METHODS (functions)
}
![Page 5: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/5.jpg)
Class Exampleclass BMW_Z4 {
int ModelYear;
string LicensePlate;
bool TopUp;
void Drive()
{
Console.WriteLine(“Roadin’ and Rockin’“);
}
void OpenTop()
{
TopUp = false;
}
}
![Page 6: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/6.jpg)
Class Example
class BMW_Z4 {
int ModelYear;
string LicensePlate;
bool TopUp;
void Drive()
{
Console.WriteLine(“Roadin’ and Rockin’“);
}
void OpenTop()
{
TopUp = true;
}
}
Methods(functions)
Attributes (data)
![Page 7: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/7.jpg)
Instantiating Classes
• Defining the class is akin to defining a type
• An object is an instance of a class
• Class:Object::Type:Variable
• Use new() to instantiate the class
![Page 8: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/8.jpg)
Instantiating Class & Using ObjectBMW_Z4 myCar;myCar = new BMW_Z4();
myCar.LicensePlate = "BMR4ME";myCar.ModelYear = 2004;myCar.Drive();myCar.OpenTop();myCar.Drive();
![Page 9: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/9.jpg)
Memory TraceDog d1, d2;
d1 = new Dog (5, “bob”);
d2 = new Dog (5, “bob”);
d1 = d2;
null
![Page 10: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/10.jpg)
Memory Trace(declare the variables; they are dead)
Dog d1, d2;
d1 = new Dog (5, “bob”);
d2 = new Dog (5, “bob”);
d1 = d2;
null
d1 d2
![Page 11: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/11.jpg)
Memory Trace(bring d1 to life)
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
![Page 12: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/12.jpg)
Memory Trace(bring d1 to life)
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
![Page 13: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/13.jpg)
Memory Trace(bring d1 to life)
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
rabid=false
weight=5
name=“bob”
![Page 14: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/14.jpg)
Memory Trace(bring d2 to life)
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
rabid=false
weight=5
name=“bob”
![Page 15: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/15.jpg)
Memory Trace(bring d2 to life)
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
rabid=false
weight=5
name=“bob”
rabid=false
weight=5
name=“bob”
![Page 16: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/16.jpg)
Memory Trace(comparison)
d1 and d2 do NOT occupy
the same space in memory,
so they are NOT ==
Example:
d1 == d2
evaluates to false
null
d1 d2
rabid=false
weight=5
name=“bob”
rabid=false
weight=5
name=“bob”
![Page 17: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/17.jpg)
Memory Trace(have d1 point to d2)
Dog d1, d2;
d1 = new Dog (5, “bob”);
d2 = new Dog (5, “bob”);
d1 = d2;
null
d1 d2
rabid=false
weight=5
name=“bob”
rabid=false
weight=5
name=“bob”
![Page 18: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/18.jpg)
Memory Trace(have d1 point to d2)
Dog d1, d2;
d1 = new Dog (5, “bob”);
d2 = new Dog (5, “bob”);
d1 = d2;
null
d1 d2
rabid=false
weight=5
name=“bob”
rabid=false
weight=5
name=“bob”
![Page 19: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/19.jpg)
Memory Trace(have d1 point to d2)
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
rabid=false
weight=5
name=“bob”
rabid=false
weight=5
name=“bob”
![Page 20: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/20.jpg)
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
![Page 21: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/21.jpg)
class BMW_Z4 {
private int ModelYear;
public string LicensePlate;
private bool TopUp;
public void Drive()
{
Console.WriteLine("Roadin’ and Rockin’");
}
public void OpenTop()
{
TopUp = false;
}
}
VisibilityExample
Method aretypically public
Note the visibility change(most attributes will be private)
![Page 22: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/22.jpg)
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
![Page 23: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/23.jpg)
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)
![Page 24: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/24.jpg)
class BMW_Z4 {
private int modelYear;
private string licensePlate;
private bool topUp;
public int ModelYear
{
get { return modelYear; }
set { if (value >= 2003) modelYear = value; }
}
public string LicensePlate
{
get { return licensePlate; }
set { if (value.Length() == 6) licensePlate = value; }
}
...
}
PropertiesExample
![Page 25: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/25.jpg)
Constructors• So far, we’ve 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)
![Page 26: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/26.jpg)
class BMW_Z4 {
private int modelYear;
private string licensePlate;
private bool topUp;
public BMW_Z4 ()
{
ModelYear = 2004;
TopUp = false;
LicensePlate = "DEALER";
}
...
}
ConstructorExample
![Page 27: Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors](https://reader030.vdocuments.site/reader030/viewer/2022032701/56649c775503460f9492c3be/html5/thumbnails/27.jpg)
FIN