Types of constructors

S

Sathyaish

What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.
 
S

Sathyaish

That is a treasure, Rolf. Thank you so very much.

I understood every bit of it. I want some more. Can you please provide
me some simple, easy-to-understand code snippets for conversion
constructors and copy constructors?
 
R

Rolf Magnus

Sathyaish said:
What is a private constructor,

It's a constructor that is declared in a private section of a class.
and why would a class have one?

Because it might not want other classes to be able to create instances of
it.
What are the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.

Just like any member, a constructor can be public, protected or private. And
just like normal member functions it can have parameters or not.

There is a special kind of constructors, the conversion constructors. A
constructor that can be called with exactly one argument is considered a
conversion constructor, used to convert the parameter type into the class
the constructor belongs to.
And there is a special kind of conversion constructors, the copy
constructor. 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.
 
M

Mike Wahler

Sathyaish said:
That is a treasure, Rolf. Thank you so very much.

I understood every bit of it. I want some more. Can you please provide
me some simple, easy-to-understand code snippets for conversion
constructors and copy constructors?

class A
{
public:
/* 'conversion constructor': */
/* converts a type 'int' object */
/* to a type 'A' object */
A(int)
{
}

/* 'copy constructor': */
/* creates a copy of an existing */
/* type 'A' object */
A(const A& a)
{
}
};

int main()
{
int i(0);

A a1(i); /* creates type 'A' object by */
/* converting from type 'int' object 'i' */
/* (calls constructor 'A::A(int) )' */

A a2(a1); /* creates a copy of type 'A' object 'a1' */
/* (calls constructor 'A::A(const A&) )' */

return 0;
}

-Mike
 
Joined
Jun 6, 2011
Messages
1
Reaction score
0
Sathyaish wrote:

> What is a private constructor,


It's a constructor that is declared in a private section of a class.

> and why would a class have one?


Because it might not want other classes to be able to create instances of
it.

> What are the other kinds of constructors besides:
>
> (1) public constructors; and
> (2) parameterized constructors
>
> And I understand that they are not mutually exclusive of one another.


Just like any member, a constructor can be public, protected or private. And
just like normal member functions it can have parameters or not.

There is a special kind of constructors, the conversion constructors. A
constructor that can be called with exactly one argument is considered a
conversion constructor, used to convert the parameter type into the class
the constructor belongs to.
And there is a special kind of conversion constructors, the copy
constructor. 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.
----------------------------------------------------
Hi genius, i have two questions here.
1) Is it possible to have a constructor in the private section? because whenever we create an object in the main() the constructor gets invoked. since the object creation is done outside the class boundary, having the constructor in private section of the class leads to error.

2) And i am little confused with the second answer "Because it might not want other classes to be able to create instances of it" How does one class create instance of the other? can you please help me answering both the questions? thanks in advance.

------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top