Archive for the ‘Languages’ Category

Hello world

Thursday, April 28th, 2011

Python is one of those languages that are used by allot of different companies but probably does not get the credit that how much it is used. Google uses it, allot of the Ubuntu backend is python. Python is able to do object orientated programming, but does not include the full essence of objects like other languages like C++/Java/C# where you have a private/public/protected but use a “__” before the variable as a “hidden” variable within the class.

So going to do some posts of how cool Python can be, but to start with the classic “hello world” program, like most languages (C++/PHP/C#/Java) most of the functions/methods are actually english words that should mean something to the writer/reader of the program, so to display a message to the screen you can use the “print” command and here is the example program

print "hello world"

and this will output, if you save out that program as helloworld.py

hello world

to call the saved program via the command line you need to call the interpreter (the python program).

python helloworld.py

Javascript {} []

Saturday, March 26th, 2011

Someone asked me what is the difference between {} and [] within javascript.

Basically the difference is one is a key => value pair like PHP ‘{}’, and the other is just a pure array of values ‘[]‘.

Here is the code.

var simplearray = ["genux","linux"];
 
var keyvaluearray = {"name" : "genux", "OS" : "linux"};

To access the different values, since the simple array is index by numbers 0 onwards you use the [x] notation, and the key value array you can reference the value by the name of the key like in object orientated way.

alert(simplearray[0]);
 
alert(keyvaluearray.name);

Function vs Method

Tuesday, March 8th, 2011

The main difference between a function and a method is that a function can live independently of the any instance of a class, where as a method sits within a class.

That is about it, e.g. a function is

int func(int value);
...
 
int main()
{
  cout << funct(3) << endl;
}

whereas a method has to live within a class

class myclass
{
    int myMethod(int it);
};
 
....
 
int main()
{
   myclass theclass;
   cout << theclass.myMethod(4) << endl;
}

Strings

Tuesday, March 8th, 2011

Just a small thing encase anyone else finds this interesting as such.. but you cannot add a set of ” ” strings together in c++ without declaring one being a string.. for example

cout << "hi" + "bye" << endl;

the error would be

error: invalid operands of types ‘const char [3]’ and ‘const char [4]’ to binary ‘operator+

because c++ treats them both as char[] variables and not as a string, so you need to wrap one up into a string for it to work

cout << string("hi") + "bye" << endl;

Why -> and not *name.type

Tuesday, March 8th, 2011

When you are coding with pointers in c++ and you want to access the function/variable from that pointer deferenced, how come you cannot use something like below.

struct typeT{
   int value1;
};
 
typeT* tt = new typeT;
*tt.value1;

it is because the access element of the variable tt is higher in the compiler and thus it tries to equate

tt.value1

first, which is not a good thing because the tt has not been de-referenced and thus it is just a memory address pointer to the actual object. so you need to do

(*tt).value1

because like in maths the () will be equated first and then the access element part “.” and thus to make it easier

tt->value1

will change to

(*tt).value1

within the compiler.

Moonlight

Tuesday, March 8th, 2011

Within kubuntu (or ubuntu derived setups) you can have a moonlight development .. aka the silverlight open source version that will run on linux.

if you install the

monodevelop-moonlight

and also the

monodoc-moonlight-manual

packages, the last one is the manual if you need some help, but the first one will allow the monodevelop development environment have the moonlight plugin which creates a project to develop silverlight applications in.

Curly brackets

Tuesday, March 8th, 2011

It all depends on your type of coding, but I for style I do prefer to put array data within a separate part of the echo statement, for example.

$arr = array("first" => 1, "second" =>2);
 
echo "this is the first value " . $arr["first"];

but of course you can put the arr object within the echo statement as long as you { .. } it out, which is kinder similar to using the ” . as such,

echo "this is the second value {$arr["second"]}";

both work, but because of the array object uses the “..” for the hash key, then you will need to use the { .. }