Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views

Object Oriented Programming: Module Road Map

This document provides an introduction to object-oriented programming concepts like classes, objects, methods, properties, constructors, and encapsulation. It uses the example of a CD player class to demonstrate how classes define objects, how objects are instantiated from classes, and how methods and properties allow interacting with objects to read/change their state. The key points are that classes define objects, objects are specific instances of classes, and methods and properties provide the interface to control objects.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Object Oriented Programming: Module Road Map

This document provides an introduction to object-oriented programming concepts like classes, objects, methods, properties, constructors, and encapsulation. It uses the example of a CD player class to demonstrate how classes define objects, how objects are instantiated from classes, and how methods and properties allow interacting with objects to read/change their state. The key points are that classes define objects, objects are specific instances of classes, and methods and properties provide the interface to control objects.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Object Oriented Programming

This lecture is an introduction to a range of concepts and terms related to object oriented programming. This will not be an exhaustive lesson in OO. There are certain aspects I want to highlight in order to complete the material in the module. One of the things you will have noticed in the lab session for this week is that a lot of words were thrown at you. In understanding OO concepts there is a new vocabulary that needs to be understood in order to come to terms with the subject.

Module Road Map

Within the module road map we are going to concern ourselves with the nature of custom classes in C sharp.

Credit Categories
This lecture will start to address the following credit categories. Assignment 1 Credit Category 3 Assignment 2 Credit Category 3 Assignment 3 Credit Category 2 & 3

Consider the Following


The first thing I want you to do is to take a look at the following and tell me what you are looking at:

These are the controls from Windows Media Centre allowing us to do the following Stop a track Rewind Skip previous Skip next Fast forward What I plan to do is use these controls to make a number of points related to object oriented programming. It is interesting to consider the history of these symbols and where they first originated. The technology in question dates back to the reel to reel tape recorders first appearing in the 1930s

This is a picture of a German Magnetophon machine. We can see how the technology works we have two reels. The first reel is loaded with magnetic tape and it transfers from one reel to the other as the reels spin. At the same time the tape passes over a play/recording head which translates the arrangement of the magnetic particles to sound. If we look at the controls on the machine we can see an early version of the controls we are interested in.

The recording / playback mechanism is very much dictating the controls on the interface. I have no idea who came up with the controls as we know then today or even when. However this vintage Polish recorder already has an early version of the symbols.

This common interface eventually became the de facto standard. (A standard that becomes popular because most people use/like it.) Over time what happened was that the same set of controls became used on a range of other products. Video Cassette Players

CD Players

MP3 Players

And curiously enough computer media players

(It is curious because the reel to reel mechanism is no longer working behind the scenes!)

And the Point is?


I appreciate that you didnt come on this module to be taught a history of tape recording devices. What I want to do here is use the above point to make a number of points about the nature of OO programming. By the end of this session you should have the beginnings of an understanding of some of the concepts we are interested in.

Classes versus Objects


Before we do anything else we need to make sure that we are clear on the difference between a class and an object. A class is a definition for an object

An object is a specific instance of a class

If we were going to buy a CD player we might look at Amazon and pick one there. We would look at its picture, examine the specifications and see the kind of thing that we are getting.

Now an advert in Amazon for a CD player is not a CD player. So we go ahead and buy the item and it is delivered. When it arrives we have an instance of the player as an object. The class is the definition (recipe) for an object The objects is the thing based on the recipe (the thing we use) When we create an object it is called instantiation (we create an instance of a class)

Example in C Sharp Code


Lets imagine that we have created a class in C # that controls a hardware interface to a CD player allowing us to control the device. I am not interested in the inner workings of the class simply the kinds of services the class might offer. Let us assume that the class code is stored within a file called clsCDPlayer.cs Notice how we use the three letter prefix to identify it as a class.

Remember a class is not an object, simply the definition of one. To do anything with this class we need to instantiate an object based on it. To do this in C sharp we would do the following: clsCDPlayer MyPlayer = new clsCDPlayer(); clsCDPlayer defines the name of the class file we are basing the object on MyPlayer is the name of the object we are creating = new clsCDPlayer(); ...makes a call to the constructor.

Constructors
The constructor is a section of code within the class that runs as soon as we create the object. It is rather like when we turn on our new CD player. It initialises the hardware, checks to see if there is already a disk in the tray and displays information on the liquid crystal display. In C sharp the constructor is a function with the same name as the class file...

Constructors may accept parameters allowing us to specify data at the creation of the object. For example... clsCDPlayer MyPlayer = new clsCDPlayer("track 11"); ...allows us to initialise the object and make it start at a specific track. Constructors may also be overloaded so that there are a range of initialisation options available.

Once we have instantiated an object based on the class we may now start doing things to it via its methods and properties.

Command and Control Methods and Properties


Once we have an object created we need to be able to tell it to do things and also read/change its state. Command allows us to tell the computer technology what it is we want to do Control allows us to say how it is we want the technology to do it and find out what it is doing at any point in time

To achieve this we use the objects methods and properties. Methods allow us to tell the object to perform a task Properties allow us to set or find out about the state of the object

For our CD player there are some very obvious methods and properties to consider. Methods Play Stop Pause FF Rewind Skip back Skip ahead Properties Track number Volume

Methods in C Sharp

A method in C Sharp is a defined as a function within the class. It may be void or return a value and may accept parameters. When the function is created in the class it is seen as a method by code using the class. For example we might have a Play method in the CD player class. The code within the class would look like this...

The code that creates an object based on the class might look like this...

We could modify the code such that it allows a parameter specifying a track.

In which case the code using the class might now look like this...

We could also change the return value of the function so that it returns a value of true or false based on if the track exists or not. The function within the class would look like this...

The code using the class might now look like this...

Properties in C Sharp
A property allows us to find out about or set something about the state of an object. One property that our class might have is Volume. The Volume property would be defined within the class like so...

There are quite a few things to notice here. Firstly the variable where the data is stored within the class is set to private. This is an example of data hiding. The idea is that we want any code using the class to only have access to the data we allow. To change the data it must use methods and properties that control (restrict) access. The private member variable is named using camel case.

For example volume firstName lastName Public Properties Named Using Pascal Notation Fore example Volume FirstName LastName Also notice the two parts of the property definition. The getter allows data to be retrieved from the member variable. The setter allows data to be sent to the member variable. The code using the class might look something like this...

We may find out the volume of the CD player by reading the value of the Volume property.

Encapsulation
Another term that you will come across in OO programming is encapsulation. It really isnt that difficult a concept to grasp. If we think back to the discussion on the controls on a tape deck we can see an example of encapsulation. For each successive device reel to reel, VCR, CD, DVD etc., the controls have remained the same. However the internal technology we are controlling has changed. This is even to the point where the technology on a computer based media player has nothing to do with the nature of the controls used. (On an MP3 player there is no tape!) What has happened here is that the external interface on the object has remained the same but the inner workings of the object has changed. Imagine with our CD player class we wanted to use a variation on the class to control a reel to reel tape machine. What would we need to do? We would start by re writing the code that controls the underlying hardware What we would not change are the methods and properties used by any code that makes use of the class

This allows our applications to be both modular and able to cope with change.

The Data Conduit

The data conduit class is a good example of encapsulation. The class provided on the module web site allows us to connect to an SQL server database using the SQL server specific ADO.NET (Active Data Objects) classes from the .NET class library. We are not going to open up the class in this lecture but we will take a first look at the interface provided for command and control. //create a new instance of the data conduit called Addresses clsDataConduit Addresses = new clsDataConduit(); //connect to the stored procedure Addresses.Execute("sproc_tblAddress_GetAll"); The first line of code creates an instance of the class called Addresses. The second executes a stored procedure called sproc_tblAddress_GetAll Let us suppose we wanted to take off the SQL server database and replace it with an OLEDB (Object Linking and Embedding, Database)? OLEDB is an alternate set of database drivers provided by Microsoft to connect to a wide range of other database management systems (e.g. Access, MySQL). To do this what we would need to do is change the code in the class but keep the interface the same. One issue worth thinking about at this point is how to address the problem created in our code by not encapsulating the name of the stored procedure. One approach would be to encapsulate stored procedure names in a Globals class. clsGlobals Globals = new clsGlobals(); clsDataConduit Addresses = new clsDataConduit (); Addresses.Execute(Globals.sproctblAddressGetAll);

Using the Debugger in Visual Studio


The last thing I want to cover is introduce the Visual Studio debugger and use it to show samples of code to illustrate the above. I will use my Address book case study. The first thing I want to do is access the code for the start up form. We shall place a break point in the code for the click event of the save button by pressing F9

The next step is to run the program. Next we enter some data and press save...

The code will stop and wait at the break point

In order to step over lines at this level of the application press F10 F11 allows you to step into the classes in the middle tier.

You might also like