Wednesday, 22 January 2014

Week 3 - Object Oriented programming

Hey everyone! Welcome to my SLOG! This is the page where I’ll be keeping track of my personal experiences in CSC148 and summarizing what I’ve learnt each week. I’ll be aiming to write a post about once a week (hopefully every weekend) describing what I've learnt about the wonderful world of Python programming.

So these last few weeks I've been learning about Object Oriented Programming!

What is Object Oriented Programming(OOP)?

This type of programming is basically just creating objects which contain data and operations which let you manipulate that data. It even has its own programming language! This language is mainly made up of features such as user-defined classes.

Luckily, you can turn practically any real life situation into a class. If you wanted to make a class for a shopping list for example, then you can do that! After stating your class like this:

class Shopping_list:

then you can write almost any class-specific method you want. This type of programming is a lot more intuitive and realistic than other types of programming (e.g. procedural programming) since it can be applied to more real-life situations and puts the objects and methods together in a package. This allows you to be more specific with your code and lets you apply different classes to different situations.

Example of Object Oriented Programming

One example of a class we made in our first few weeks of lecture was the class Point. This class basically allowed us to create a point with x and y coordinates. We would first have to initialize it, and give an object these properties. For example, to initialize point p we would have to assign it as a Point object:

p = Point(4, 5) 

with 4 being the x axis coordinates, and 5 being the y axis coordinate.
We are then able to run several functions on it, such as a distance calculation between two points, or between a point and the origin.

What can classes do?

Another example of a user-defined class would be class Dog, which we had to make as part of our first exercise. However, we had to allow objects of class Dog interact with objects of another class Toy (aka. Dogs should be able to play with Toys). So objects from different classes can be made to interact with each other.

One other thing we learnt this week was about Inheritance. This is when a user-defined class is able to get most of its features and methods from another ‘parent class’. This allows certain instances of the child class to function with the same methods as instances of the parent class. So objects are basically able to inherit certain functions! (Sort of like how we inherit genes from our parents) By importing a class, you avoid repetition and make code a lot more efficient and easier to fix! If you find a problem with your code, it’s a lot easier to have to change one parent file rather than having to go back and change the methods of two different classes.


Mini-Glossary

Class: This is basically a data type which contains class-specific functions.

Initializer method: (The __init__ method). This automatically creates an object of type class. As Professor Heap described it, if the class is a factory, it sets a new object to its “default factory settings”.

Instance: An object of type class. E.g. ‘Hi’ is an instance of string.

Method: A class-specific function

Inheritance: When objects are able to share some derived functions from another class.


I think that’s it for this week! 

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete