Constructor & Destructor
Constructors
• Def- A constructor is a special member
function that is a member of a class and
has same name as that class.
• Use- It is used to initialize the object of the
class type with a legal initial value.
Special Characteristics of Constructors
1. These are called automatically when the objects are
created.
2. All the objects of the class having a constructor are
initialized before some use.
3. These should be declared in the public section for
availability to all the functions.
4. Constructor has no return type even void.
5. They can not be inherited.
6. These cannot be static.
7. Default and copy constructor are generated by the
compiler whenever required. Generated constructors
are public.
8. Constructors can have default argument as other C++
functions.
Declaration and Definition
• It can be defined either inside the class
definition or outside the class definition
class X { int i ;
public:
int j,k ;
X() { i = j = k =0;}
};
class X { int i ;
public:
int j,k ;
X() { i = j = k =0;}
};
X::X()
{
I = j = k =0;
}
Note
• Generally a constructor should be defined
under the public section of a class, so that
its object can be created in any function
class X { int i ;
X() { i = j = k =0;}
public:
int j,k ;
void check(void); // member function
};
void X :: check (void)
{
X obj1; //valid
}
int main()
{
X obj2; //invalid
}
Default constructor
• A constructor that accept no parameter is
called the default constructor.
Copy Constructor
• A copy Constructor is used to initialize an object
from another object.
• If you have not defined a copy constructor, the
complier automatically creates it and it is public
• Copy constructor always takes argument as a
reference object.
Consider the following class definition
class sample {
int i, j;
public:
sample ( int a, int b)
{
i = a;
j = b;
}
sample ( sample & s)
{
i = s.i ;
j = s.j ;
}
void print(void)
{
cout<<i <<“ “<<j <<“n”;
}
Above constructors may be used as
follows
sample s1 (10,12); //1st const. called
sample s2 (s1) // copy const. called
sample s3 = s1; copy const. called
Home work
• In a class does not define a constructor,
what will be the initial value of its object.
• What are the significance of default
constructor.
• How many situations when a copy
constructor is automatically called.
Default Arguments
• Just like any other function a constructor
can also have default arguments
• This constructor is equivalent to default
constructor. Because its also allows us to
create objects without any value provided.
• For example
class A
{
int i,j;
public:
X(int x=10,int y=20);
};
A::X(int x,int y)
{
i=x;
j = y;
}
int main()
{
A obj1;
A obj2(250);
A obj3(2,4);
getch();
return 0;
}
i=10
j=20
i=250
j=20
i=2
j=4
obj1 obj2 obj3
Order of Constructor Invocation
• The objects are constructed in the order they are
defined. Consider the following statement
• Sample s1,s2,s3; // the order of construction is
s1,s2,s3.
• But when a class containing objects of another
class as its members. In such a case, the
constructor for member objects are invoked first
and then only, the constructor for containing
class is invoked.
• For example
class A
{
public:
A()
{
cout<<“Constructing A”
<<endl;
}};
class B
{
public:
B()
{
cout<<“Constructing B”
<<endl;
}};
class C
{
private:
A objA;
B objB;
public:
C()
{
cout<<“Constructing C”
<<endl;
}};
int main()
{
clrscr();
C objC;
getch();
}

Constructor & Destructor

  • 1.
  • 2.
    Constructors • Def- Aconstructor is a special member function that is a member of a class and has same name as that class. • Use- It is used to initialize the object of the class type with a legal initial value.
  • 3.
    Special Characteristics ofConstructors 1. These are called automatically when the objects are created. 2. All the objects of the class having a constructor are initialized before some use. 3. These should be declared in the public section for availability to all the functions. 4. Constructor has no return type even void. 5. They can not be inherited. 6. These cannot be static. 7. Default and copy constructor are generated by the compiler whenever required. Generated constructors are public. 8. Constructors can have default argument as other C++ functions.
  • 4.
    Declaration and Definition •It can be defined either inside the class definition or outside the class definition class X { int i ; public: int j,k ; X() { i = j = k =0;} };
  • 5.
    class X {int i ; public: int j,k ; X() { i = j = k =0;} }; X::X() { I = j = k =0; }
  • 6.
    Note • Generally aconstructor should be defined under the public section of a class, so that its object can be created in any function
  • 7.
    class X {int i ; X() { i = j = k =0;} public: int j,k ; void check(void); // member function }; void X :: check (void) { X obj1; //valid } int main() { X obj2; //invalid }
  • 8.
    Default constructor • Aconstructor that accept no parameter is called the default constructor.
  • 9.
    Copy Constructor • Acopy Constructor is used to initialize an object from another object. • If you have not defined a copy constructor, the complier automatically creates it and it is public • Copy constructor always takes argument as a reference object. Consider the following class definition
  • 10.
    class sample { inti, j; public: sample ( int a, int b) { i = a; j = b; } sample ( sample & s) { i = s.i ; j = s.j ; } void print(void) { cout<<i <<“ “<<j <<“n”; } Above constructors may be used as follows sample s1 (10,12); //1st const. called sample s2 (s1) // copy const. called sample s3 = s1; copy const. called
  • 11.
    Home work • Ina class does not define a constructor, what will be the initial value of its object. • What are the significance of default constructor. • How many situations when a copy constructor is automatically called.
  • 12.
    Default Arguments • Justlike any other function a constructor can also have default arguments • This constructor is equivalent to default constructor. Because its also allows us to create objects without any value provided. • For example
  • 13.
    class A { int i,j; public: X(intx=10,int y=20); }; A::X(int x,int y) { i=x; j = y; } int main() { A obj1; A obj2(250); A obj3(2,4); getch(); return 0; } i=10 j=20 i=250 j=20 i=2 j=4 obj1 obj2 obj3
  • 14.
    Order of ConstructorInvocation • The objects are constructed in the order they are defined. Consider the following statement • Sample s1,s2,s3; // the order of construction is s1,s2,s3. • But when a class containing objects of another class as its members. In such a case, the constructor for member objects are invoked first and then only, the constructor for containing class is invoked. • For example
  • 15.
    class A { public: A() { cout<<“Constructing A” <<endl; }}; classB { public: B() { cout<<“Constructing B” <<endl; }}; class C { private: A objA; B objB; public: C() { cout<<“Constructing C” <<endl; }}; int main() { clrscr(); C objC; getch(); }