Sunteți pe pagina 1din 15

1

MODULE-3

CONSTRUCTORS AND DESTRUCTORS

CONSTRUCTOR:
It is a method in the class with the same name as that of the class. It has no
return type. It gets called automatically whenever an object is created. The main purpose of constructor is
to perform initialization.

SYNTAX:
class name
{
---------
---------
Public: name(parameters)
{
-----------
-----------
}
};
name VI;
2

The constructor has some special characteristics:-


1. They should be declared in the public section.
2. They are invoked automatically when the objects are created.
3. They do not have return types, not even void and therefore, they
cannot return values.
4. They cannot be inherited, though a derived class can call the base
class constructor.
5. Like other C++ functions, they can have default arguments.
6. Constructors cannot be virtual.
7. We cannot refer to their addresses.
8. An object with a constructor(or destructor) cannot be used as member
of a union.
9. They make implicit calls to the operators new and delete when memory allocation is required.

There are different types of constructors:-

Default constructor
Parameterized constructor
Dynamic constructor
Multiple constructor
Copy constructor
3

DEFAULT CONSTRUCTOR:

A constructor that accepts no parameters is called the default constructor.

#include<iostream.h>
class student
{
int rno;
char sname[20];
public:
student( )
{
cout<<number:;
cin>>rno;
cout<<name:;
cin>>sname;
}
void show( )
{
cout<<rno<<\t<<sname<<end l;
}
};
4

void main( )
{
student s; // invoking a default constructor
s.show( );
}

PARAMETERIZED CONSTRUCTOR:

The constructors that can take arguments are called parameterized constructors.

Example:
#include<iostream.h>
class integer
{
int m, n;
public:
integer( int x, int y)
{
m=x;
n=y;
}
5

void display(void )
{
cout<<m=<<m<<\n;
cout<<n=<<n<<\n;
}
};
int main( )
{
integer int1(0, 100); // constructor called implicity
integer int2=integer(25, 75); // constructor called explicity
cout<<\n object 1<<\n;
int 1. display( );
cout<<\n object 2<<\n;
int 2.display ( );
return 0;
}
6

MULTIPLE CONSTRUCTOR: or (overloading constructor)

Any number of constructors can be written in a class is called as multiple constructor.


Example:
#include<iostream.h>
class student
{
char name[10];
int rno;
public:
student ( )
{
cout<<enter number;
cin>>rno;
cout<<enter name;
cin>>name;
}
student( int rollno , char n[10])
{
rno= rollno;
strcpy(name, n);
}
7

void show( )
{
cout<<rno:<<rno;
cout<<name:<<name;

}
};
void main( )
{
student s1, s2(2, xyz);
s1.show( );
s2.showI( );
}

Multiple constructor is also known as constructor overloading.


8

COPY CONSTRUCTOR:

A copy constructor is used to declare and initialize an object from the other object.

Example:
# include<iostream.h>
# include<string.h>
class student
{
int rno;
char sname[20];
public:
student(int rollno, char name[20])
{
rno=rollno;
strcpy(sname, name);
}
student(student &s)
{
rno= s.rno;
strcpy(sname ,s.sname);
}
9

void show( )
{
cout<<rollno=<<rno;
cout<<name=<<sname<<\n;

}
};
void main( )
{
student s1(1 ,abc);
student s2(s1); // copy constructor
student s3 = s1; // copy constructor(copy initialization)
s1.show( );
s2.show( );
s3.show( );
}
10

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 the new operator.

Example:
#include<iostream.h>
class student
{
int len;
char *name;
public:
student()
{
cout<<enter length of your name:;
cin>>len;
name=new char[len+1];
}
void read();
void print();
};
11

void student :: read()


{
cout<<enter your name:;
cin>>name;
}

void student :: print()


{
cout<<length of your name is<<len;
cout<<your name is<<name;
}

void main()
{
student s;
s.read();
s.print();
}
12

CONSTRUCTOR WITH DEFAULT ARGUMENTS:

It is possible to define constructors with default arguments.

Example:

#include<iostream.h>
class A
{
int x,y;
public:
A( )
{
x=0;
y=0;
}
A(int m, int n=1)
{
x=m;
y=n;
13

}
void put( )
{
cout<<x=<<x;
cout<<y=<<y;
}
};

void main( )
{
A A1, A2 (4, 5), A3 (6);
A1.put( );
A2.put( );
A3.put( );
}
14

DESTRUCTORS:
Destructor is a member function having its class name. It will not have any return type
and any parameters. It is preceded with a tilde (~) symbol. If the class is having any number of
constructors, we can define only single destructor. Destructors are automatically executed
whenever a program execution or a block execution is finished. These are used for destructing
objects which are constructed through constructor.

Example:

#include<iostream.h>
class student
{
int rno;
char sname[20];
Public:
student( )
{
cout<<constructed\n;
}
15

~student( )
{
cout<<destructed\n;
}
};
void main( )
{
student s[2];
}

S-ar putea să vă placă și