0% found this document useful (0 votes)
26 views

Constructor

Bca constructors

Uploaded by

tsafridi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Constructor

Bca constructors

Uploaded by

tsafridi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1 What is constructor?

What are the different types of constructors(15)

2 What are the characteristics of a constructor(5 or 2)

3 Define a copy constructor(2)

4 Give the format of a dynamic constructor(2)

5 What is destructor? Explain its characteristics. Can it accept


arguments(15)

6 Differentiate constructor and destructor(2)

7 How do we invoke a constructor function?(2)

8. Explain the general form of defining a constructor in a derived class.(5)

9. What is automatic initialisation of objects(2)

Constructor
Definition
• Special member function with the same name as that of the class.
• Its purpose is to allocate appropriate memory to the objects of a class and
to initialize objects.
Example: class integer
{
int a,b;
public: integer( ) //constructor
{
a =b = 0;
}
};

Characteristics of a constructor
• They should be declared in the public section
• They are invoked automatically when the objects are created
• They can have default arguments.
• They can be overloaded
• They do not have return types, not even void. So they cannot return any
values.
• They cannot be inherited, but a derived class constructor can call the base
class constructor
• They cannot be virtual.
• An object with a constructor or destructor cannot be used as a member of
a union.
• They make implicit calls to the operators new and delete, when memory
allocation is required
Different types of constructors
Three types of constructors
1. Default constructor
• A constructor that accepts no arguments or parameters
• If user does not define a default constructor, the compiler supplies
it.
• It is invoked automatically , when the object is declared.
Example
integer obj; /*will automatically invoke the default constructor*/
[Note: here integer is the class name and obj is its object.]
2. Parameterized constructor
• Constructor that receives arguments.
• Can be used to initialise different objects of a class with different
values when they are created.
• Can be called implicitly or explicitly
Example:
/* implicit call */
integer obj1(10, 20);
integer obj2(30,40);
/* explicit call */
integer obj2 = integer(30, 40);
3. Copy constructor
• It accepts a reference to its own class as a parameter.
Example
class integer
{….
……
public: integer(integer &i)
};

Multiple constructors in a class


Class integer
{
int a, b;
public:
integer( ) //default constructor
{
a=0; b = 0 ;
}

Integer (int x, int y) //paremeterized constructor


{
a = x; b = y;
}

integer( integer &i) //copy constructor


{
a = i.a;
b = i.b;
}

The declaration integer I1; would invoke the default constructor


The statement integer I2 (20, 40); would invoke parameterised
constructor.
The statement integer I3 (I2); would invoke the copy constructor which
copies the values of I2 to I3.

Format of a dynamic constructor


Allocation of memory to objects at the time of their construction is
known as dynamic construction of objects. The memory is allocated with
the help of a new
operator
Example:
class string
{
char *name;
int length;
public:
string( ) // constructor1
{
length = 0;
name = new char[length + 1];
}
string( char *s) //constructor2
{
length = strlen(s);
name = new char[ length + 1];
strcpy(name, s);
}
};

int main( )
{

string name1(“Louis”);

….

}
Format for defining a constructor in a derived class
The header line of constructor definition consists of two parts separated
by a colon.
The first part includes the declaration of all arguments, passed to the
derived constructor.
The second part lists the function calls to the base constructor
Derived constructor( arglist1,arglist2,…..arglistN, arglistD) :
base1(arglist1), base2(arglist2),…baseN(arglistN)
{
Body of derived constructor
}
Example
Class alpha
{
int x;
public: alpha(int i ){ x = i; }
};
Class beta
{
float y;
public: beta( int j){ y = j; }
}
class gamma: public alpha, public beta
{
int m, n;
public: gamma( int a, float b, int c, int d: alpha(a), beta(b)
{
m = c;
n = d;
}
}

Destructor
Definition
• It is a special member function with the same name as that of the
class name, preceded by a tilde ( ̴ ) eg: ̴ integer( ) { }
• It is used to destroy objects created by the constructor
• Destructor is automatically executed by the compiler when the
object goes out of scope or upon exit from a program or a block.
• It is also invoked when delete operator is used to free memory
allocated with class pointer.
• A class can have only one destructor.

Characteristics of a destructor
• Destructor does not have a return type, not even void
• Destructor does not receive any arguments
• They do not have default values nor can be overloaded
• Destructors cannot be inherited, but the derived class can call the
destructor of the base class
• Destructors can be virtual, but constructors cannot
• Compiler can define a destructor if not explicitly defined.
• Destructor can make implicit calls to operator delete, if memory
de-allocation is needed for an object
• An object with a constructor or destructor cannot be used as a
member of a union.

Automatic initialization of objects


C++ provides a special member function called the constructor
which enables an object to initialize itself when it is created

You might also like