Documente Academic
Documente Profesional
Documente Cultură
MODULE-3
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
Default constructor
Parameterized constructor
Dynamic constructor
Multiple constructor
Copy constructor
3
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
void show( )
{
cout<<rno:<<rno;
cout<<name:<<name;
}
};
void main( )
{
student s1, s2(2, xyz);
s1.show( );
s2.showI( );
}
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 main()
{
student s;
s.read();
s.print();
}
12
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];
}