top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: How many types of constructors C++ support ?

+1 vote
385 views
C++: How many types of constructors C++ support ?
posted Sep 4, 2016 by Ganesh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

I don't have depth knowledge of C++, however I know the basics of C++. C++ supports three types of constructors named as default constructor, parameterized constructor and copy constructor.
Default constructor does not take any input from user while creating and initializing an object.
Example: A() { a = 10;l b = 20} where a and b are data members of the class A.

Parameterized constructor takes input from the user to create and initialize the object.
Example: A(int x, int y) { a = x; b = y} where a and b are data members of the class A and initialized with the values (x and y) that user passed.

Copy constructor is a special type of constructor while uses another object to create and initialize the object.
Example: A(A & object) { a = object.a; b = object.b} where a and b are data members and those are initialized from object of same class type.

One more thing, parameterized constructor can have default arguments. I meant to say that if user does not pass a particular input parameter constructor definition would consider the default value for that parameter.

answer Sep 5, 2016 by Harshita
Similar Questions
+1 vote

Are C++11 final classes really final, or is it still possible to define a derived class through some hack without violating language rules?

It seems we devirtualize calls to virtual members of final classes in some cases, so I assume final is really final, but I'm not sure. :-)

0 votes

Which is the best practice for variable declaration within the function ? Or is it vary from one language to other one ?

+6 votes

First see this example: let you have a number 8 which is divisible by 4 which is square of 2 and 27 which is divisible by 9 which is square of 3.I need a solution for numbers input from 1 to 10^18.hope for efficient program written in any programming language.
Thanks in advance.

...