Вы находитесь на странице: 1из 10

Reference: - Object Oriented Programming With C++ by E.

Balagurusamy

Constructors & Destructors

Constructors

- A constructor is a special member function whose task is to initialize the objects of its
class.
- It is special because its name is same as the class name.
- The constructor is invoked whenever an object of its associated class is created.
- It is called constructor because it constructs the values of data members of the class.
- Like member function constructor can be defined within or outside of class.
- Example:-

class Integer{

int i;
public:
Integer( ){
i=0; Constructor definition with in class.
}
};
main( ){
Integer a; Invoking the constructor.
}
- A constructor that accepts no parameters are called the default constructor.
- If no default constructor is defined, then the compilers supplies default constructor.
- Special characteristics of Constructor are:
o These are called automatically when the objects are created.
o All objects of the class having a constructor are initialized before some use.
o These should be declared in the public section for availability to all the
functions.
o Return type (not even void) cannot be specified for constructors.
o These cannot be inherited, but a derived class can call the base class
constructor.
o These cannot be static.
o Default and copy constructors are generated by the compiler wherever
required. Generated constructors are public.
o These can have default arguments as other C++ functions.
o A constructor can call member functions of its class.
o A constructor can call member functions of its class.
o The make implicit calls to the memory allocation and deallocation operators
new and delete.
o These cannot be virtual.

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Parameterized Constructor:

- The constructor that can take arguments are called parameterized constructor.
- Remember, when the constructor is parameterized, we must provide appropriate
arguments for the constructor.
- The parameters of a constructor can be any type except that the class to which it
belongs.
- However, a constructor can accept a reference to its own class as a parameter. In such
cases constructor is called copy constructor.
- Example:-

class Point{

int x,y;
public:
Point (int a, int b ){
x=a; Parameterized Constructor.
y=b;
}
}; Invoking the parameterized
main( ){ constructor implicitly.
Point a(10,20);
}

- Constructor can invoked explicitly as follows:


Point a = Point(10,20);

Multiple Constructors in class.

- When more than one constructor function is defined in a class, we say that constructor
is overloaded.
- Example:-

class Point{

int x,y;
public:
Point (int a, int b ){
x=a; Constructor Overloading .
y=b;
}
Point( ){
X=y=0;
}
}; Invoking constructors implicitly.
main( ){
Point b,a(10,20);

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

}
-

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

C++ Program to calculate age of a person.


#include<iostream.h>
#include<conio.h>
class Date{
public:
int d,m,y;
public:
Date(); Default
Date(int,int,int); constructor
void age(Date,Date);
};
Date::Date(){
D=m=y=0;
} Parameterized
Date::Date(int dd, int mm, int yy){ Constructor
d=dd;
m=mm; Both constructors
y=yy; are defined outside
} of class.

Date Date::age(Date tod, Date dob){


Date a;
a.y=tod.y-dob.y;
if(tod.m<dob.m){
a.y-=1;
tod.m+=12;
}
a.m=tod.m-dob.m;
if(tod.d<dob.d){ Invoking
a.m-=1; Constructors
tod.d+=30;
}
a.d=tod.d-dob.d;
return a;
}

main(){
int td,tm,ty,bd,bm,by;
clrscr();
cout<<"Enter Todays Date\n" ;
cin>>td>>tm>>ty;
cout<<"Enter Date of birth\n";
cin>>bd>>bm>>by;
Date t(td,tm,ty),b(bd,bm,by),a;
a=t.age(t,b);
cout<<a.y<<"-years "<<a.m<<"-months "<<a.d<<" -days";

getch();
}

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Constructors with Default Arguments

- It is also possible to define construct with default arguments.


- For example, constructor point can be defined as
o Point(int =0, int=0);
o Point(int, int=0);
- It is important to distinguish between the default constructor A::A( ) and default
argument constructor A::A(int a=0).
- The default argument constructor can be called with either one argument or no
arguments.
- When called with no arguments, it becomes a default constructor.
- When both forms are used in class, it causes ambiguity for statement
A a;

The ambiguity is whether to call A::A( ) or A::A(int a=0).

- Example:-

class Point{

int x,y;
public:
Point(int=0,int=0);
}; Constructor with default argument
Point::Point (int a, int b ){
x=a;
y=b;
}

main( ){
Point a;
Point(10); Invoking constructors implicitly.
Point(10,20);
}

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Dynamic Initialization of Objects

- Class objects can be initialized dynamically too.


- That is to say, initial value of an object may be provided during run time.
- One advantage of dynamic initialization is that we can provide various initialization
formats, using overloaded constructor.
- This provides the flexibility of using different format of data at runtime depending
upon situation.
- Example:-
class Point{
int x,y;
public:
Point(int ,int);
void display( );
};
Point::Point (int a, int b ){
x=a;
y=b;
}
Point::display( ){
cout<<x<<y;
}

main( ){
int a,b;
cout<< “Enter two points\n”;
cin>>a>>b;
Point p(a,b);
}

Copy Constructor

- A copy constructor is used to declare and initialize an object from another object.
- For Example, the statement
Integer i2(i1);
Would define the object i2 and at the same time initialize it to the value of i1.
- Another form of above statement is
Integer i2=i1;
- The process of initializing through a copy constructor is known as copy initialization.
- Remember the statement
i2=i1;
will not invoke the copy constructor.
- However, if i1 and i2 are objects, this statement (i2=i1) is legal and simply assigns the
values of i1 to i2. This is task of overloaded assignment operator.

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

- A constructor takes a reference to an object of the same class as itself as an argument.


- Example:-

class Integer{
int a;
public:
Integer( ){
a=100;
}
Integer(Integer &o){ Copy Constructor
a=o.a
}
void dispay( ){
cout<<a<<endl;
}
};
main( ){
Integer i1;
Integer i2=i1; Invoking Copy Constructor
i1.display( );
i2.display( );
}

Dynamic Constructors

- The constructors can be also used to allocate memory while creating objects.
- Allocation of memory to the objects at the time of their construction is called as
dynamic construction of objects.
- The memory is allocated with the help of new operator.
- Example:-
class Point{
int *x,*y;
public:
Point(int ,int);
void display( );
};
Point::Point (int a, int b ){
x=new int (a);
y=new int(b);
}
Point::display( ){
cout<<x<<y;
}
main( ){
int a,b;

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

cout<< “Enter two points\n”;


cin>>a>>b;
Point p(a,b);
p.display( );
}

Destructor.

- The syntax for declaring a destructor is :


~name_of_the_class( )
{
}
So the name of the class and destructor is same but it is prefixed with a ~ (tilde).
- It does not take any parameter nor does it return any value.
- Overloading a destructor is not possible and can be explicitly invoked.
- In other words, a class can have only one destructor.
- A destructor can be defined outside the class.
- Example:-
class Point{
int *x,*y;
public:
Point(int ,int);
~Point( );
void display( );
};
Point::Point (int a, int b ){
x=new int (a);
y=new int(b);
}
Point::display( ){
cout<<x<<y;
}
Point::~Point( ){
delete x;
delete y;
cout<< “Object is deleted…\n”;
}
main( ){
int a,b;
cout<< “Enter two points\n”;
cin>>a>>b;
Point p(a,b);
p.display( );
}

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

C++ program to show use of Constructor, Destructor & Static


Member.
#include<iostream.h>
#include<conio.h>
class Test{
static int cnt;
public:
~Test();
Test();
Test(int, char);
void show();
};
Test::Test(){
cnt++;
cout<<cnt<<endl;
}
void Test::show(){
cout<<"Welcome";
}
Test::~Test(){
cnt--;
cout<<cnt<<endl;
}
main(){
Test t1,t2,t3,t4,t5;
t1.show();
}
}

C++ program to show use of Constructor, Copy Constructor,


Dynamic Initialization of Objects, Dynamic Object and
Destructor.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class Point{
int *x,*y;
char *nm;
public:
Point();
Point(int,int);
Point(Point &);
~Point();
void show();
};
Point::Point(){
x=new int(1);
y=new int(1);
nm=new char[6];
nm="NMIMS";
}
Point::Point(int a,int b){
x=new int (a);

Computer Programming-II Notes Prepared By: - Anurag Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

y=new int (b);


nm=new char[8];
nm="Shirpur";
}
Point::Point(Point &p){
x=new int(*p.x+8);
y=new int(*p.y);
nm=new char[7];
nm="Campus";
}
Point::~Point(){
delete x;
delete y;
delete nm;
cout<<"\nDont Change Behaviour\n ";
}
void Point::show(){
gotoxy(*x,*y);
cout<<nm;
}
main(){
clrscr();
Point *p=new Point();
Point *p1=new Point(7,1);
Point *cp= new Point(*p1);
//cp=*p1;
p->show();
p1->show();
cp->show();
delete p;
delete p1;
delete cp;
getch();
}

Computer Programming-II Notes Prepared By: - Anurag Joshi

Вам также может понравиться