Niby Babu
 A constructor is a special member function whose task
is to initialize the objects of its class.
 It is special because it has same name as its class name.
 It invokes automatically whenever a new object of its
associated class is created
 It is called constructor because it constructs the values
of the data member of the class.
class add
{
int m, n ;
public :
add ();
};
add :: add ()
{
m = 0;
n = 0;
}
 When a class contains a
constructor, it is guaranteed that
an object created by the class will
be initialized automatically.
 add a ;
 Above declaration not only
creates the object a of type add,
but also initializes its data
members m and n to zero.
 There is no need to write any statement to invoke the
constructor function.
 If a ‘normal’ member function is defined for
initialization, we need to invoke that function for each
and every objects separately.
 A constructor that accepts no parameters is called the
default constructor.
 The default constructor for class A will be A : : A (
)
 If no constructor is defined in the class in
such situation the compiler implicitly
provides a constructor, which is called as
default constructor.
class sample
{
int someDataMember;
public:
void someFunction ()
{
.. ..
}
};
class sample
{
int someDataMember;
public :
sample()
{ }
void someFunction ()
{
.. ..
}
};
After
Compilation
1. The constructor do not have return type
even void.
2. The constructor must be declared in the
public section.
3. The constructor cannot be virtual.
4. They are invoked automatically when the
objects are created.
#include<iostream.h>
Class Number
{
int a;
public:
Number();
void square();
};
Number::Number()
{
a= 10;
}
void Number::square()
{
cout<<“Square :
”<<a*a;
}
Void main()
{
Number obj;
obj.square();
}
Output:
Square : 100

An introduction to constructor

  • 1.
  • 2.
     A constructoris a special member function whose task is to initialize the objects of its class.  It is special because it has same name as its class name.  It invokes automatically whenever a new object of its associated class is created  It is called constructor because it constructs the values of the data member of the class.
  • 3.
    class add { int m,n ; public : add (); }; add :: add () { m = 0; n = 0; }  When a class contains a constructor, it is guaranteed that an object created by the class will be initialized automatically.  add a ;  Above declaration not only creates the object a of type add, but also initializes its data members m and n to zero.
  • 4.
     There isno need to write any statement to invoke the constructor function.  If a ‘normal’ member function is defined for initialization, we need to invoke that function for each and every objects separately.  A constructor that accepts no parameters is called the default constructor.
  • 5.
     The defaultconstructor for class A will be A : : A ( )  If no constructor is defined in the class in such situation the compiler implicitly provides a constructor, which is called as default constructor.
  • 6.
    class sample { int someDataMember; public: voidsomeFunction () { .. .. } }; class sample { int someDataMember; public : sample() { } void someFunction () { .. .. } }; After Compilation
  • 7.
    1. The constructordo not have return type even void. 2. The constructor must be declared in the public section. 3. The constructor cannot be virtual. 4. They are invoked automatically when the objects are created.
  • 8.
    #include<iostream.h> Class Number { int a; public: Number(); voidsquare(); }; Number::Number() { a= 10; } void Number::square() { cout<<“Square : ”<<a*a; } Void main() { Number obj; obj.square(); } Output: Square : 100