You are on page 1of 3

C++ Programming 58 Class and Object

Chapter 6

Constructor and Destructor


Constructor: A constructor is a special member function which is called
automatically when an object is created. A constructor has the same name as the
class and has no return type. It is called constructor, because it constructs the values
of data members of the class. i.e. it is used to initialize private data members.
Characteristics of constructors:
 Constructor should be declared in public section.
 Constructor has the same name as the class name.
 No return type should be specified including void.
 It is invoked implicitly at the time of creation of the objects.
 The address of the constructors cannot be retrieved.
 An object with a constructor must not be used as a member of a union.
 Like other C++ functions, they can have default arguments.

Types of constructors
Depending on how the member data of objects are provided with values while
they are declared. C++ provides 4 types of constructors.
1. Default constructors.
2. Parameterized constructors or argument constructors.
3. Overloaded constructors.
4. Copy constructors.

1. Default constructor: When a constructor is defined without any argument, it


is called a default constructor.

Suppose xyz is a class, the default constructor in the class takes the
following form:

xyz( )
{
statements;
}

The statements within the body of the function assign the values to the data
members of the class.

P54. WAP to illustrate the default constructor.

2. Parameterized constructor:
A constructor with one or more arguments is called Parameterized constructor.
Since constructor is a function similar to other functions in C++, it is possible to pass
arguments to the constructors from the main function.

P55. WAP to illustrate the Parameterized constructor.

Mahesha S SSC, Tumkur Cell: 98860 44689


C++ Programming 59 Class and Object

3. Overloaded constructors:
A class may have zero or more constructors. If Two or more constructors
exists in the same class, they differ in their argument list such constructors are called
overloaded constructors.

The main reasons for using constructor overloading are :


 To get flexibility
 To allow creation of both initialized and uninitialized objects.
 To define copy constructors
 To support arrays.

P57. WAP to illustrate the Overloaded constructor.

4. Copy constructor: This is another form of parameterized constructor in which an


object is initialized with another object of same class.
Syntax:
classname (classname &obj)
{
//body of constructor
}
Here obj is a reference to an object that is being used to initialize another
object. The copy constructor takes one argument, passed by reference.

P58. WAP to illustrate the Copy constructor.

DESCTRUCTORS
A destructor is a special type of member function called automatically when the
object scope is ended.

A destructor has the same name as the constructor (same as the class name) but
prefixed by tilde (~) operator. The role of destructor is to deallocate the memory block
allocated by the constructor.

Characteristics:
1. Destructors do not have return type.
2. It should be a public member.
3. Destructors do not have any argument.
4. There should be only one destructor in each class.
5. Destructors cannot be overloaded.

P59. WAP to illustrate the Destructor.


P60. WAP to accept a date and print the same using overloaded constructor and
destructor

Review Questions

One mark Questions:


1. What is constructor? Why do we need it?
2. What is the purpose of a destructor?

Mahesha S SSC, Tumkur Cell: 98860 44689


C++ Programming 60 Class and Object

3. What is the role of destructor function in a class?


4. What is default constructor?
5. What name must have a constructor and destructor?

Three marks Questions:


1. Explain the concept of constructor overloading.
2. Give the special properties of constructor function.
3. When do you overload a constructor? Explain.
4. Give the difference between constructor and destructor.

Seven marks Questions:

1. a. What is destructor? How it differs from constructor? Why a destructor


cannot be overloaded?
b. Write the use of copy constructor.
2. Explain any 3 types of constructors.
3. a. What are constructors and destructor? Explain how they differ from normal
functions?
b. Explain parameterized constructor.

Mahesha S SSC, Tumkur Cell: 98860 44689

You might also like