Week 3: Object-Oriented Programming

As opposed to procedural programming, objected oriented programming is centered on the creation of objects rather than simply functions.  Objects are defined using classes, and effectively serve as a blueprint for evoking instances of the object.  For example, a Bicycle class would contain all the information necessary to build a virtual 'bicycle', so that when we declare bicycle_1 = Bicycle(), we create a bicycle with the name bicycle_1.  Declaring bicycle_2 = Bicycle() creates a distinct second instance of bicycle, stored at a different memory address but made on the same blueprint.

Like actual objects, objects we define in object-oriented programming can have features or attributes. For example, the Bicycle class may have a location attribute.  We can access and change attributes; for example, if we wanted bicycle_1 to have location with x and y coordinates 2, we could type bicycle_1.x = 2 and bicycle_1.y = 2 . 

Methods are not unlike functions but are evoked with a particular instance, and are procedural in nature.  For example, if we wanted to move bicycle_1 forward n steps, we could create a 'forward' function for the Bicycle class that increases the y-coordinate attribute by n.  For example, assuming our Bicycle starts at the origin, bicycle_1.forward(10) would mean that the coordinates of the bicycle would then be (0,10).

Instances of an object can be returned as well, and objects can be given parameters that can be handled in its constructor.  Not only do objects help divide large tasks into manageable pieces, but they also emulate how the real world is structured. 

No comments:

Post a Comment