What are constructors? how are they created? What is their use? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 6

What are constructors? how are they created? What is their use?

anyone please tell..?

19th Jun 2018, 3:11 PM
ƀbhĆ®shĆ«k[Inactive]
7 Respostas
+ 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); }
19th Jun 2018, 4:02 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 4
AK-47 thank you dude...
19th Jun 2018, 4:03 PM
ƀbhĆ®shĆ«k[Inactive]
+ 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....
19th Jun 2018, 3:20 PM
ƀbhĆ®shĆ«k[Inactive]
+ 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.
19th Jun 2018, 4:03 PM
Tarika
Tarika - avatar
+ 2
@ashish "CONTRACTOR" ..............really!
31st Jul 2018, 11:33 AM
Harry Potter
Harry Potter - avatar
+ 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.
19th Jun 2018, 5:21 PM
Ashish Kumar Sahoo
Ashish Kumar Sahoo - avatar