C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 17)
17.
Which of the following statement is correct?
Discussion:
26 comments Page 1 of 3.
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;
}
#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?
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)
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)
YUKTHI said:
9 years ago
By default a constructor is immediately called as soon as an object is created.
(2)
Ankush said:
10 years 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;
}
#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;
}
Sathriyan said:
1 decade ago
When object is created automatically constructor is declared.
Naveen singh said:
1 decade ago
If constructor is not define in class, then it will be provided by JVM automatically.
(1)
Bala Manikandan said:
1 decade ago
A constructor is called at the time of initialization of an object and not at declaration.
Shrikant said:
1 decade ago
Because object needs to construct class elements and create real world object.
Poonam said:
1 decade ago
Constructor is called as soon as the object is invoked.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers