Computer Science Notes for Class 12 (ASSEB / AHSEC)

Chapter 4 - Constructor and Destructor

Next -Destructor

Introduction to Constructor

In this chapter, we will learn about constructor, need for constructor, its characteristics, types of constructor and constructor overloading

What is a Constructor?

A constructor is a special member function of a class that is automatically called whenever an object of the class is created.

Its main purpose is to initialize the data members (variables) of an object.

Definition

A constructor is a special member function having the same name as the class and no return type. It is automatically executed when an object is created

Why Do We Need Constructors?

Suppose we create a student object

Student s1;

Without a constructor, the variables inside the object may contain garbage values.

A constructor assigns proper initial values automatically.

Example Without constructor

Roll = Garbage

Marks = Garbage

Example With constructor

Roll = 1

Marks = 95

Thus, constructors make programs safer and easier to use

Characteristics of Constructors

  • Constructor name must be the same as the class name
  • It has no return type, not even void.
  • It initializes data members.
  • A constructor can be overloaded.
  • Constructors may have parameters.
  • They are generally declared in the public section.
  • Constructors cannot be inherited.
  • Constructors cannot be virtual

Syntax of a Constructor

class ClassName

{

public :

                ClassName( )

                {

                // Initialization

                }

};

Types of Constructors

There are three types of constructors:

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor

1.Default Constructor

A constructor that does not take any arguments is called a default constructor.

Syntax

ClassName( )

{

}

Example

Student( )

{

                roll = 10;

                marks = 85;

}

Advantages of Default Constructor

  • Initializes every object automatically
  • Reduces program effort
  • Avoids garbage values.
  • Makes program reliable

2.Parameterized Constructor

A constructor that accepts arguments (parameters) is called a parameterized constructor. Different objects can be initialized with different values.

Syntax

ClassName ( type variable)

{

}

Example

#include<iostream>

using namespace std;

class Student

{

                int roll;

                int marks;

public:

               Student (int r, int m)

                {

                                roll = r;

                                marks = m;

                }

                void display( )

                {

                                cout<<”Roll = “<<roll<<endl;

                                cout<<”Marks = “<<marks<<endl;

                }

};

int main( )

{

                Student s1(1,90);

                Student s2(2,95);

                s1.display( );

                s2.display( );

                return 0;

}

Output

Roll = 1

Marks = 90

Roll = 2

Marks = 95

Explanation

For object s1

r = 1

m = 90

For object s2

r = 2

 m = 95

Thus, different objects can have different initial values.

3.Copy Constructor

A constructor that creates one object by copying another object of the same class is called a copy constructor.

Syntax

ClassName(ClassName &obj)

{

}

Example

#include<iostream>

using namespace std;

class Student

{

                int roll;

public:

                Student (int r)

                {

                                roll = r;

                }

                Student (Student &obj)

                {

                                roll = obj.roll;

                }

                void display( )

                {

                                cout <<”Roll =”<<roll<<endl;

                }

};

int main( )

{

                Student s1(10);

                Student s2(s1);

                s1.display( );

                s2.display( );;

                return 0;

}

Output

Roll = 10

Roll = 10

Constructor Overloading

A class can have more than one constructor with different parameter lists. This is called constructor overloading.

Example

#include<iostream>

using namespace std;

class Student

{

               int roll;

public:

                Student ( )

                {

                                roll = 0;

                }

                Student ( int r)

                {

                                roll = r;

                }

                void display( )

                {

                                cout<<”Roll = “<<roll<<endl;

                }

};

int main( )

{

                Student s1;

                Student s2(15);

                s1.display( );

                s2.display( );

                return 0;

}

Output

Roll = 0

Roll = 15

Important Board Examination Points

Next - Destructor
Scroll to Top