Java, C++, Ruby, Python, etc…

Read more..

Java

public class Point {
    public int x = 0;
    public int y = 0;
    //constructor
    public Point(int a, int b) {
	x = a;
	y = b;
    }

Point originOne = new Point(23, 94);

C++

#include <iostream>

class IntList {
  public:
    IntList();                         // constructor; initialize the list to be empty
    void AddToEnd(int k);              // add k to the end of the list
    void Print(ostream &output) const; // print the list to output

  private:
    static const int SIZE = 10;      // initial size of the array
    int *Items;                      // Items will point to the dynamically allocated array
    int numItems;                    // number of items currently in the list
    int arraySize;                   // the current size of the array
};

IntList L; 

OR

IntList *p;
p = new IntList;  // The no-arg constructor is called

Python

class MyClass:
    """A simple example class"""
    i = 12345
    def f(self):
        return 'hello world'

x = MyClass()

Perl

package Inventory_item;
    sub new {
        my($class) = shift;

        bless {
            "PART_NUM"    => undef,
            "QTY_ON_HAND" => undef
        }, $class;
    }

package main;
   $item = Inventory_item->new();

Ruby

class Person
  attr_accessor :name

  def initialize(name)
  	@first_name = name
  end

end

matz = Person.new

Javascript

person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"

person.run = function() {
	this.state = "running"
	this.speed = "4ms^-1"
}

Javascript

person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"

person.run = function() {
	this.state = "running"
	this.speed = "4ms^-1"
}

PHP 5

class ExtendClass extends SimpleClass
{
	function __construct() {
       parent::__construct();
       print "In SubClass constructor\n";
   }

    // Redefine the parent method
    function displayVar()
    {
        echo "Extending class\n";
        parent::displayVar();
    }
}

$extended = new ExtendClass();
$extended->displayVar();

PHP 4

class Cart {
    var $items;  // Items in our shopping cart

    // Add $num articles of $artnr to the cart

    function add_item($artnr, $num) {
        $this->items[$artnr] += $num;
    }

    // Take $num articles of $artnr out of the cart

    function remove_item($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } elseif ($this->items[$artnr] == $num) {
            unset($this->items[$artnr]);
            return true;
        } else {
            return false;
        }
    }
}

C#

public class Person
{
    // Field
    public string name;

    // Constructor
    public Person()
    {
        name = "unknown";
    }

    // Method
    public void SetName(string newName)
    {
        name = newName;
    }
}
class TestPerson
{
    static void Main()
    {
        Person person1 = new Person();
        System.Console.WriteLine(person1.name);

        person1.SetName("John Smith");
        System.Console.WriteLine(person1.name);
    }
}

VB.NET

Public Class TheClass
  Public Sub DoSomething()
    MsgBox("Hello world", MsgBoxStyle.Information, "TheClass")
  End Sub
End Class

Smalltalk

Object subclass: #MessagePublisher
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Smalltalk Examples'

MessagePublisher new

OR

publisher := MessagePublisher new

OR

MessagePublisher new publish

Pike

class animal
{
  string name;
  float weight;

  void create(string n, float w)
  {
    name = n;
    weight = w;
  }

  void eat(string food)
  {
    write(name + " eats some " + food + ".\n");
    weight += 0.5;
  }
}

animal some_animal;
some_animal = animal();
animal my_dog = animal();

Objective-C

Stack.h

#import < objc/Object.h>

@interface Stack : Object
{
  StackLink *top;
  unsigned int size;
}

- free;
- push: (int) anInt;
- (int) pop;
- (unsigned int) size;

@end

Stack.m

#import "Stack.h"

@implementation Stack

#define NULL_LINK (StackLink *) 0

+ new
{
  self = [super new];
  top = (StackLink *) 0;
  return self;
}

- free
{
  StackLink *next;

  while (top != NULL_LINK)
    {
      next = top->next;
      free ((char *) top);
      top = next;
    }
  return [super free];
}

- push: (int) value
{
  StackLink *newLink;

  newLink = (StackLink *) malloc (sizeof (StackLink));
  if (newLink == 0)
    {
      fprintf(stderr, "Out of memory\n");
      return nil;
    }
  newLink->data = value;
  newLink->next = top;
  top = newLink;
  size++;

  return self;
}

- (int) pop
{
  int value;
  StackLink *topLink;

  if (0 != size)
    {
      topLink = top;
      top = top->next;
      value = topLink->data;
      free (topLink);
      size--;
    }
  else
    {
      value = 0;
    }
  return value;
}

- (unsigned int) size
{
  return size;
}

@end

id s;
s = [Stack new];

example is from here