Measures Concepts
GitHub icon

Constructors

Constructors - language feature

< >
Example from JavaScript, TypeScript:
class Person { constructor(name) { this._name = name } } new Person("Jane")
class Foobar { public: Foobar(double r = 1.0, double alpha = 0.0) // Constructor, parameters with default values. : x_(r * cos(alpha)) // <- Initializer list { y_ = r * sin(alpha); // <- Normal assignment } private: double x_; double y_; }; Foobar a, b(3), c(5, M_PI/4);
Example from C#:
public class MyClass { private int a; private string b; // Constructor public MyClass() : this(42, "string") { } // Overloading a constructor public MyClass(int a, string b) { this.a = a; this.b = b; } } // Code somewhere // Instantiating an object with the constructor above MyClass c = new MyClass(42, "string");
Example from Speedie:
class Foobar |float| x |float| y constructor (|float| r=1, |float| alpha=0) .x = r * alpha.cos .y = r * alpha.sin || a = foobar() || b = foobar(3) || c = foobar(5, M_PI/4)
Example from CFML:
component { // properties property name="cheeseName"; // constructor function Cheese init( required string cheeseName ) { variables.cheeseName = arguments.cheeseName; return this; } }

Languages with Constructors include Java, JavaScript, Python, C++, PHP, MATLAB, C#, TypeScript, Objective-C, Visual Basic .NET, Object Pascal, Cython, Speedie, Action!, CFML, Pizza, Aardvark, Dale, Oracle Java, Deesel, cooC, Real-Time Concurrent C

Languages without Constructors include C, C3, progsbase, C2, Tick C

This question asks: Does the language have a syntax and/or notion of constructors?

Read more about Constructors on the web: 1.

HTML of this page generated by Features.ts

View source

- Build the next great programming language Search Add Language Features Creators Resources About Blog Acknowledgements Queries Stats Sponsor Day 605 feedback@pldb.io Logout