2. Concepts
• Constructor
• Parameterized constructor
• Multiple constructor in a class
• Constructor with default arguments
• Copy constructor
• Const object
• Destructor
3. Constructor
A constructor in c++ is a special method that is
automatically called when an object of a class
in created.
It is used to initialize the data members of new
object generally. The constructor in c++ has
the same name as class or structure.
4. Parameterized constructor
• The constructor integer() defined initialized the
data members of all the objects to zero.
• It may be necessary to initialize the various data
element of different objects with different values
when they are created.
• C++ permits us to achieve this objective by
passing arguments to the constructor function
when the objects are created.
• the constructor that can take arguments are
called parameterized.
5. Multiple Constructor in a Class
• The constructor itself supplies the data values
and no values are passed by the calling
program.
• In the second case, the function call passes
the appropriate values from main().
• C++ permits us to use both these constructor
in the same class.
6. Constructor with default arguments
It is possible to define constructor with default
argument. For example, the constructor
complex()can be declared as follows:
Complex(float real, float imag=0);
The default value of the argument imag ils zero.
Then the statement
complex c(5.0);
Assigns the value 5.0 to the real variable and 0.0
imag. However, the statement
complex c(2.0,3.0);
7. Copy constructor
A copy constructor is a member function which
initializes an object using another object of
the same class.
When as object of the class is returned by value.
When an object of the class is passed by value
as an argument.
When an object is constructed based on another
object of the same class.
8. const Object
• We may create and use constant objects using
const keyword before object declaration.
• For example, we may create X as a constant
object of the class matrix as follows:
const matrix X(m, n);
Any attempt to modify the values of m and n will
generate compile-time error. Further, a constant
object can only const member functions.
As we know, a const member is a function
prototype or function definition where the
keyword const appears after the function’s
signature.
9. Destructors
A destructor as the name implies is used to
destroy the objects that have been created by
a constructor. Like a constructor the
destructor is a member function whose name
is the same as the class name but is preceded
by a tilde. For example , the destructor for the
class integer can be defined as shown below:
~integer(){ }
10. Cont…
A destructor never takes any argument nor dose
it return any value. It will be invoked implicitly
by the compiler upon exit from the program
to clean up storage that is no longer
accessible. It is a good practice to declare
destructors in a program since it releases
memory space for future use
11. Cont…
Whenever new is used to allocate memory in the
constructors we should use delete to free that
memory. For example, the destructor for the
matrix class discussed above may ne defined as
follows:
matrix :: ~matrix()
{
for(int i=0;i<d1;i++)
delete p[i];
delete p;
}