4. object-oriented programming procedural programming structs and objects object-oriented...

28
4. Object-Oriented Programming • Procedural programming • Structs and objects • Object-oriented programming • Concepts and terminology • Related keywords

Upload: bryan-lowry

Post on 14-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

4. Object-Oriented Programming

• Procedural programming • Structs and objects • Object-oriented programming• Concepts and terminology• Related keywords

Procedural Programming

The material described so far relates to procedural programming.

Procedural programming is characterized by structural design, a divide-and-conquer approach to solving problems.

Problems are decomposed into smaller and smaller sub-problems, until they can be solved in code.

Procedural Programming

Top-down structured design

Procedural Programming

Structured design is not very good at modeling real-world entities.

Data is separate from functions, making data management complex.

It works well for small projects, but makes it hard to partition large projects with thousands of functions.

Procedural code itself can also be cumbersome.

Procedural Programming

For example, a procedural geometry library might include these functions:

DrawLine(int x1, int y1, int x2, int y2);TranslateLine(ref int x1, ref int y1, ref int x2, ref int y2, , int

xDistance, int yDistance);ScaleLine(ref int x1, ref int y1, ref int x2, ref int y2, double

scaleFactor);RotateLine(ref int x1, ref int y1, ref int x2, ref int y2, double

angle);

DrawPoint(int x1, int y1);TranslatePoint(ref int x1, ref int y1, int xDistance, int yDistance);ScalePoint(ref int x1, ref int y1, double scaleFactor);RotatePoint(ref int x1, ref int y1, double angle);

These involve long parameter lists and can easily become complicated.

Procedural Programming

This can be simplified by using a variable type called a record in some other languages, or a struct in C, C++ and C#.

These are programmer-defined types that bundle together other types, for example:

struct Line{

int x1;int y1;int x2;int y2;

}struct Point{

int x1;int y1;

}

Structs are very useful for representing groups of heterogeneous variables, such as database records, dates, geometric entities, etc.

Procedural Programming

Using record types, our function library example can be streamlined somewhat, but the basic issues remain:

DrawLine(Line myLine);

TranslateLine(ref Line myLine, int xDistance, int yDistance);

ScaleLine(ref Line myLine, double scaleFactor);

RotateLine(ref Line myLine, double angle);

DrawPoint(Point myPoint);

TranslatePoint(Point myPoint, int xDistance, int yDistance);

ScalePoint(Point myPoint, double scaleFactor);

RotatePoint(Point myPoint, double angle);

Procedural Programming

Data must be passed between many functions, resulting in long parameter lists and complicated storage techniques.

Because function calls are atomic, it is impossible to enforce a required sequence of calls. This is especially the case with initialization and allocation/de-allocation of memory, the source of many programming errors.

It is difficult to take advantage of similarities between entities. In our geometry example, each type requires its own set of functions to manage them.

There is no way of enforcing consistency among entities. For example, if we want to ensure that every geometric entity in our library could be rotated, we have to do this manually.

Object-Oriented Programming

Object-oriented programming (OOP) solves many of the problems of procedural programming, and involves a fundamentally different approach.

It approaches problems in terms of things, or objects, and determine which behaviors are associated with these objects.

Instead of treating functions and data as separate entities, data and functions are merged together into objects.

Object-oriented programming was driven by the complexity of managing large software projects. By gathering data and functions into distinct entities, large projects are naturally partitioned into smaller, more manageable components.

Object-Oriented Programming

Objects and messages in object-oriented design.

Object-Oriented Programming

At its simplest, an object is a struct type that includes one or more functions:

class Line{

int x1;int y1;int x2;int y2;public void Draw();

}

Object-Oriented Programming

This can make code much simpler…

Procedural code:

x1 = 100; y1 = 100; x2 = 200; y2 = 200;TranslateLine(x1, y1, x2, y2, 10, 10);RotateLine(x1, y1, x2, y2, 30.0);DrawLine(x1, y1, x2, y2);

Object-oriented code:

Line myLine = new Line(100, 100, 200, 200);myLine.Translate(10, 10);myLine.Rotate(30.0);myLine.Draw();

Object-Oriented Programming

Object Oriented Programming (OOP) is essentially different from procedural programming.

Problems are modeled using objects that represent real-world or conceptual things, which tends to be more intuitive and expressive than a procedural approach.

Object-oriented design involves modeling hierarchies of real-world entities.

Objects have a state, capabilities, and responsibilities. Relationships between objects are an important part of an object-oriented model.

OOP requires considering not only the problem being modeled, but the users of class you are designing. Design is an integral part of any kind of object-oriented programming.

Overview of OOP: Wikipedia article

Object-Oriented Terminology Class – an object type. In our example, Line is a class.

Object – A specific instance of a Class. For example, myLine is an object in the example above. Creating an object from a class is called instantiation.

Association – Interaction between objects.

Composition – One class being made up of others. For example, a polygon might be composed of a set of lines.

Encapsulation – The functionality required to make an object work can be hidden from users of the object, which protects it from accidental changes.

Inheritance (specialization) – Objects can be designed hierarchically so that consistency is enforced.

Polymorphism – Different kinds of objects can be treated similarly.

Abstraction – The detailed Information about an object can be contained at the level that best represents it.

The last four items are especially important concepts.

Object-Oriented Programming

Encapsulation

Object-Oriented Programming

Encapsulation

Each class has a public interface and a private implementation. The interface specifies what the object can do; the implementation specifies how it is done.

Separating these two makes it possible to change how an object works after it is in use. It also means users of the object need not know the details of how it works.

Access Modifiers provide a way of controlling what part of a class is exposed to external code:class Line{

private int x1;private int x2;…

}Public – available to all codePrivate – only available to the current classProtected – available to this class and its children

Object-Oriented Programming

Inheritance (specialization) - Classes can inherit elements of other classes.

Object-Oriented Programming

Object-Oriented Programming

Geometry is the Base class and Line a Derived class, Other synonyms are Parent and Child, Ancestor and Descendent, Class and Subclass.

Line IS-A kind of geometry. Line HAS (is composed of) coordinates.

Descendent classes normally add some new behavior or implement some behavior in a way that is specific to the class.

In our example, Draw is an abstract method, that is, it is not implemented by the Geometry base class, but must be implemented by both Line and Point.

In C#, all objects implicitly descend from a class called Object, which has a few methods required of all objects, such as ToString().

Object-Oriented Programming

Polymorphism is the ability of different classes to be treated uniformly. In this example, we don’t know the specific type; all we need to know is that it IS-A Geometry object:

public void DrawMap(){

for (int i=0; i<MyGeometryCollection.Count; i++){

MyGeometryCollection[i].Draw();}

}

This makes it possible to add new kinds of geometry without changing the code that manages other geometry objects.

Object-Oriented Programming

Abstraction

Object-Oriented Programming

Abstraction

Information about an object can be stored at the appropriate level of abstraction.

This allows users of a class to treat it at the level of abstraction suitable to the problem they are solving.

Object-Oriented Programming

Variable scope in objects:

All constants, variables, and methods declared at the object level are visible within that object’s methods.

Private means visible only within that object.

Protected means visible within that object and its children

Public means visible to other objects.

Object-Oriented Programming

Instantiating an object:

Line myLine = new Line(100, 100, 200, 200);

When a new object is created, a constructor is called (a default constructor is part of the Object class).

In C#, a constructor is identified by the fact that it has the same name as its class, and no return type. Like other methods, constructors can be overloaded:

The purpose of a constructor is to ensure that all objects are in an initialized and usable state as soon as they are created.

Object-Oriented Programming

Sample constructor:

class Line{

private int x1;private int y1;private int x2;private int y2;

public Line(int X1, int Y1, int X2, int Y2){

x1 = X1;y1 = Y1;x2 = X2;y2 = Y2;

}}

Object-Oriented Programming

this is a special C# keyword that refers to the current instance of an object. For example:

public void Translate(int xOffset, int yOffset)

{this.x1 += xOffset;this.y1 += yOffset;this.x2 += xOffset;this.y2 += yOffset;

}

This can be implicit – you usually only need to use it to clarify to the reader what the code is doing, or when the current object is used as a parameter to a method.

Object-Oriented Programming

The static keyword indicates that the given entity is part of the Class, not the object. That means there is only one instance of it for the entire application.

For example, we could count the geometric items created by an application by adding a static variable to the Geometry class:

class Geometry{

…protected static int iGeometryCount = 0;

}

class Line : Geometry{

Line(int x1, int x2, int y1, int y2){

iGeometryCount ++; …

}}

Object-Oriented Programming

The null keyword indicates that an object reference is empty or uninitialized.

As programs become more complicated, it is often necessary to test to make sure an object exists before using it:

If (myLine != null)myLine.Draw();