Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
aded content
Tags: section blanking Mobile edit Mobile web edit
m Reverted edits by 197.156.122.147 (talk) to last version by Lfstevens
Line 12:
== Types ==
{{unreferenced section|date=June 2013}}
 
=== Parameterized constructors ===
 
Constructors that can take arguments are termed as parameterized constructors.
The number of arguments can be greater or equal to one(1).
For example:
<source lang="cpp">
class Example
{
int x, y;
public:
Example();
Example(int a, int b); // Parameterized constructor
};
Example :: Example()
{
}
Example :: Example(int a, int b)
{
x = a;
y = b;
}
</source>
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="cpp">
Example e = Example(0, 50); // Explicit call
 
Example e(0, 50); // Implicit call
</source>
 
=== Default constructors ===