HOME C,C++ HOME



tally erp9 training

PROGRAMMING IN C++

CONSTRUCTORS AND DESTRUCTORS

CONSTRUCTORS


Constructors are member functions of any class, is used for initialization (to initialize variable, to allocate memory).

Assume that each time an object of the date class is created, it is to be initialized with the date 01-01-2002. The date within the class (any class) cannot be initialized at the time of declaration. This is because the declaration of a class serves only as a template, and no memory is allocated at the time of declaration.

This problem can be solved by initialization of function i.e., called a constructor. This function (constructor) will be automatically invoked as soon as an object is created, i.e., the programmer does not have to specifically call the function to initialize the variables.

// Ex-17: PGM ILLUSTRATES THE USE OF CONSTRUCTORS

#include<conio.h>
#include<iostream.h>
class date
{
private:
int d;
int m;
int y;
public:
date() // constructor
{
d=01;
m=01;
y=2002;
}
void display()
{
cout<<endl<<d<<"-"<<m<<"-"<<y<<endl;
}
};

void main()
{
clrscr();
date obj;
obj.display();
getch();
}


In above example, as soon as an object, obj is created, the constructor will be invoked and the values d,m,y initialized to the required date 01-01-2002.

Note:
The constructor has not any parameter and does not return any value. Constructors are an exception to the general rule in C++ that all functions must specify a return type and also return a value of the specified type. Moreover, since it is to be invoked automatically. It is declared in the public section of the class.

A constructor can also take parameters. A constructor that does not take parameters is called the default constructor of a class. Apart from this, assume that when an object of the date class is created, the members are to be initialized with values specified by the user.

// Ex-18: CONSTRUCTORS – PASSING VALUES THROUGH AN OBJECT

#include<iostream.h>
#include<conio.h>
class date
{
private:
int d;
int m;
int y;
public:
date(int dd, int mm, int yy)
{
d=dd;
m=mm;
y=yy;
}
void display()
{
cout<<endl<<d<<"-"<<m<<"-"<<y<<endl;
}
};

void main()
{
clrscr();
date obj(1,1,2002);
obj.display();
getch();
}


Note:

A single class definition could include any no. of constructors, which will invoked depending on the type, number and sequence of parameters. This is a special feature of C++, called overloading, and is discussed in detail later.

DESTRUCTORS


A destructor is complementary to a constructor, is used to de-initialize the objects when they are destroyed. A destructor is automatically invoked when an object of a class goes out of scope or when the memory occupied by it is de-allocated using the delete operator. Therefore, a programmer is relieved of the task of clearing the memory space occupied by a data member every time an object goes out of scope.

Declaration of destruction has same name as its class but prefixed with a ~(tilde) symbol. A class can have only one destructor. It cannot take arguments, specify a return value, or explicitly return a value. The overloading of destructor is not possible.

// Ex-19: PGM ILLUSTRATES DESTRUCTOR

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class point
{
private:
int x,y;
public:
point(int a, int b)
{
x=a;
y=b;
}
void get()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
~point()
{
cout<<"Job is over"<<endl;
}
};

void main(void)
{
clrscr();
point p(10,20);
p.get();
// getch();
}


LIFE CYCLE OF AN OBJECT:

// Ex-20: PGM ILLUSTRATES LIFE CYCLE OF AN OBJECT

#include<iostream.h>
#include<conio.h>
class test
{
public:
test()
{
cout<<endl<<"Constructor invoked";
}

~test()
{
cout<<endl<<"Destructor invoked";
}
};

test obj1;
void main()
{
//clrscr();
cout<<endl<<"Main() begins";
test obj2;
{
cout<<endl<<"Inner block begins";
test obj3;
cout<<endl<<"Inner block ends";
}
cout<<endl<<"Main() ends";
//getch();
}

Note:
- obj1 – Global scope, it’s destructor is invoked when the program ends.
- obj2 – Function scope, it’s destructor is invoked when main() ends.
- obj3 – Block scope, it’s destructor is invoked when inner block ends.


SCOPE RESOLUTION OPERATOR:
Assume that in a huge class with a lot of member data and methods, the member functions are defined within the class definition itself. This may make the class definition messy and unreadable. The scope resolution operator (: :) separates the body of the function from the body of the class(Simply we can say as, the function declared in a class, but definition is outside the class i.e., in S.R.O).

Using the scope operator, the programmer can define a member function outside the class definition, without the function losing its connections with the class itself.

Syn:
return_type class_name :: function_name()
{
. . . . . . . . . . . .
. . . . . . . . . . . .
}

// Ex-21: PGM ILLUSTRATES THE USE OF S.R.O.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class product
{
private:
int pno,qty;
char pname[25];
float rpu;
public:
void setp(int mpno, char* mpname, int mqty, float mrpu);
void getp();
float getamt();
};

void product::setp(int mpno, char* mpname, int mqty, float mrpu)
{
pno=mpno;
strcpy(pname,mpname);
qty=mqty;
rpu=mrpu;
}

void product::getp()
{
cout<<"\n\n\t\t"<<pno<<" "<<pname<<" "<<qty<<" "<<rpu<<" ";
}

float product::getamt()
{
return(qty*rpu);
}

void main(void)
{
product ob;
clrscr();
ob.setp(25,"HAMAM",12,9.25);
ob.getp();
cout<<ob.getamt()<<endl;
getch();
}