lecture 8: object oriented programming. what is a programming object? an object is an instance of a...

40
Lecture 8: Object Oriented Programming

Upload: alfred-rogers

Post on 19-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Lecture 8:Object Oriented Programming

Page 2: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

What is a Programming Object?

An object is an instance of a class. A class is a template for an object. Everything's an object!!! Statements such as these are not all that helpful when we are struggling to understand the basic concepts of object-oriented programming (OOP).

It is better to look as some examples of objects, such as a textbox, a push-button or some user-defined non-GUI object like a cardDeck. Each of these objects has properties that can be accessed and sometimes manipulated. Multiple instances can be defined for them, and there can be different characteristics for each instance.

hello there... Start

A programming object is a collection of data (variables or literal constants) and a set of functions that operate on the data. Programming objects give us the illusion of material objects and can be used to simplify the task of program design.

Page 3: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Life Cycle of an Object

Every object has a clearly defined life cycle. Apart from the normal state of "being in use," this life cycle includes two important stages:

Construction: When an object is first instantiated it needs to be initialized. This initializaztion is known as construction and is carried out by a constructor function.

Destruction: When an object is destroyed, there are often some clean-up tasks to perform, such as freeing memory. This is the job of a destructor function.

Note: The actions of the destructor function may not occur immediately, so we cannot rely on this function to release memory when we need it. Later we will look into the use of disposable objects to help manage critical resources.

Page 4: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Class Members

The methods (functions) and data in a class are called class members.

Data Members - Data members are instance members unless they are explicitly designated as static. Static data values are associated with the class as a whole. Each instance of a class has its own copy of an instance data value. Data members can be fields, constants, and events.

Function Members - Function members perform some operation on the data members of the class. Function members can be methods, properties, constructors, finalizers, operators, and indexers.

Page 5: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Methods

Are the standard form for functions and procedures. They may have zero or more arguments, they can be either instance or static, and they may or may not return a data value.

public static void SomeMethod(){ // stuff}

public static int SomeOther(){ return // some integer}

public int YetAnother(int val){ // so something with val return //some integer}

public void OneMore(ref int val){ // change the value of val}

Page 6: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Properties & Fields

These are sets of functions that can be accessed from the client through dot-notation. Properties can do the same type of things that methods can do, except they have their own syntax (e.g. get and set).

public string SomeProperty{ get { return // the value of the property } set { //set the property }}

public string SomeOtherProperty{ get { return // the value }}

Page 7: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Constructors

public class TheClassName{ public TheClassName { // stuff to do the construction } // other stuff}

public class AnExample{ public AnExample() { // default constructor stuff }

public AnExample(int num) { // constructor stuff that uses num }}

The constructor has the same name as the class and it initializes data or does whatever is needed to be done when a class instance is created.

CupOfCoffee myCup = new CupOfCoffee( );CupOfCoffee myCup = new CupOfCoffee("Maxwell House - Original",false,true);

CupOfCoffee(string coffeebrand, bool cream, bool sugar)

Page 8: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Finalizers (Destructors)

The finalizer has the same name as the class (preceded by a tilde, ~) and it releases data or does whatever is needed to be done when a class instance is disposed.

class SampleClass{ ~SampleClass() { // destructor stuff }}

"Destructors are used by the .NET Framework to clean up after objects. In general, you don't have to provide code for a destructor method; instead, the default operation works for you."

We cannot rely on the destructor to free up resources that are used by an object instance, as this may be a long time after the last time the object is used. Instead we can create Disposable Objects.

Page 9: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Disposable Objects

<ClassName> <VariableName> = new <ClassName> ();

. . .

Using (<VariableName>)

{

. . .

}

using (<ClassName> <VariableName> = new <ClassName>() )

or

In both cases below, the variable <VariableName> will be usable within the using code block and will be disposed of automatically at the end (i.e. Dispose( ) is called when the code block finishes).

Page 10: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Operators

Operators are infix functions usually defined by one or two symbols. Examples are =, +, >=, &&, and so on. In C# we can overload operators for user-defined data types.

public double x,y,z;public Vec_3(double x, double y, double z){ this.x = x; this.y = y; this.z = z;}

public static Vec_3 operator +(Vec_3 lhs, Vec_3 rhs) { Vec_3 result = new Vec_3(lhs);

result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; }

public Vec_3(Vec_3 rhs){ x = rhs.x; y = rhs.y; z = rhs.z;}

Page 11: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Indexers

Indexers are used like index values to gain access into an array, except that they can refer to an object. Instead of creating a name, you use the this keyword, which refers the the current object.

public somedatatype this[int index]{ get { // do something return some_value; } set { //set a value in the class related to index }}

Page 12: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Inheritance

There are two types of inheritance, implementation and interface. We will review implementation inheritance first, since it is the more common form of inheritance.

In implementation inheritance, a type derives from a base type, taking all the base type's member fields and functions.

class ExampleClass : object{ // stuff}

class ExampleClass{ // same stuff}

these are the same

Page 13: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Virtual Methods

Declaring a method as virtual allows it to be overidden by any class that is derived from the defining class. Virtual members cannot be private, as this would cause a paradox -- a member cannot be overridden by a derived class and yet be inaccessible from the derived class.

class SomeBaseClass{ public vitual void SomeVirtualMethod() { // method stuff }}

Page 14: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Overridding a Virtual Methodin a Derived Class

class BaseGraphicsClass{ public virtual void DrawLine() { } public virtual void DrawPoint() { }}

class NewDerivedClass : BaseGraphicsClass{ public override void DrawPoint() { }}

Instances of NewDerivedClass have access to the DrawLine() method as well as the new version of the DrawPoint() method.

Page 15: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 16: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Unified Modeling Language (UML)

Page 17: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 18: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

UML Representation of a Class

Page 19: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 20: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 21: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 22: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 23: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 24: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 25: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 26: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 27: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 28: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 29: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 30: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Virtual Members

Page 31: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's

Abstract Classes

Page 32: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 33: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 34: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 35: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 36: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 37: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 38: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 39: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's
Page 40: Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's