Tuesday 5 April 2011

OOP I did it again...

Right first off. This second year lab is mostly teaching you about Object Orientated Programming, or OOP (insert poor joke here). Objects and classes are a way of thinking about problems.

The first example that the lab script will try to introduce to you this concept is the threevector class.
The first thing we teach is called encapsulation.

Firstly lets look at the code that we are given.

class threevector
{
   private:
   double _x, _y, _z;

   ....
};



This part of the code defines the class name and the private variables. So. Translation, we have a "type", called threevector, and it stores three "variables" inside called _x, _y and _z*.
The private part means that only the threevector can see it. This seems like an inconvenience, but it is in fact a good thing. If only threevector can see _x, _y and _z, then only threevector would ever need to deal with it.^

Finally the double part means that these are "double precision float point" variables. That just means that its a rather precise number which is stored in a form like Standard Form (eg h = 6.62606896 x 10-34Js). This means its pretty good for storing things that can vary a lot.


*I prefer my private member variables to have names beginning with an underscore. It makes it easier later on.
^Consider your calculator. It probably has hundreds of buttons and functions. But, do you know how it does all that? Do you know what the buttons each do internally? Probably not. Also, if you got a calculator that needed you to go take it apart just to add two numbers, you'd think it was pretty dumb. This is what "Encapsulation" means.
Leave the complicated bits inside of the calculator (like transistors and diodes) and press the buttons on the outside.