C++ Programming - OOPS Concepts - Discussion

Discussion Forum : OOPS Concepts - General Questions (Q.No. 17)
17.
Which of the following statement is correct?
A constructor is called at the time of declaration of an object.
A constructor is called at the time of use of an object.
A constructor is called at the time of declaration of a class.
A constructor is called at the time of use of a class.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
26 comments Page 1 of 3.

Ankush said:   1 decade ago
Here is an example you can understand clearly :).

#include <iostream>

using namespace std;

class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor
Line::Line(void)
{
cout << "Object is being created" << endl;
}

void Line::setLength( double len )
{
length = len;
}

double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line;

// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

Baru said:   1 decade ago
Constructor having same class name that's why we are call the constructor at the time of object creation.

Object creation is:.

Classname refernce variable=new classname () ;.

In this constructor name n classname is also same that's why we are call the constructor at the time of object creation.

Divya said:   1 decade ago
While creating an object, memory is allocated to that object when its constructor is invoked, it invokes only once during declaration of object, if you have not defined any constructor inside class then default constructor is invoked.

Vedi G said:   6 years ago
constructor is called at the time of object creation.

#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"constructor"<<endl;
}
}
int main()
{
A b1;
}
(2)

Nino said:   8 years ago
A constructor is called at the time of declaration of an object.

class A {};
class C /* declaration of a class */
{
A a_; /* declaration of an object (but not definition) of type A */
};

will call the constructor of A?
(2)

Ashish Bardiya said:   1 decade ago
Constructor is call when an object is created or initialize. When we not write a code for constructor then compiler automatically added default constructor.

Sanjay nannaware said:   9 years ago
The constructor is a special member function of a class which is called/invoked when the object is created. It has the same name as that of a class.
(1)

Farhana said:   1 decade ago
Constructor is a special member function of a class which is called/invoked when the object is created. It has the same name as that of a class.

Payal Y. Makan said:   1 decade ago
A constructor is called whenever an object is created and declaration is nothing but creation of an object.

Vikrant Siwach said:   1 decade ago
A constructor is always called when an object is initialized, not declared and certainly not "used".


Post your comments here:

Your comments will be displayed after verification.