Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
Jc1500 (talk | contribs)
m Python: added a missing ":"
Coding standards
Line 26:
public:
Example();
Example(int a, int b); //parameterized Parameterized constructor
};
Example :: Example()
Line 39:
When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the ''shorthand'' method.
<source lang="c">
Example e = Example(0, 50); //explicit Explicit call
 
Example e(0, 50); //implicit Implicit call
</source>
 
Line 82:
In C++, when constructor is invoked without new the objects are created on stack. When objects are created using new they are created on heap. They must be deleted implicitly by a destructor or explicitly by a call to operator ''delete''.
 
== Language details ==
 
=== Java ===
Line 104:
public class Example
{
//definition Definition of the constructor.
public Example()
{
this(1);
}
 
//overloading Overloading a constructor
public Example(int input)
{
data = input; // This is an assignment
}
 
//declaration Declaration of instance variable(s).
private int data;
}
</source>
 
<source lang="java">
//code Code somewhere else
//instantiating Instantiating an object with the above constructor
Example e = new Example(42);
 
</source>
 
Line 158 ⟶ 157:
public class MyClass
{
private int a;
private string b;
 
//constructor Constructor
public MyClass() : this(42, "string")
{
}
 
//overloading Overloading a constructor
public MyClass(int a, string b)
{
this.a = a;
this.b = b;
}
}
</source>
 
<source lang="csharp">
//code Code somewhere
//instantiating Instantiating an object with the constructor above
MyClass c = new MyClass(42, "string");
</source>
Line 192 ⟶ 191:
public class MyClass
{
private static int _A;
 
//normal Normal constructor
static MyClass()
{
_A = 32;
}
 
//standard Standard default constructor
public MyClass()
{
 
}
}
</source>
 
<source lang="csharp">
//code Code somewhere
//instantiating Instantiating an object with the constructor above
// right before the instantiation
//the The variable static constructor is executed and _A is 32
MyClass c = new MyClass();
</source>
Line 263 ⟶ 262:
<source lang="ocaml">
type MyClass(_a : int, _b : string) = class
// primaryPrimary constructor
let a = _a
let b = _b
do printfn "a = %i, b = %s" a b
 
// additionalAdditional constructors
new(_a : int) = MyClass(_a, "") then
printfn "Integer parameter given"
Line 281 ⟶ 280:
 
<source lang = "ocaml">
//code Code somewhere
// instantiating an object with the primary constructor
let c1 = new MyClass(42, "string")
 
// instantiating an object with additional constructors
let c2 = new MyClass(42)
let c3 = new MyClass("string")
Line 296 ⟶ 295:
 
* Creation procedures have no explicit return type (by definition of ''procedure'').{{Efn|Eiffel ''routines'' are either ''procedures'' or ''functions''. Procedures never have a return type. Functions always have a return type.}}
* Creation procedures are named.
* Creation procedures are designated by name as creation procedures in the text of the class.
* Creation procedures can be explicitly invoked to re-initialize existing objects.
Line 410 ⟶ 409:
# Default attribute values, if you have any.
my %defaults = ( foo => "bar" );
 
# Initialize attributes as a combination of default values and arguments passed.
my $self = { %defaults, @_ };
Line 473 ⟶ 472:
class Person
{
private $name;
 
public function __construct($name)
{
$this->name = $name;
}
 
public function getName()
{
return $this->name;
}
}
</source>
Line 518 ⟶ 517:
<source lang="ruby">
class ExampleClass
def initialize
puts "Hello there"
end
end