Introductions to
Constructors
Introductions to Constructors:
• When a variable is declared and if it is not initialized, it contains the
garbage value.
• The programmer needs to initialize variables with appropriate values.
• This can be done through the public member functions.
• In the first step the object is created and in the second step the data
members are initialized through public member functions.
• It could be better if the initialization is done at the time the object is
created or declared.
• This can avoid calling the public member function to initialize the data
members. This initialization is possible with constructors.
• The constructor is a special member function of a class that
is executed whenever an object is created.
• A constructor will have same name as the class and it does
not have any return type at all, not even void. Constructors
can be very useful for initializing the data members.
Syntax:
class name (argument list)
{
constructor body;
}
Eg:
Class s4
{
int a,b;
public:
s4 ()// constructor fun
{a=b=0;}
};
Main()
{
s4 s;// constructor fun invoked automatically when an object created
}
 Constructors are 7 type:
1) Default constructor
2) Parameterized constructor
3) Multiple constructor
4) Default argument constructor
5) Dynamic initialization of object
6) Dynamic constructor
7) Copy constructor
Default constructor:
The constructor fun does not having parameters
Class p
{int a,b;
Public:
P()
{a=b=10;}
};
Parameterized constructor:
The constructor fun which having parameters
Class pp
{ int a,b;
Public:
pp( int x, int y)
{a=x;b=y;}
};
Multiple constructor (constructor overloading):
One class contains more than one constructor function Default constructor
and parameterized constructor in one class
class pp
{ int a,b;
Public:
pp() // default constructor
{a=b=10;}
pp(int x, int y)// parameterized constructor {a=b=10;}
{a=x;b=y;}
};
Default argument constructor:
Constructor fun with default arguments
class pp
{ int a,b;
Public:
pp( int x=10,int y=20)// default argument constructor
{a=x;b=y;}
};
A class does not allow to contain both default constructor and default
argument constructor, it leads to an ambiguity error
Dynamic initialization of object:
Class object initialized dynamically ie initial values are provide during
run time, by reading values from main program
class pp
{ int a,b;
Public:
pp( int x,int y)
{a=x;b=y;}
};
Dynamic constructor:
The memory allocation of the object at run time using the memory
management operator new.
New operator is used to allocate memory space at run time.
Pointer-variable = new data type;
int*p; p = new int;
Here 2 bytes of memory is allocated and address is return to the
pointer variable p
Copy constructor:
Copy constructor is used to copy an object. The copy constructor allows the
programmer to create a new object from an existing one by initialization. The
argument of copy constructor is the address of class object
class pp
{ int a,b;
Public:
pp( int x=10,int y=20)// default argument constructor
{a=x;b=y;}
pp( pp &ob) // copy constructor
{a=ob.a;
b=ob.b;}
};

Introductions to Constructors.pptx

  • 1.
  • 2.
    Introductions to Constructors: •When a variable is declared and if it is not initialized, it contains the garbage value. • The programmer needs to initialize variables with appropriate values. • This can be done through the public member functions. • In the first step the object is created and in the second step the data members are initialized through public member functions. • It could be better if the initialization is done at the time the object is created or declared. • This can avoid calling the public member function to initialize the data members. This initialization is possible with constructors.
  • 3.
    • The constructoris a special member function of a class that is executed whenever an object is created. • A constructor will have same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for initializing the data members.
  • 4.
    Syntax: class name (argumentlist) { constructor body; } Eg: Class s4 { int a,b; public: s4 ()// constructor fun {a=b=0;} }; Main() { s4 s;// constructor fun invoked automatically when an object created }
  • 5.
     Constructors are7 type: 1) Default constructor 2) Parameterized constructor 3) Multiple constructor 4) Default argument constructor 5) Dynamic initialization of object 6) Dynamic constructor 7) Copy constructor
  • 6.
    Default constructor: The constructorfun does not having parameters Class p {int a,b; Public: P() {a=b=10;} };
  • 7.
    Parameterized constructor: The constructorfun which having parameters Class pp { int a,b; Public: pp( int x, int y) {a=x;b=y;} };
  • 8.
    Multiple constructor (constructoroverloading): One class contains more than one constructor function Default constructor and parameterized constructor in one class class pp { int a,b; Public: pp() // default constructor {a=b=10;} pp(int x, int y)// parameterized constructor {a=b=10;} {a=x;b=y;} };
  • 9.
    Default argument constructor: Constructorfun with default arguments class pp { int a,b; Public: pp( int x=10,int y=20)// default argument constructor {a=x;b=y;} }; A class does not allow to contain both default constructor and default argument constructor, it leads to an ambiguity error
  • 10.
    Dynamic initialization ofobject: Class object initialized dynamically ie initial values are provide during run time, by reading values from main program class pp { int a,b; Public: pp( int x,int y) {a=x;b=y;} };
  • 11.
    Dynamic constructor: The memoryallocation of the object at run time using the memory management operator new. New operator is used to allocate memory space at run time. Pointer-variable = new data type; int*p; p = new int; Here 2 bytes of memory is allocated and address is return to the pointer variable p
  • 12.
    Copy constructor: Copy constructoris used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization. The argument of copy constructor is the address of class object class pp { int a,b; Public: pp( int x=10,int y=20)// default argument constructor {a=x;b=y;} pp( pp &ob) // copy constructor {a=ob.a; b=ob.b;} };