Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
Filled in 6 bare reference(s) with reFill 2
No edit summary
Line 13:
 
<syntaxhighlight lang="cpp">
class ExamplePoint {
private:
public:
Example() int x;
int ay;
Example(int a, int b); // Parameterized constructor.
public:
 
Example::Example Point() = default;
private:
Point(int x, int x_;y):
Example(int a) x{x}, y{y} {} // Parameterized constructor
int y_;
};
 
Example::Example() = default;
 
Example::Example(int x, int y) : x_(x), y_(y) {}
</syntaxhighlight>
 
<syntaxhighlight lang="cpp">
ExamplePoint ep = ExamplePoint(0, 50); // Explicit call.
ExamplePoint e2p2(0, 50); // Implicit call.
</syntaxhighlight>
 
Line 37 ⟶ 33:
 
<syntaxhighlight lang="cpp">
import std;
#include <iostream>
 
class StudentPoint {
private:
public:
int x;
int y;
public:
Student(int ax = 0, int by = 0); // Default constructor.
 
int a;
int b;
};
</syntaxhighlight>
Line 50 ⟶ 46:
=== Copy constructors ===
{{see also|Copy constructor (C++)}}
Like C++, Java also supports "Copy ConstructorConstructors". But, unlike C++, Java doesn't create a default copy constructor if you don't write your own. Copy constructors define the actions performed by the compiler when copying class objects. A Copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object). It is used to create a copy of an existing object of the same class. Even though both classes are the same, it counts as a conversion constructor.
While copy constructors are usually abbreviated copy ctor or cctor, they have nothing to do with class constructors used in [[.NET]] using the same abbreviation.
 
Line 83 ⟶ 79:
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 where the initializer list cannot be used.
 
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 90 ⟶ 86:
 
<syntaxhighlight lang="cpp">
class FoobarPolarPoint {
private:
public:
Foobar(double r = 1.0,x;
double y;
public:
PolarPoint(double r = 1.0, double alphatheta = 0.0): // Constructor, parameters with default values.
: x_( x{r * std::cos(alpha)theta)}, y{r * std::sin(theta)} //* <- Initializer list */ {
std::println("Hello"); // Constructor body
{
{}
y_ = r * sin(alpha); // <- Normal assignment
}
 
private:
double x_;
double y_;
};
</syntaxhighlight>
Example invocations:
<syntaxhighlight lang="cpp">
PolarPoint a, b(3), c(5, std::numbers::pi/4);
Foobar a,
b(3),
c(5, M_PI/4);
</syntaxhighlight>
 
Line 325 ⟶ 316:
 
<syntaxhighlight lang="java">
class Example {
public Example() { // Non-parameterized constructor
{
Example() // Non-parameterized constructor
{
this(1); // Calling of constructor
System.out.println("0-arg-cons");
}
 
Example(int a) // Parameterized constructor
public Example(int a, int b); { // Parameterized constructor.
{
System.out.println("1-arg-cons");
}
Line 346 ⟶ 335:
 
<syntaxhighlight lang="java">
public class Example {
// Declaration of instance variable(s).
{
private int data;
 
// Definition of the constructor.
public Example() {
{
this(1);
}
 
// Overloading a constructor
public Example(int input) {
{
data = input; // This is an assignment
}
 
// Declaration of instance variable(s).
private int data;
}
</syntaxhighlight>
Line 624 ⟶ 610:
=> #<ExampleClass:0x007fb3f4299118>
</syntaxhighlight>
 
=== Rust ===
[[Rust (programming language)|Rust]] does not have constructors in the sense of object-oriented programming, but often structs have a <code>new()</code> method that essentially acts as a constructor. The return type is usually indicated as <code>Self</code>.
 
<syntaxhighlight lang="rust">
struct Point {
x: b(3)i32,
y: i32,
}
 
impl Point {
pub fn new(x: i32, y: i32) -> Self {
Point { x, y }
{}
}
 
fn main() {
let p: Point = Point::new(10, 20);
println!("Point is at ({}, {})", p.x, p.y);
}
<syntaxhighlight>
 
=== Visual Basic .NET ===