Content deleted Content added
→ColdFusion Markup Language (CFML): lang="cfc" |
|||
(14 intermediate revisions by 3 users not shown) | |||
Line 33:
<syntaxhighlight lang="cpp">
import std;▼
class Point {
private:
Line 57 ⟶ 55:
== Syntax ==
* [[Java (programming language)|Java]], [[C++]], [[C Sharp (programming language)|C#]], [[ActionScript]], {{nowrap|[[PHP]] 4}}, and [[MATLAB]] have a naming convention in which constructors have the same name as the class with which they are associated.
* In [[Rust (programming language)|Rust]], the convention for the "constructor" is to name it <code>new</code>.
* In PHP 5, a recommended name for a constructor is <code>__construct</code>. For backwards compatibility, a method with the same name as the class will be called if <code>__construct</code> method can not be found. Since PHP 5.3.3, this works only for non-namespaced classes.<ref name="php5cpnstructor">[http://www.php.net/manual/en/language.oop5.decon.php Constructors and Destructors], from PHP online documentation</ref>
* In PHP 7, you should always name the constructor as <code>__construct</code>. Methods with the same name as the class will trigger an E_DEPRECATED level error.<ref name="php5cpnstructor"/>
Line 67 ⟶ 66:
== Memory organization ==
In Java, C#, and VB .NET, the constructor creates reference type objects on the heap, whereas primitive types (such as <code>int</code>, <code>double</code>, etc.) are stored on the [[Stack-based memory allocation|stack]] (though some languages allow for manually allocating objects on the stack through a <code>stackalloc</code> modifier). VB .NET and C# also allow the use of the <code>new</code> operator to create value type objects, but these value type objects are created on the stack regardless of whether the operator is used or not. In these languages, object destruction occurs when the object has no references and then gets destroyed by the garbage collector.
In C++, objects are created on the stack when the constructor is invoked without the <code>new</code> operator, and created on the heap when the constructor is invoked with the <code>new</code> operator (which returns a pointer to the object). Stack objects are deleted implicitly when they go out of scope, while heap objects must be deleted implicitly by a destructor or explicitly by using the
== Language details ==<!-- see also Category:Programming language comparisons -->
Line 79 ⟶ 76:
In [[C++]], the name of the constructor is the name of the class. It returns nothing. It can have parameters like any [[Method (computer programming)|member function]]. Constructor functions are usually declared in the public section, but can also be declared in the protected and private sections, if the user wants to restrict access to them.
The constructor has two parts. First is the [[initializer list]] which follows the [[parameter (computer science)|parameter list]] and before the method body. It starts with a colon and entries are comma-separated. The initializer list is not required, but offers the opportunity to provide values for data members and avoid separate assignment statements. The initializer list is required if you have ''const'' or reference type data members, or members that do not have parameterless constructor logic. Assignments occur according to the order in which data members are declared (even if the order in the initializer list is different).<ref>https://stackoverflow.com/questions/1242830/constructor-initialization-list-evaluation-order Constructor</ref> The second part is the body, which is a normal method body enclosed in curly brackets. It is generally cheaper and better practice to use the initializer list as much as possible, and only use the constructor body for non-assignment operations and assignments where the initializer list cannot be used or is otherwise insufficient.
C++ allows more than one constructor. The other constructors must have different parameters. Additionally constructors which contain parameters which are given default values, must adhere to the restriction that not all parameters are given a default value. This is a situation which only matters if there is a default constructor. The constructor of a [[base class]] (or base classes) can also be called by a derived class. Constructor functions are not inherited and their addresses cannot be referenced. When memory allocation is required, the ''new'' and ''delete'' operators are called implicitly.
Line 86 ⟶ 83:
<syntaxhighlight lang="cpp">
▲import std;
class PolarPoint {
private:
double x;
double y;
public:
PolarPoint(double r = 1.0, double theta = 0.0): // Constructor, parameters with default values.
x{r * std::cos(theta)}, y{r * std::sin(theta)} /* <- Initializer list */ {
std::println("
}
};
Line 99 ⟶ 98:
Example invocations:
<syntaxhighlight lang="cpp">
PolarPoint a
PolarPoint b(3) PolarPoint c(5, std::numbers::pi / 4); </syntaxhighlight>
Line 173 ⟶ 174:
'''Cheese.cfc'''
<syntaxhighlight lang="
component {
// properties
Line 187 ⟶ 188:
Create instance of a cheese.
<syntaxhighlight lang="
myCheese = new Cheese( 'Cheddar' );
</syntaxhighlight>
Line 193 ⟶ 194:
Since ColdFusion 10,<ref>[https://wikidocs.adobe.com/wiki/display/coldfusionen/cfcomponent CFComponent]</ref> CFML has also supported specifying the name of the constructor method:
<syntaxhighlight lang="
component initmethod="Cheese" {
// properties
Line 316 ⟶ 317:
<syntaxhighlight lang="java">
class
public
this(1); // Calling of constructor
System.out.println("
}
public
System.out.println("
}
}
public static void main(String[] args)▼
public class Example {
▲ public static void main(String[] args) {
X x = new X();
}
}
</syntaxhighlight>
Line 335 ⟶ 338:
<syntaxhighlight lang="java">
// Declaration of instance variable(s).
private int data;
// Definition of the constructor.
public
this(1);
}
// Overloading a constructor
public
data = input; // This is an assignment
}
}
</syntaxhighlight>▼
class Y extends X {
<syntaxhighlight lang="java">▼
private int data2;
public Y() {
super();
data2 = 1;
}
public Y(int input1, int input2) {
super(input1);
data2 = input2
}
}
public class Example {
public static void main(String[] args) {
Y y = new Y(42, 43);
}
}
</syntaxhighlight>
A constructor taking zero number of arguments is called a "no-arguments" or "no-arg" constructor.<ref>{{cite web|url=http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html|title= Providing Constructors for Your Classes |publisher=Oracle Corporation|date=2013|access-date=2013-12-20}}</ref>
=== JavaScript/TypeScript ===
As of ES6, [[JavaScript]] has direct constructors like many other programming languages. They are written as such
<syntaxhighlight lang="javascript">
class FooBar {
constructor(baz) {
this.baz = baz;
}
}
</syntaxhighlight>
Line 373 ⟶ 390:
<syntaxhighlight lang="javascript">
const foo = new FooBar('7');
</syntaxhighlight>
Line 380 ⟶ 397:
<syntaxhighlight lang="javascript">
function FooBar (baz) {
this.baz = baz;
}
</syntaxhighlight>
This is instantiated the same way as above.
The [[TypeScript]] equivalent of this would be:
class FooBar {
baz: string;
constructor(baz: string) {
this.baz = baz;
}
}
const foo: FooBar = new FooBar('7');
▲</syntaxhighlight>
=== Object Pascal ===
Line 526 ⟶ 556:
In the typical case, only the <code>__init__</code> method need be defined. (The most common exception is for immutable objects.)
<syntaxhighlight lang="
>>> exampleInstance = ExampleClass(42)▼
if __name__ == "__main__":
</syntaxhighlight>▼
This prints:
<pre>
Creating new instance...
Initialising instance...
▲>>> print(exampleInstance.payload)
42
</pre>
▲</syntaxhighlight>
Classes normally act as [[Factory (object-oriented programming)|factories]] for new instances of themselves, that is, a class is a callable object (like a function), with the call being the constructor, and calling the class returns an instance of that class. However the <code>__new__</code> method is permitted to return something other than an instance of the class for specialised purposes. In that case, the <code>__init__</code> is not invoked.<ref name="auto"/>
Line 630 ⟶ 667:
println!("Point is at ({}, {})", p.x, p.y);
}
</syntaxhighlight>
=== Visual Basic .NET ===
|