This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,29 @@
class MyClass
{
public:
void someMethod(); // member function = method
MyClass(); // constructor
private:
int variable; // member variable = instance variable
};
// implementation of constructor
MyClass::MyClass():
variable(0)
{
// here could be more code
}
// implementation of member function
void MyClass::someMethod()
{
variable = 1; // alternatively: this->variable = 1
}
// Create an instance as variable
MyClass instance;
// Create an instance on free store
MyClass* pInstance = new MyClass;
// Instances allocated with new must be explicitly destroyed when not needed any more:
delete pInstance;

View file

@ -0,0 +1,8 @@
class MyClass
{
public:
MyClass(): variable(0) {}
void someMethod() { variable = 1; }
private:
int variable;
};

View file

@ -0,0 +1,6 @@
class MyClass
{
public:
virtual void someMethod(); // this is polymorphic
virtual ~MyClass(); // destructor
};

View file

@ -0,0 +1,12 @@
(defclass circle ()
((radius :initarg :radius
:initform 1.0
:type number
:reader radius)))
(defmethod area ((shape circle))
(* pi (expt (radius shape) 2)))
> (defvar *c* (make-instance 'circle :radius 2))
> (area *c*)
12.566370614359172d0

43
Task/Classes/D/classes.d Normal file
View file

@ -0,0 +1,43 @@
import std.stdio;
class MyClass {
//constructor (not necessary if empty)
this() {}
void someMethod() {
variable = 1;
}
// getter method
@property int variable() const {
return variable_;
}
// setter method
@property int variable(int newVariable) {
return variable_ = newVariable;
}
private int variable_;
}
void main() {
// On default class instances are allocated on the heap
// The GC will manage their lifetime
auto obj = new MyClass();
// prints 'variable = 0', ints are initialized to 0 by default
writeln("variable = ", obj.variable);
// invoke the method
obj.someMethod();
// prints 'variable = 1'
writeln("variable = ", obj.variable);
// set the variable using setter method
obj.variable = 99;
// prints 'variable = 99'
writeln("variable = ", obj.variable);
}

View file

@ -0,0 +1,25 @@
type
TMyClass = class
private
FSomeField: Integer; // by convention, fields are usually private and exposed as properties
public
constructor Create;
begin
FSomeField := -1;
end;
procedure SomeMethod;
property SomeField: Integer read FSomeField write FSomeField;
end;
procedure TMyClass.SomeMethod;
begin
// do something
end;
var lMyClass: TMyClass;
lMyClass := new TMyClass; // can also use TMyClass.Create
lMyClass.SomeField := 99;
lMyClass.SomeMethod;

View file

@ -0,0 +1,44 @@
program SampleClass;
{$APPTYPE CONSOLE}
type
TMyClass = class
private
FSomeField: Integer; // by convention, fields are usually private and exposed as properties
public
constructor Create;
destructor Destroy; override;
procedure SomeMethod;
property SomeField: Integer read FSomeField write FSomeField;
end;
constructor TMyClass.Create;
begin
FSomeField := -1
end;
destructor TMyClass.Destroy;
begin
// free resources, etc
inherited Destroy;
end;
procedure TMyClass.SomeMethod;
begin
// do something
end;
var
lMyClass: TMyClass;
begin
lMyClass := TMyClass.Create;
try
lMyClass.SomeField := 99;
lMyClass.SomeMethod();
finally
lMyClass.Free;
end;
end.

View file

@ -0,0 +1,8 @@
def makeColor(name :String) {
def color {
to colorize(thing :String) {
return `$name $thing`
}
}
return color
}

View file

@ -0,0 +1,5 @@
? def red := makeColor("red")
# value: <color>
? red.colorize("apple")
# value: "red apple"