Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading

Initialization, Constructors, Destructors

Data Structures using C ++

Questions: 1. Explain with examples the types constructors in C++. 2. Explain with syntax multiple constructors in a class. 3. What is constructor with default arguments? 4. What is the purpose of constructor and destructor? Explain with suitable example the different types of constructors in C++. 5. Narrate on parameterized constructor and copy constructor with an example for each of them.

Initialization, Constructors, Destructors

Purpose:

Objects need to initialize the variables. Such initialized variables can then be used for processing. If the variables are not been initialized then they hold some garbage value. And if such variables are taken for operation then unexpected results may occur. In order to avoid that the class can include a special function called constructor.

The constructor can automatically be called whenever a new object of this class is created. The constructor will have the same name as the class name. It should not have any return type.

In C++ there are various ways of using constructors. Let us understand with the help of programming examples ‒

 

1. Default Constructor

This is the simplest way of defining the constructor.

Definition : A default constructor is a constructor that does not take any arguments. It is automatically called when an object is created without passing any parameters.

C++ Program

#include <iostream>

using namespace std;

class image

{

     private:

       int height, width;

     public:

       image()     // Constructor is defined. Note that name of the constructor is similar to the name of       the class.

//Purpose of constructor is to initialize the variables

       {

              height=0;

              width=0;

       }

       int area()

       {

              cout<<"Enter the value of height"<<"\n";

              cin>>height;

              cout<<"Enter The value of width"<<"\n";

              cin>>width;

              return (height*width);

       }

};

void main()  // object is created and values are initialized. When object gets created the compiler invokes the constructor image()

{

       image obj1;

       clrscr();

       cout<<"The area is :"<<obj1.area()<<endl;

       getch();

}

Output

Enter the value of height

10

Enter The value of width

20

The area is :200

 

2. Parameterised Constructors

Definition : A parameterised constructor is a constructor that accepts one or more arguments.

It is used to initialize the object with specific values at the time of its creation.

We can call the parameterised constructor using

1. Implicit call

2. Explicit call

For example:

Here an object obj1 gets created by passing the parameters 5 and 3 for the class image.

image obj1(5,3); <‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒ Implicit call

image obj1=image (5,3); <‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒ Explicit call

Most commonly use of implicit call is preferred in parameterised constructor. Following program makes use of parameterised constructor.

C++ Program

#include<iostream>

using namespace std;

class image

{

    private:

        int height,width;

    public:

        image(int x,int y) //constructor

        {

        height=x;

        width=y;

        }

        int area()

        {

        return (heightwidth);

        }

};

void main()

{

    image obj1(5,3);

    cout<<"The area is :"<<obj1.area()<<endl;

    getch();

}

Output

The area is 15

 

3. Overloaded Constructor

Definition : Constructor overloading in C++ means defining multiple constructors in the same class, each having a different number or type of parameters.

The overloaded constructor can be defined by more than one functions with the same name but having different number of parameters. For instance we can have

image();

image(int, int);

both the functions in the same program. Let us have the overloaded constructor in the following program ‒

C++ Program

#include<iostream>

using namespace std;

class image

{

private:

    int height, width;

public:

    image(int,int);//constructor

    image();//another constructor   // Note that both the constructors have the same name but one is default constructor and another is parameterised constructor.

    int area();//regular function   // The overloaded constructors are used here.

}:

image::image(int x, int y)//First constructor

{

    height=x;

    width=y;

}

image::image() //second constructor

{

    height=4;

    width=7;

}

int image::area()

{

    return (height*width);

}

void main()

{

    clrscr();

    image obj1(10,20);

    image obj2;

    cout<<"The area is :"<<obj1.area()<<endl;

    cout<<"\tFollowing area is obtained by another constructor"<<endl;

    cout<<"The area is :"<<obj2.area()<<endl;

    getch();

}

Output

The area is :200

Following area is obtained by another constructor

The area is :28

 

4. Copy Constructor

The copy constructor is called whenever a new variable is created from an object.

In C++ the copy constructor is created when the copy of existing object needs to be created. Usually the compiler creates a copy constructor for each class when no copy constructor is defined. Such a constructor is called implicit constructor and when the copy constructor is explicitly created in the program then it is called explicit constructor.

Definition : Copy constructor is a special type of constructor in which new object is created as a copy of existing object.

In other words in copy constructor one object is initialized by the other object. The general form of copy constructor is ‒

classname (classname &object)

{

   //body of the constructor

}

While invoking the copy constructor we will use following syntax

classname new_object_name(old_object_name);

Thus the copy constructor takes a reference to an object of same class as an argument.

Purpose :

The copy constructor is used when ‒

1. A new object is initialized from an existing object.

2. The object is passed by a value to a function.

3. An object is returned by value from a function.

C++ Program

#include<iostream>

using namespace std;

class test

{

   int x;

   public:

   //default constructor

   test();

   //parameterized constructor

test(int val)

{

   x=val;

}

//copy constructor

test(test &obj)

{

   x=obj.x;    //entered is in value in obj.x

}

void show()

{

   cout<<x;

}

};

void main()

{

   int val;

   cout<<"Enter some number"<<endl;

   cin>>val;

   test Old(val);

//call for copy constructor

   test New(Old); <‒‒‒‒ object 'Old' is passed as argument to object 'New'

   cout<<"\n The original value is: ";

   Old.show();

   cout<<"\n The New copied value is: ";

   New.show();

   cout<<endl;

   getch();

}

Output

Enter some number

500

The original value is: 500

The New copied value is: 500

In above program there are three constructors first one is the simple constructor and second constructor is a constructor in which parameter is passed. The copy constructor is always declared by passing reference parameter to it. And the reference variable is given by '&'. We can not pass the parameter by value to copy constructor.

 

5. Dynamic Constructors

Definition : The constructors can allocate the memory for the objects. Dynamic constructors are the kind of constructors which can allocate the memory for the objects using the operator new.

Following is a simple program which makes use of dynamic constructor for allocating the memory for the string.

C++ Program

#include<iostream>

#include<cstring>

using namespace std;

class DConstruct

{

   char *mystr;

   int len;

   public:

   DConstruct();

   DConstruct(const char*);

   void display();

};

DConstruct::DConstruct() //Dynamic constructor

{

   len=0;//initially length is 0

   mystr = new char[len+1];

   strcpy(mystr,""); //initialization by empty string

}

DConstruct::DConstruct(const char *s1) //Dynamic constructor

{

   len=strlen(s1);

   mystr=new char[len+ 1]; //allocating memory for mystr

   strcpy(mystr,s1);//copying the string to mystr

}

void DConstruct::display()

{

   cout<<mystr;

}

void main()

{

   DConstruct string1: //empty constructor gets called

   DConstruct string2("India"); //second dynamic constructor gets called

   clrscr();

   string1.display();

   cout<<"\n The copied string is: ";

   string2.display();

   getche();

}

Output

The copied string is: India

 

6. Characteristics of Constructors

Following are some rules that must be followed while making use of constructors‒

 1. Name of the constructor must be same as the name of the class for which it is being used.

2. The constructor must be declared in the public mode.

3. The constructor gets invoked automatically when an object gets created.

4. The constructor should not have any return type. Even a void type should not be written for the constructor.

5. The constructor can not be used as a member of union or structure.

6. The constructors can have default arguments.

7. The constructors can not be inherited. But the derive class can invoke the constructor of base class.

8. Constructors can make use of new or delete operators for allocating or releasing the memory.

9. Constructors can not be virtual.

10. Multiple constructors can be used by the same class.

11. When we declare the constructor explicitly then we must declare the object of that class.

 

Review Questions

1. Explain with examples the types constructors in C++.

2. Explain with syntax multiple constructors in a class.

3. What is constructor with default arguments?

4. What is the purpose of constructor and destructor? Explain with suitable example the different types of constructors in C++.

5. Narrate on parameterized constructor and copy constructor with an example for each of them.

 

Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ - Initialization, Constructors, Destructors


Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation