Our new Indie Games subforum is now open for business in G&T. Go and check it out, you might land a code for a free game. If you're developing an indie game and want to post about it, follow these directions. If you don't, he'll break your legs! Hahaha! Seriously though.
Our rules have been updated and given their own forum. Go and look at them! They are nice, and there may be new ones that you didn't know about! Hooray for rules! Hooray for The System! Hooray for Conforming!

Giant Programming Clusterfuck++

NightslyrNightslyr Registered User regular
Shamelessly stolen from Janin's awesome OPs (all examples below written in Python):

Turning progamers into programmers hell yeah

I wrote up this wall of text while playing EVE. I hope that this thread will become a place where people new to programming can learn what languages are available, how to operate basic parts of programming, get help deciphering inscrutable homework assignments, etc. It might also serve as a useful repository for commonly-asked questions like "what is a pointer", "how do I write a function", etc.

Basic overview

In the code samples here, lines starting with the "#" character are comments. This means that they are not part of the program itself, but are instructions or notes to the reader.

Stuff that doesn't go anywhere specific

Always turn on compiler or interpreter warnings, and set them to maximum. If possible, make them errors rather than warnings. Compiler writers are very smart, and probably know more than you do. Read the documentation to your compiler or interpreter to determine how to set warning and error levels.

Variables

Variables are a way to "label" a certain value. They can be used to reduce code duplication. If you wanted to change the base value in the old code, you would have to update it in three places. In the new code, it only has to be updated in one.
# Old code
value1 = (5 * 10) + 5
value2 = (5 * 10) + 6
value3 = (5 * 10) + 7

# New code
base = 5 * 10
value1 = base + 5
value1 = base + 6
value1 = base + 7

To document code behavior. In the old code, the numbers "5", "10", and "100" are "magic numbers". Their purpose is unknown - may be it's how many cars have been sold, how many employees took the day off sick, or how many dollars were spent on candy bars. May be there's a comment hidden somewhere about its purpose, or may be not. In the new code, their purposes are inherently documented everywhere the variables are used.
# Old code
var = 100
income_today = var * 10 * 0.3

# New code
cent_multiplier = 100
net_dollar_income = 10
tax_rate = 0.3
income_today = net_dollar_income * cent_multiplier * tax_rate

In most modern languages, variables have "types". A type determines what values a variable can hold and what operations you can perform on it. For example, you can add two numbers, but cannot add a number and a string.

Common types of variables:
Boolean - May be either True or False, nothing else. In some primitive languages, such as C, the integers 0 and 1 are used instead of true and false.
Integer - A number with no fractional part.
Floating Point - A number that may contain fractional parts. Because computers only operate on Base 2 numbers, floating point math in most languages can give very "weird" values. For an example, enter "javascript:alert (0.1 + 0.2)" into your browser's URL bar.
String - An ordered list of characters. These might be letters like A, digits like 2, punctuation like !, or "unprintable" control characters such as a "new line" marker.
List - An ordered list of variables. These are usually accessed by a "0-based" index. For example, position 0 of the list (5 10 15 20) is 5, and position 3 is 20.

Conditionals

Conditionals are what separate a programming language from a mere list of equations. A conditional always has an if-block, and sometimes an else-block. In some languages, the two can be combined to save space. One thing to note here is the use of "==" to check for equality. Because "=" is used to assign to a variable, "==" checks if two values are equal.

An example of an if statement:
name = "Alice"
if name == "Alice":
        print "Hello Alice!"
else:
        if name == "Bob":
                print "Guten Tag Bob!"
        else:
                print "I don't know who you are."

# The same as above, with a combined 'elif' statement
name = "Alice"
if name == "Alice":
        print "Hello Alice!"
elif name == "Bob":
        print "Guten Tag Bob!"
else:
        print "I don't know who you are."

Loops

Loops are used to help reduce code duplication. If you have to do something many times, it's easier to put into a loop than try to write out every step. And if you want to change how many times something happens, it'll have to be in a loop.

The most basic form of the loop is the while loop. This loop will simply run over and over until some condition is met. If you make a mistake in the body, the loop might run forever and freeze your program.
x = 10
while x < 500:
        x = x * 10
print x

The other popular form of the loop is the for loop. A for loop is used for doing something once for every item in a group.
numbers = [5, 10, 15, 20, 25, 30]
# The number is: 5
# The number is: 10
# etc
for number in numbers:
        print "The number is: ", number


# The name is: Alice
# The name is: Bob
# etc
for name in ["Alice", "Bob", "Charlie", "David", "Eve"]:
        print "The name is: ", name

Nightslyr on
My PA, PSN, XBL, Origin, and Steam names are the same. 3DS Friend Code: 1607-1682-2948
steam_sig-400.png
Stack Exchange | http://www.mpdevblog.blogspot.com
«13456763

Posts

  • NightslyrNightslyr Registered User regular
    edited January 2009
    This page reserved for any other intro info that other people want.

    My PA, PSN, XBL, Origin, and Steam names are the same. 3DS Friend Code: 1607-1682-2948
    steam_sig-400.png
    Stack Exchange | http://www.mpdevblog.blogspot.com
  • ObsObs __BANNED USERS regular
    edited January 2009
    You should add threading.

    spacer.png
    spacer.png
    Obs.gif
  • NightslyrNightslyr Registered User regular
    edited January 2009
    Obs wrote: »
    You should add threading.

    Since I'm still more or less a beginner myself, I'll need someone to PM me some notes on that. Is threading even appropriate for a beginner programming thread, though?

    My PA, PSN, XBL, Origin, and Steam names are the same. 3DS Friend Code: 1607-1682-2948
    steam_sig-400.png
    Stack Exchange | http://www.mpdevblog.blogspot.com
  • iTunesIsEviliTunesIsEvil Registered User regular
    edited January 2009
    Meh, we get more than beginner questions in the thread sometimes. I can write some C# threading stuff for you, but if you want a language outside of that you'd need to talk to someone else. :)

  • bowenbowen Registered User regular
    edited January 2009
    Threading becomes a nightmare outside of anything but .NET, or other CRE languages, in my experience. I hate doing it in C++.

  • NightslyrNightslyr Registered User regular
    edited January 2009
    C# sounds like the best option, then. I remember trying to do/simulate threading in C about 8 years ago. I was thoroughly confused.

    My PA, PSN, XBL, Origin, and Steam names are the same. 3DS Friend Code: 1607-1682-2948
    steam_sig-400.png
    Stack Exchange | http://www.mpdevblog.blogspot.com
  • BlueSquareBlueSquare Registered User
    edited January 2009
    bowen wrote: »
    Threading becomes a nightmare outside of anything but .NET, or other CRE languages, in my experience. I hate doing it in C++.

    What is a CRE language?

  • NightslyrNightslyr Registered User regular
    edited January 2009
    BlueSquare wrote: »
    bowen wrote: »
    Threading becomes a nightmare outside of anything but .NET, or other CRE languages, in my experience. I hate doing it in C++.

    What is a CRE language?

    Maybe they mean CLR?

    My PA, PSN, XBL, Origin, and Steam names are the same. 3DS Friend Code: 1607-1682-2948
    steam_sig-400.png
    Stack Exchange | http://www.mpdevblog.blogspot.com
  • KrisKris Registered User regular
    edited January 2009
    I remember tackling threading in Java during one of my introductory computer science courses, while we were making this plane dogfighting game. It was horrible and couldn't get my threads running properly. A little write-up would be much appreciated, even if it is for C#. :D

    Steam: Zephyrall || XBL: Zephyrall || PS3: Zephyrall_KN || Battle.Net: Zephyrall#398
  • NightslyrNightslyr Registered User regular
    edited January 2009
    Well, someone's going to have to PM me about threads, because like I said before, I know jack about them.

    My PA, PSN, XBL, Origin, and Steam names are the same. 3DS Friend Code: 1607-1682-2948
    steam_sig-400.png
    Stack Exchange | http://www.mpdevblog.blogspot.com
  • PantsBPantsB Registered User regular
    edited January 2009
    bowen wrote: »
    Threading becomes a nightmare outside of anything but .NET, or other CRE languages, in my experience. I hate doing it in C++.

    Its implementation of threads is clearly more fundamental but its very doable on C or C++ if you're in the right C frame of thought. And 99.9% of the time a simple fork()s in C works just as well and will likely be faster since you don't have to go through the added overhead of the framework. C# is nice and all but it always feels really bulky whenever I use it and I can't really imagine using it for something so performance sensitive that threading must be used instead of simple forking.

    Of course, I get to use the FUBAR'd proprietary language developed in the 70s (or technically its 2nd generation descendant, the other half is its early 90s fourth generation descendant, also proprietary). Therefore I probably shouldn't criticizing perfectly reasonable languages like C#.
    Kris wrote: »
    I remember tackling threading in Java during one of my introductory computer science courses, while we were making this plane dogfighting game. It was horrible and couldn't get my threads running properly. A little write-up would be much appreciated, even if it is for C#. :D

    This is a problem with how we're teaching programming in the US. About the most complicated thing that should be done in an introductory course is Towers of Hanoi.

    11793-1.png
    Spoiler:
  • PantsBPantsB Registered User regular
    edited January 2009
    Nightslyr wrote: »
    Now, to tie all this into arrays. An array is actually treated, internally, as a pointer. You can dereference it, modify it, whatever. What's more, you can treat normal pointers like arrays! An example:
    int array[3] = {7, 8, 9}; /* Array of 3 integers */
    int a = 5, b = 10;
    int *a_ptr = &a;
    
    /* Dereferencing an array returns the first value of that array */
    printf ("array[0] = &#37;d, *array = %d\n", array[0], *array); /* Prints "array[0] = 7, *array = 7" */
    
    /* A pointer can be treated like an array */
    printf ("a_ptr[0] = %d\n", a_ptr[0]); /* Prints "a_ptr[0] = 5" */
    
    /* Because arrays are contiguous memory space, moving the pointer around will move around in the array */
    /* This is how the [] operator is implemented */
    printf ("array[1] = %d, *(array + 1) = %d\n", array[1], *(array + 1)); /* Prints "array[1] = 8, *(array + 1) = 8" */
    
    /* If your pointer is pointing to contiguous variables, you can do the same thing */
    printf ("b = %d, a_ptr[1] = %d, *(a_ptr + 1) = %d\n", b, a_ptr[1], *(a_ptr + 1)); /* Prints "b = 10, a_ptr[1] = 10, *(a_ptr + 1) = 10" */
    

    You should probably include something about allocation here. And multidimensional arrays. Oh and casting.
    Something like:
    int** stuffDoneHere(int x, int y)
    {
    int z=0;
    int ** multiArray=(int**)malloc(sizeof(int*)*x);
    for(z=0;z<y;z++)
    {
       multiArray[z]=(int*)malloc(sizeof(int)*y));
    }
    return multiArray;
    }
    
    But I allocate my memory idiosyncratically (I'd been using malloc for arrays for a year before someone pointed out I should be using calloc). And sorry if my rusty C code is f'ed up I'm not by a compiler to check.

    Oh and this would be a decent place to go into standard error output. And from there you get to files pretty easily.

    edit:

    Frustration rant - I miss C. That is a fucking elegant language. Its mysteries are deep and its truth powerful. This is the crap I get to code in
    Spoiler:

    11793-1.png
    Spoiler:
  • Mr.FragBaitMr.FragBait Registered User
    edited January 2009
    Here is an example of threading in python, which might be easier to understand than c# for beginners.
    The lack of syntax coloring makes it hard to read though. And the php tags made it look atrocious.
    Spoiler:

    bAm-BaM wrote:
    Ladies and gentlemen, I think we have found the answer to the eternal question, "cock goes where?"
    Cock goes in the mouth good sirs. Cock goes in the mouth.
  • AngelHedgieAngelHedgie Registered User regular
    edited January 2009
    Actually, Nightslyr, the three (yes, three) principles of OOP are encapsulation (what you called information hiding), inheritance (what you called behavior sharing), and most important, polymorphism.

    Polymorphism Made Simple

    So, what is polymorphism? Simply put, it's that objects remember what they are and behave accordingly, even when they're referenced through a parent class.

    For example, let's say we make a Mammal class, with a function Speak that doesn't return anything:
    public class Mammal
    {
       public Mammal()
       {
       }
    
       public string Speak()
       {
          return "";
       }
    }
    

    Now, let's make some child classes depicting other animals:
    public class Dog : Mammal
    {
       public Dog() : base()
       {
       }
    
       public override string Speak()
       {
          return "Woof\n";
       }
    }
    
    public class Cat : Mammal
    {
       public Cat() : base()
       {
       }
    
       public override string Speak()
       {
          return "Meow\n";
       }
    }
    
    public class Pig : Mammal
    {
       public Pig() : base()
       {
       }
    
       public override string Speak()
       {
          return "Oink\n";
       }
    }
    
    public class Human : Mammal
    {
    
       private string _name = "";
    
       public Human(string Name) : base()
       {
           _name = Name;
       }
    
       public override string Speak()
       {
          return "Hi, I'm " + _name + ".\n";
       }
    }
    

    Now, we'll create an ArrayList of Mammals, then ask them to speak.
    ArrayList<Mammal> MammalList = new ArrayList<Mammal>();
    MammalList.Add(new Dog());
    MammalList.Add(new Cat());
    MammalList.Add(new Pig());
    MammalList.Add(new Human("Bob"));
    
    string speech = "";
    
    foreach (Mammal m in MammalList)
    {
        speech += m.Speak();
    }
    
    label1.Text = speech
    

    The label will now say:
    Woof
    Meow
    Oink
    Hi, I'm Bob.

    As you're already pointing out, I called each of the elements in the example as Mammals, though, not as their specific child class. But because of polymorphism, eash of those Mammals remembered what they were, and called their specific version of the Speak function.

    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum
    Spoiler:
  • AngelHedgieAngelHedgie Registered User regular
    edited January 2009
    Loops Part Two: The Loopening

    There are two more types of loops used in modern programming languages that you'll see. The first is called a do-while loop, and is used when you want a while loop to execute at least once.
    int x = 1;
    int y = 1;
    
    do
    {
       x = x + 1
    }
    while (x <= 0)
    
    while (y <= 0)
    {
       y = y + 1;
    }
    

    In the above example, you will have x equal to 2, but y equal to 1. This is because the while statement is evaluated at the start of the while loop, but at the end of the do-while loop.

    The other form of loop is a relative newcomer, only seen in newer languages and addon libraries for older OO languages like C++. It's called a foreach loop, and is used to run a function across all elements of a collection (like an array).
    int[] intArray = [1, 2, 3, 4, 5];
    
    foreach (int i in intArray)
    {
       i = i + 3
    }
    

    This will add 3 to each element in the array, giving us [4, 5, 6, 7, 8].

    Doing this with a for loop is much more work:
    int[] intArray = [1, 2, 3, 4, 5];
    
    for (int i = 0, i < 4, i++)
    {
       intArray[i] = intArray[i] + 3;
    }
    

    Note that the syntax of the foreach loop is much simpler than that of the for loop.

    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum
    Spoiler:
  • EtheaEthea Registered User regular
    edited January 2009


    The other form of loop is a relative newcomer, only seen in newer languages and addon libraries for older OO languages like C++. It's called a foreach loop, and is used to run a function across all elements of a collection (like an array).

    Could also explain it uses an iterator, and what that is.

  • AngelHedgieAngelHedgie Registered User regular
    edited January 2009
    Ethea wrote: »


    The other form of loop is a relative newcomer, only seen in newer languages and addon libraries for older OO languages like C++. It's called a foreach loop, and is used to run a function across all elements of a collection (like an array).

    Could also explain it uses an iterator, and what that is.

    Actually, foreach doesn't use an iterator.

    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum
    Spoiler:
  • BlueSquareBlueSquare Registered User
    edited January 2009
    Ethea wrote: »


    The other form of loop is a relative newcomer, only seen in newer languages and addon libraries for older OO languages like C++. It's called a foreach loop, and is used to run a function across all elements of a collection (like an array).

    Could also explain it uses an iterator, and what that is.

    Actually, foreach doesn't use an iterator.


    Actually, it does in the CLR languages (really enumerator here). It only unrolls to a 'for loop' on intrinsic types.

  • NightslyrNightslyr Registered User regular
    edited January 2009
    Ack, so many replies/revisions! I'll get to work on all those tomorrow. Thanks for all of the input so far, everyone.

    My PA, PSN, XBL, Origin, and Steam names are the same. 3DS Friend Code: 1607-1682-2948
    steam_sig-400.png
    Stack Exchange | http://www.mpdevblog.blogspot.com
  • AngelHedgieAngelHedgie Registered User regular
    edited January 2009
    Iteration Vs. Recursion

    There are actually two ways to repeat code - iteration, where we loop over and over through a piece of code; and recursion, where we call a function inside of itself. Let's take a look at Nightslyr's Fibonacci function:
    int fib(int n)
    {
       if (n == 0)
          return 0;
       if (n == 1)
          return 1;
       return fib (n - 1) + fib (n - 2);
    }
    

    This is an example of a recursive function. If you look at the bottom, you can see that to get any value past the first 2, we call the function inside of itself. That said, we could write the same function iteratively:
    int fib2(int n)
    {
       if (n == 0)
          return 0
       if (n == 1)
          return 1 
       else
       {
          int v1 = 1;
          int v2 = 1;
          int temp = 0;
          n = n-2
          for (int i = 0; i < n; i++)
          {
             temp = v1 + v2;
             v1 = v2;
             v2 = temp;
          }
          return v1 + v2;
       }
    }
    

    That will do the exact same thing as the recursive example. However, it is a bit more complicated.


    Now, here's the kicker - which one would you use to calculate, say...the 5th Fibonacci number? How about the 25th? I would use the recursive function to get the first, but the iterative one to get the 25th. The reason is simple - while the iterative function is more complex, it always remains at the same level of complexity. In comparison, watch what happens when we start looking at the mechanism of the recursive function:

    fib(2) = fib(1) + fib(0) = 1 + 1 = 2
    fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + fib(0) = (1 + 1) + 1 = 3
    fib(4) = fib(3) + fib(2) = (fib(2) + fib(1)) + (fib(1) + fib(0)) = ((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0)) = ((1 + 1) + 1) + (1 + 1) = 5

    And so on. As you can see, the recursive function increases in complexity in a geometric fashion, whereas the iterative version increases arithmetically.

    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum
    Spoiler:
  • PantsBPantsB Registered User regular
    edited January 2009
    (I'll essentially be using psuedo code below. I think AH is using C# and I'm trying to remain consistent but its been 2 years since I used it)

    Polymorphism is probably the central concept behind OOP, and one of the more difficult to fully grasp. Its also one of the few areas where different languages require fundamentally different designs depending on their implementation of inheritance.

    Abstract Classes
    Consider: What if you were using the above Mammal system. No animal is simply a "Mammal" and there is no "Mammal" noise to make when it speaks. The system should thus be prohibited from creating an instance of "Mammal" as this does not reflect reality.

    While this may appear harmless in other examples it could lead to instability and security holes.

    This is achievable through an "abstract class."
    abstract private class Mammal
    {
      public virtual string Speak();
    
    }
    
    Not there is no Constructor, nor is there any code behind Speak(). The lack of code behind Speak is optional, but by using the virtual keyword, any non-abstract subclass must implement Speak.

    Time is running short (I want to bang out a few more things before 530 when I'm out of here), so forgive the pseudo-code here on out.

    Multiple inheritance:
    In some languages (C++, LISP, Python), a class can inherit from more than one class. Other languages (Java, C#, PHP) you can inherit obligations through more than one class if you use a specific type of abstract class called an "interface." The former is increasingly deprecated because (in part) of the diamond problem.

    The above example could have "Mammal" as an interface if we removed the name attribute (or made it some combination of final, constant and/or static depending on the language).
    Interface Vendor
    {
    public bool takeMoney();
    protected bool evaluateChoice();
    public bool receiveChoice();
    protected bool dispenseToy(Object x);
    }
    

    Any vendor - a coke machine, a candy machine, a cigarette machine, or a robot bartender would have to implement those features.
    Class RobotBarKeep extends Robot implements Vendor
    {
    /*etc*/
    }
    


    There's more if anyone wants to tackle Final classes and probably more important the static concept, fire away.

    Oh and Linked Lists. That should probably be covered in combination with recursion, right before Tower of Hanoi

    11793-1.png
    Spoiler:
  • ObsObs __BANNED USERS regular
    edited January 2009
    Intro to Cocoa Programming (Mac OS X)


    What you will need: a Mac and Xcode Developer Tools. You can buy a Mac from your local Apple store and Xcode should be available online as well, some say it also comes on the installation DVD with most Macs.

    Prerequisites: You should be familiar with object oriented concepts and programming in general, as these are universal things that may not be specific to Cocoa. If you already know C and at least one object oriented language such as C++ or Java, you will do well and learn fast as fuck.

    I'll update this section here regularly with new stuff over time, so check back every once and a while because I can tell already I'm probably not going to write all this stuff in one sitting. If you have any questions PM me or ask in the thread (but I don't really keep up with every page of this thread).

    So let's begin.



    Part 1: Objective C 2.0

    This language is what you will use to do your Cocoa programming. If I had to describe Objective-C, I would basically tell you it's C with powerful object oriented shit added to it. C is a complete subset of Objective-C, which means any Objective-C compiler will also compile pure C code; you can freely mix C, C++ and Objective-C code together in the same application without problems. This gives you a powerful advantage, because you have the ease of high level coding like with Java and C#, but you are not restricted to it all the time (so you could do some custom optimization).

    Because of this, a good Objective-C programmer should also be a good C programmer.

    In Objective-C there exist things called objects. Objects are basically structures that carry two things, variables and methods. An object is defined by a class. The class describes the format of the object, meaning it tells you what variables the object has and their types, and what methods the objects run and what they return. In Objective-C, a class is also used to allocate an object.

    When you want an object to run a certain method, you send it a message. There is a certain format to sending a message to an object, it looks like this:
    [objectRecievingMessage messageBeingSent];
    


    Notice this is dramatically different from some languages where you may write something like:

    object.Function();


    Well, it gets wierder. If you are sending messages with parameters, they start to look like this
    [objectRecievingMessage messageBeingSentWithParameterOne: 55.0  ParameterTwo: 958.0 ParameterThree: 46.0];
    

    It looks bizarre at first, the function being peppered with parameters all up inside it like that, but the end result is some very readable code. The format for the messages is defined of course when you write the classes.

    In Objective-C there is also a variable of type id. An id variable stands for object identifier. You can think of an id as a pointer to any type of object. So if you see a method that returns a type "id", it means it returns a pointer to an object. Because an id can be any object type, you don't really know shit about it other than the fact that it IS an object.



    A class is defined by two files: a header file (.h) and an implementation file (.m). Xcode generally creates both these files when you go to define a new class.

    Here is an example of a class definition, in it's own file called noobert.h
    #import <Foundation/Foundation.h> [I] ////This line adds all the necessary header files for Cocoa.
    [/I]
    
    @interface noobert : NSObject {[I] ////This line means you are defining an interface for a class named noobert, which inherits the instance and method variables of the class [b]NSObject[/b].[/I]
         
         int one; [I]///standard int[/I]
         float two; [I] ///standard float[/I]
    }
    
    -(float)multiplyNumbers; [I] //this defines a method that returns a float[/I]
    -(void)printGarbage;      [I]//this defines a method that returns nothing[/I]
    -(void)newNumbersForFirst:(int)num1 Second:(float)num2;[I]              //And this method returns nothing and takes two parameters, num1 and num2, of type int and float respectively.[/I]
    
    @end    [I]//and here we know that the class definition has come to an end. [/I]
    
    Spoiler:

    And here is the implementation of that class, in a separate file called noobert.m
    #import <Foundation/Foundation.h>
    #import "noobert.h"     [I]////Notice we have to import the .h file for the class to this .m file so it can know what the hell is going on. We use quotes because it's a file we created here in the project.[/I]
    
    @implementation noobert     [I] ////Now this line lets us know we are implementing methods for the noobert class.[/I]
    
    
    -(float)multiplyNumbers {
    
         return (one * two); [I]//returns the result of multiplying these two numbers, which will be a float. [/I]
    }
    
    
    -(void)printGarbage {
    
          printf("\n get the fuck outta here"); [I]//prints a line to the console, wherever that is. [/I]
    }    
    
    -(void)newNumbersForFirst:(int)num1 Second:(float)num2 {
    
          one = num1;
          two = num2;
    } 
    
    @end         [I]// implementation is over at this point[/I]
    
    Spoiler:



    So now suppose we have this class called noobert, and we wish to create an object of type noobert.

    We will need to create a variable, allocate memory for the noobert object, and then initialize it. It works like this (assuming all this code is a part of something larger):
    noobert *variableName;      [I]//The * means this is a pointer to an object of type noobert. We do not create statically defined objects. If you try you will get an error. [/I]
    
    variableName = [[noobert alloc] init];   
    
    [I]///In the line above you are telling the class to allocate memory for a new object of it's type. The alloc function returns an object of type "noobert". Then, for that object, you call it's init function which prepares the new object instance for use. If you think it's weird that something abstract like a class could receive a message like that in Objective-C, don't worry about it. I think every class in Objective-C actually creates it's own instance at the start of a program that serves a single purpose of creating new objects of it's own type, but I could be horribly wrong. Either way, doesn't really matter. [/I]
    
    
    //and now the new object can be used for stuff. 
    
    int small;
    [variableName printGarbage];
    float value = [variableName multiplyNumbers];
    small = (value * 2);
    [variableName newNumbersForFirst:small Second:[variableName multiplyNumbers]];
    
    //etc...
    
    






    Ok well I've talked about Objective-C this whole time and I actually haven't said anything about Cocoa yet so I'll have to continue later because I'm going to sleep.

    I don't really know how much more I'll write about Objective-C, I mean I could basically go on forever, and I've only scratched the syntactic surface here. It's a simple language to learn though if you have the right background, so next update I'll show you how to start building applications right away, complete with full UI and stuff, no command line BS. Peace.




    Part 2: Building a Cocoa Application

    coming soon

    update: in case anyone cares, I'll get to this eventually, sorry I'm just pretty loaded with work right now

    spacer.png
    spacer.png
    Obs.gif
  • noobertnoobert Registered User
    edited January 2009
    Obs wrote: »
    Intro to Cocoa Programming (Mac OS X)


    Reserved.

    waitin' on 'dis.

  • BlueSquareBlueSquare Registered User
    edited January 2009
    As relating to .NET languages, (syntax is C#):

    An abstract class can not be marked private. That would defeat the purpose of being inherited. It can be marked public or internal.

    If a method must be implemented by sub classes (edit: i had weird wording here), it is marked abstract, such as your Speak() method. It is not a virtual method.

    Abstract classes can certainly have a constructor and any other implementation that is seen in non-abstract classes.

    An Interface is not an abstract class at all. It is a special type that defines a contract that the object that implements the interface is bound to uphold. There is not much else to it than that. Interfaces also can not define the accessor on it's members. IOW, you can not tell the implementer that they must set function Foo to private.

    One important concept I have not yet seen discussed is the concept of immutability.

  • ASimPersonASimPerson And protect them from the evils of the world like trigonometry and prime numbers.Registered User regular
    edited January 2009
    As the user with the 11th-most posts in the old thread, let me state here:

    Ask some freakin' C questions :)

    redoctober2.png
    SE++ Forum Battle Archive | PST = Pacific Standard Time | DRUNKSTUCK: A Homestuck recap
  • ecco the dolphinecco the dolphin Registered User regular
    edited January 2009
    ASimPerson wrote: »
    As the user with the 11th-most posts in the old thread, let me state here:

    Ask some freakin' C questions :)

    I second this sentiment.

    Although I get the feeling we're getting a bit long in the tooth, old friend. Back in my days, I remember having to write assembly bitblt instructions by hand to work with SVGA graphics (640 x 480 x 16-bit colour!? WOW!!! I think my S3 video card had enough video memory to do 800 x 600 x 16-bit colour as well!). See, it was real mode on the x86 back then - I hadn't quite mastered protected mode, so I could only access 64k of video RAM at any time. Ahh, bank switching.

    Remember EMS and XMS? You wanted more than 640k but didn't want to go into protected mode? EMS and XMS were the only real ways (well, there was that fake real mode with 32-bit flat memory addresses that you could get into, but that required you to remove memory managers like himem.sys and emm386, so it didn't feel that good).

    Learning how to use DMA to pipe sound out to sound-blaster compatible cards.

    Man, those were the days.

    Now, you get callbacks and HALs and whatever else to do everything for you.

    Times have changed, I say. Perhaps for the better, but I still remember the shock I had when I finally played a wav file through my speakers (okay, I had calculated the sample rate wrong, so it played about 4 times as fast or something - but still!).

    Penny Arcade Developers at PADev.net.
  • ASimPersonASimPerson And protect them from the evils of the world like trigonometry and prime numbers.Registered User regular
    edited January 2009
    ASimPerson wrote: »
    As the user with the 11th-most posts in the old thread, let me state here:

    Ask some freakin' C questions :)

    I second this sentiment.

    Although I get the feeling we're getting a bit long in the tooth, old friend. Back in my days, I remember having to write assembly bitblt instructions by hand to work with SVGA graphics (640 x 480 x 16-bit colour!? WOW!!! I think my S3 video card had enough video memory to do 800 x 600 x 16-bit colour as well!). See, it was real mode on the x86 back then - I hadn't quite mastered protected mode, so I could only access 64k of video RAM at any time. Ahh, bank switching.

    Remember EMS and XMS? You wanted more than 640k but didn't want to go into protected mode? EMS and XMS were the only real ways (well, there was that fake real mode with 32-bit flat memory addresses that you could get into, but that required you to remove memory managers like himem.sys and emm386, so it didn't feel that good).

    Learning how to use DMA to pipe sound out to sound-blaster compatible cards.

    Man, those were the days.

    Now, you get callbacks and HALs and whatever else to do everything for you.

    Times have changed, I say. Perhaps for the better, but I still remember the shock I had when I finally played a wav file through my speakers (okay, I had calculated the sample rate wrong, so it played about 4 times as fast or something - but still!).

    Um, I kind of hate to say this, but while I do know everything you're talking about, I never had to deal with it.
    Spoiler:
    That said, I do program C every day and it's just my preferred language. While I'm sure OS and other system level code can be written in other languages, it's still mostly good ole C.

    redoctober2.png
    SE++ Forum Battle Archive | PST = Pacific Standard Time | DRUNKSTUCK: A Homestuck recap
  • Smug DucklingSmug Duckling Registered User regular
    edited January 2009

    ...

    And so on. As you can see, the recursive function increases in complexity in a geometric fashion, whereas the iterative version increases arithmetically.

    It doesn't have to. Recursion can do Fibonacci just fine. Just have to go about it slightly differently:
    int fib(int n) {
      return fib_helper(0, 1, n);
    }
    
    int fib_helper(int prev, int cur, int rem) {
      if(rem == 0) {
        return prev;
      } else {
        return fib_helper(cur, prev + cur, rem - 1);
      }
    }
    

    Runs in nice linear time. :)

    As an interesting exercise for anyone who's curious, try executing both my method and the previous recursive method of finding Fibonacci numbers. You will quickly see the difference in speed as you increase n.

    smugduckling,pc,days.png
  • Smug DucklingSmug Duckling Registered User regular
    edited January 2009
    Another, iterative, way to do it is called Dynamic Programming, which sounds more complex than it really is. At its core, it is about filling in a table of already calculated results so that you never need to calculate the same thing twice. A neat side-effect is that by the end of the calculation, you can have a table of results so that in the future you can recalculate the same problem by simply looking at the table.

    In the Fibonacci example, you might do it like this:
    int fib(int n) {
      int[] fibs = new int[n+1];
      fibs[0] = 0;
      fibs[1] = 1;
      for(int i = 2; i <= n; i++) {
        fibs[i] = fibs[i-1] + fibs[i-2];
      }
      return fibs[n];
      // If you wish you could return the whole array here for easy use later.
    }
    

    The iterative solution that was proposed earlier is kind of an optimization of this, designed to save memory. Since only the last two entries of the array of used, only those are kept in variables.

    smugduckling,pc,days.png
  • AiranAiran Registered User regular
    edited January 2009
    java.lang.NoClassDefFoundError

    I'm working in Eclipse. I've transferred my project files from my Vista laptop to my XP desktop. I fixed the reference file locations, etc etc.
    When I try to start main(), My laptop runs the program fine, my desktop does not. Does anyone have any ideas because this is seriously pissing me off. Google hasn't been much help, apart from telling me that main.class is missing, and to set the classpath to fix it. I'm not sure if I'm setting it correctly (Configure Build Path > Classpath tab > Advanced > Enter Classpath Variable > Configure > New).

    Seriously guys this is doing my head in :x

    [edit]NEVER MIND, I don't know what the hell happened, but the error fixed itself and after correcting a typo it found the prerequisite DLLs and shit, now it's working all fine and dandy, yay.

    paDudSig.jpg
  • PantsBPantsB Registered User regular
    edited January 2009
    BlueSquare wrote: »
    As relating to .NET languages, (syntax is C#):

    An abstract class can not be marked private. That would defeat the purpose of being inherited. It can be marked public or internal.

    If a method must be implemented by sub classes (edit: i had weird wording here), it is marked abstract, such as your Speak() method. It is not a virtual method.

    Abstract classes can certainly have a constructor and any other implementation that is seen in non-abstract classes.

    An Interface is not an abstract class at all. It is a special type that defines a contract that the object that implements the interface is bound to uphold. There is not much else to it than that. Interfaces also can not define the accessor on it's members. IOW, you can not tell the implementer that they must set function Foo to private.

    One important concept I have not yet seen discussed is the concept of immutability.
    I was trying to be more general conceptually then speaking directly to C# but a few points:

    1 - An abstract class can be private, both conceptually and in C#. I didn't intend to leave a private keyword on Mammal but that was incorrect for reasons other than inheritance.
    2 - Just screwed up on virtual keyword, rusty as a I said.
    3 - Abstract classes can have constructors, you're correct. Those constructors should not be directly accessible, however.
    4 - Interfaces are absolutely abstract classes. Abstract classes are by definition those classes that can not be directly created, only classes that inherit from them. Interfaces are a subset of abstract classes conceptually and only really exist to deal with multiple inheritance.

    11793-1.png
    Spoiler:
  • PantsBPantsB Registered User regular
    edited January 2009
    Airan wrote: »
    java.lang.NoClassDefFoundError

    I'm working in Eclipse. I've transferred my project files from my Vista laptop to my XP desktop. I fixed the reference file locations, etc etc.
    When I try to start main(), My laptop runs the program fine, my desktop does not. Does anyone have any ideas because this is seriously pissing me off. Google hasn't been much help, apart from telling me that main.class is missing, and to set the classpath to fix it. I'm not sure if I'm setting it correctly (Configure Build Path > Classpath tab > Advanced > Enter Classpath Variable > Configure > New).

    Seriously guys this is doing my head in :x

    Java classpath isn't set correctly. Always took me 20 minutes to figure out whenever I went back to Java at the start of a new semester

    11793-1.png
    Spoiler:
  • BlueSquareBlueSquare Registered User
    edited January 2009
    PantsB wrote: »
    BlueSquare wrote: »
    As relating to .NET languages, (syntax is C#):

    An abstract class can not be marked private. That would defeat the purpose of being inherited. It can be marked public or internal.

    If a method must be implemented by sub classes (edit: i had weird wording here), it is marked abstract, such as your Speak() method. It is not a virtual method.

    Abstract classes can certainly have a constructor and any other implementation that is seen in non-abstract classes.

    An Interface is not an abstract class at all. It is a special type that defines a contract that the object that implements the interface is bound to uphold. There is not much else to it than that. Interfaces also can not define the accessor on it's members. IOW, you can not tell the implementer that they must set function Foo to private.

    One important concept I have not yet seen discussed is the concept of immutability.
    I was trying to be more general conceptually then speaking directly to C# but a few points:

    1 - An abstract class can be private, both conceptually and in C#. I didn't intend to leave a private keyword on Mammal but that was incorrect for reasons other than inheritance.
    2 - Just screwed up on virtual keyword, rusty as a I said.
    3 - Abstract classes can have constructors, you're correct. Those constructors should not be directly accessible, however.
    4 - Interfaces are absolutely abstract classes. Abstract classes are by definition those classes that can not be directly created, only classes that inherit from them. Interfaces are a subset of abstract classes conceptually and only really exist to deal with multiple inheritance.

    1. It can be, only internal to another class. True. We should probably find a better example than the link you provided, which is almost a proof of concept (bad design) example of it. I can only think of a single design reason for using an abstract class in this manner.
    Either way, it *is* incorrect for inheritance because no types would have scope to it in that context. My point, although being wrong, was for you to correct your mistake so that others didn't repeat it.

    3. It needs to be decided if we're going to discuss what *can* be done and what *should* be done. Public constructors are fine on abstract classes. They aren't the best as far as design goes, but they are correct. It wouldn't make much sense for a language to support abstract classes yet allow them to be instantiated just because you made the constructor accessible.

    4. I completely disagree. Saying they are the same and then saying one is a subset of the other makes no sense to me. They function differently and provide different, yet closely related, purposes. Thinking they are the same is exactly why most people fail interview questions about the two.

  • AiranAiran Registered User regular
    edited January 2009
    PantsB wrote: »
    Airan wrote: »
    java.lang.NoClassDefFoundError

    I'm working in Eclipse. I've transferred my project files from my Vista laptop to my XP desktop. I fixed the reference file locations, etc etc.
    When I try to start main(), My laptop runs the program fine, my desktop does not. Does anyone have any ideas because this is seriously pissing me off. Google hasn't been much help, apart from telling me that main.class is missing, and to set the classpath to fix it. I'm not sure if I'm setting it correctly (Configure Build Path > Classpath tab > Advanced > Enter Classpath Variable > Configure > New).

    Seriously guys this is doing my head in :x

    Java classpath isn't set correctly. Always took me 20 minutes to figure out whenever I went back to Java at the start of a new semester

    Yeah see, I tried entering the 'jdk folder/lib' location into the Windows env variable section, I don't think Eclipse cares though. Spent an hour looking through Eclipse settings and saw nothing obvious. Somehow a reboot fixed the problem. It was a goddamn headache anyway.

    paDudSig.jpg
  • KrisKris Registered User regular
    edited January 2009
    BlueSquare wrote: »
    PantsB wrote: »
    BlueSquare wrote: »
    As relating to .NET languages, (syntax is C#):

    An abstract class can not be marked private. That would defeat the purpose of being inherited. It can be marked public or internal.

    If a method must be implemented by sub classes (edit: i had weird wording here), it is marked abstract, such as your Speak() method. It is not a virtual method.

    Abstract classes can certainly have a constructor and any other implementation that is seen in non-abstract classes.

    An Interface is not an abstract class at all. It is a special type that defines a contract that the object that implements the interface is bound to uphold. There is not much else to it than that. Interfaces also can not define the accessor on it's members. IOW, you can not tell the implementer that they must set function Foo to private.

    One important concept I have not yet seen discussed is the concept of immutability.
    I was trying to be more general conceptually then speaking directly to C# but a few points:

    1 - An abstract class can be private, both conceptually and in C#. I didn't intend to leave a private keyword on Mammal but that was incorrect for reasons other than inheritance.
    2 - Just screwed up on virtual keyword, rusty as a I said.
    3 - Abstract classes can have constructors, you're correct. Those constructors should not be directly accessible, however.
    4 - Interfaces are absolutely abstract classes. Abstract classes are by definition those classes that can not be directly created, only classes that inherit from them. Interfaces are a subset of abstract classes conceptually and only really exist to deal with multiple inheritance.

    1. It can be, only internal to another class. True. We should probably find a better example than the link you provided, which is almost a proof of concept (bad design) example of it. I can only think of a single design reason for using an abstract class in this manner.
    Either way, it *is* incorrect for inheritance because no types would have scope to it in that context. My point, although being wrong, was for you to correct your mistake so that others didn't repeat it.

    3. It needs to be decided if we're going to discuss what *can* be done and what *should* be done. Public constructors are fine on abstract classes. They aren't the best as far as design goes, but they are correct. It wouldn't make much sense for a language to support abstract classes yet allow them to be instantiated just because you made the constructor accessible.

    4. I completely disagree. Saying they are the same and then saying one is a subset of the other makes no sense to me. They function differently and provide different, yet closely related, purposes. Thinking they are the same is exactly why most people fail interview questions about the two.

    Can someone give a clear explanation of the difference between abstract classes and interfaces, and perhaps an example of when you would want to use one and not the other? I'm still shaky on this topic, even after my C# course last semester.

    Steam: Zephyrall || XBL: Zephyrall || PS3: Zephyrall_KN || Battle.Net: Zephyrall#398
  • PantsBPantsB Registered User regular
    edited January 2009
    BlueSquare wrote: »
    4. I completely disagree. Saying they are the same and then saying one is a subset of the other makes no sense to me. They function differently and provide different, yet closely related, purposes. Thinking they are the same is exactly why most people fail interview questions about the two.

    The definition of an abstract class is one that can not be instantiated.

    "Interfaces" only exist in languages that prohibit multiple inheritance. They exist explicitly because of the diamond problem. Intro courses don't teach it in these terms usually but the only reason they exist is to get some of the benefits of multiple inheritance while limiting the problems inherent by gimping some of the flexibility in the class (usually all virtual methods & only immutable/static attributes).

    If you can come up with an interface example that could not be conceptually replaced by an abstract class in a system with multiple inheritance fire away. In the same way that an abstract class is a subset with restrictions of classes in general, interfaces are a subset of abstract classes.

    11793-1.png
    Spoiler:
  • BlueSquareBlueSquare Registered User
    edited January 2009
    PantsB wrote: »
    BlueSquare wrote: »
    4. I completely disagree. Saying they are the same and then saying one is a subset of the other makes no sense to me. They function differently and provide different, yet closely related, purposes. Thinking they are the same is exactly why most people fail interview questions about the two.

    The definition of an abstract class is one that can not be instantiated.

    "Interfaces" only exist in languages that prohibit multiple inheritance. They exist explicitly because of the diamond problem. Intro courses don't teach it in these terms usually but the only reason they exist is to get some of the benefits of multiple inheritance while limiting the problems inherent by gimping some of the flexibility in the class (usually all virtual methods & only immutable/static attributes).

    If you can come up with an interface example that could not be conceptually replaced by an abstract class in a system with multiple inheritance fire away. In the same way that an abstract class is a subset with restrictions of classes in general, interfaces are a subset of abstract classes.


    Oh good lord, you win. I don't even care if someone believes your garbage anymore. Yes, there are many many debates about abstract classes versus interfaces even though they are the same thing. You are correct. I was wrong.

    I'll go back to my SAMS: Lurn OOP in 24 Ours book and my JCL programming classes.

  • AngelHedgieAngelHedgie Registered User regular
    edited January 2009
    Kris wrote: »
    Can someone give a clear explanation of the difference between abstract classes and interfaces, and perhaps an example of when you would want to use one and not the other? I'm still shaky on this topic, even after my C# course last semester.

    An abstract class is one where an object of said class cannot be created. Going back to the Mammal concept, we can define the characteristics of a Mammal (vertebrate, warm-blooded, gives birth to live young, etc.), but there's no such thing as a pure "Mammal". Instead, there are various kinds of Mammals, like Dogs and Cats and Humans and such.

    An interface is a subset of abstract classes that consist only of a list of virtual functions. When a class implements an interface, it guarantees that the functions listed in the interface definition will be implemented. For example, in order to call a foreach loop on an object in C#, the object must implement the IEnumerable interface, which details the specific commands needed for an enumerator.

    XBL: Nox Aeternum / PSN: NoxAeternum / NN:NoxAeternum
    Spoiler:
  • iTunesIsEviliTunesIsEvil Registered User regular
    edited January 2009
    Properties too AH. Methods and properties (dunno if you include properties when you say "functions").

    I dont have VS sitting in front of me: when inheriting from an abstract class (.NET), are you required to provide override implementations of all methods and properties of the abstract class?

  • His CorkinessHis Corkiness Registered User
    edited January 2009
    BlueSquare wrote: »
    PantsB wrote: »
    BlueSquare wrote: »
    4. I completely disagree. Saying they are the same and then saying one is a subset of the other makes no sense to me. They function differently and provide different, yet closely related, purposes. Thinking they are the same is exactly why most people fail interview questions about the two.

    The definition of an abstract class is one that can not be instantiated.

    "Interfaces" only exist in languages that prohibit multiple inheritance. They exist explicitly because of the diamond problem. Intro courses don't teach it in these terms usually but the only reason they exist is to get some of the benefits of multiple inheritance while limiting the problems inherent by gimping some of the flexibility in the class (usually all virtual methods & only immutable/static attributes).

    If you can come up with an interface example that could not be conceptually replaced by an abstract class in a system with multiple inheritance fire away. In the same way that an abstract class is a subset with restrictions of classes in general, interfaces are a subset of abstract classes.


    Oh good lord, you win. I don't even care if someone believes your garbage anymore. Yes, there are many many debates about abstract classes versus interfaces even though they are the same thing. You are correct. I was wrong.

    I'll go back to my SAMS: Lurn OOP in 24 Ours book and my JCL programming classes.

    "Is a subset of" != "Is exactly the same". Someone competent in OOP should recognise this.

«13456763
This discussion has been closed.