+ 6
What are constructors? how are they created? What is their use?
anyone please tell..?
7 Réponses
+ 1
On the surface, constructors are very resemble a function but without any return type.
They have the same name as their class in which they are declared. The name constructor refers to the process of constructing an object from a class.
Below the surface, each and every class you create has at least one default constructor even when you omit to declare it explicitely.
class Foo {
public:
Foo(); // constructor
};
or
class Foo {
// still has a public constructor like above
};
Using constructors has some benefit for keeping the class attributes (data members) initialized during construction process of an object.
class Foo {
public:
Foo(const string &s, int x)
: str(s), num(x)
{ }
private:
string str;
int num;
};
int main() {
string sss = "Boo!";
int a = 777;
Foo f(sss, a);
}
+ 4
AK-47 thank you dude...
+ 3
Alexander Sokolov thank you sir!....actually i want to know about constructors and not the constructor conversion...when I'll learn constructors then I'll learn about constructor conversion.....once again Thank you....
+ 2
constructor's name is same as class name, it does not have a return type not even a void, constructor are used to initialize data member, it gets automatically invoked when we create objects of a class,there can be default,parameterized constructor, default means which does not have any parameters,a constructor which have a one parameter is called as one parameterized constructor, if 2 parameteres then 2 patameterized construtor.
+ 2
@ashish
"CONTRACTOR" ..............really!
+ 1
Contractor is special type of member function which is used initialize the variable inside the class. The name of the constructor is same as the name of the class.It is created in the following way:
#Include<stdio. h>
Using namespace std ;
Class num
{
Int X, Y, z;
num(Int p, Int q, Int r) ; // parameterized constructor declared //
num() ; //default constructor //
}
When the constructor have any arguments is known as parameterized constructor. When the constructor have no arguments is known as default constructor.