CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
29
Task/Classes/C++/classes-1.cpp
Normal file
29
Task/Classes/C++/classes-1.cpp
Normal 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;
|
||||
8
Task/Classes/C++/classes-2.cpp
Normal file
8
Task/Classes/C++/classes-2.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class MyClass
|
||||
{
|
||||
public:
|
||||
MyClass(): variable(0) {}
|
||||
void someMethod() { variable = 1; }
|
||||
private:
|
||||
int variable;
|
||||
};
|
||||
6
Task/Classes/C++/classes-3.cpp
Normal file
6
Task/Classes/C++/classes-3.cpp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class MyClass
|
||||
{
|
||||
public:
|
||||
virtual void someMethod(); // this is polymorphic
|
||||
virtual ~MyClass(); // destructor
|
||||
};
|
||||
12
Task/Classes/Common-Lisp/classes.lisp
Normal file
12
Task/Classes/Common-Lisp/classes.lisp
Normal 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
43
Task/Classes/D/classes.d
Normal 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);
|
||||
}
|
||||
25
Task/Classes/DWScript/classes.dwscript
Normal file
25
Task/Classes/DWScript/classes.dwscript
Normal 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;
|
||||
44
Task/Classes/Delphi/classes.delphi
Normal file
44
Task/Classes/Delphi/classes.delphi
Normal 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.
|
||||
8
Task/Classes/E/classes-1.e
Normal file
8
Task/Classes/E/classes-1.e
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def makeColor(name :String) {
|
||||
def color {
|
||||
to colorize(thing :String) {
|
||||
return `$name $thing`
|
||||
}
|
||||
}
|
||||
return color
|
||||
}
|
||||
5
Task/Classes/E/classes-2.e
Normal file
5
Task/Classes/E/classes-2.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
? def red := makeColor("red")
|
||||
# value: <color>
|
||||
|
||||
? red.colorize("apple")
|
||||
# value: "red apple"
|
||||
Loading…
Add table
Add a link
Reference in a new issue