NOTE : -
THIS CONTENT IS NOT ORIGINAL
18. The CONSTRUCTOR and the DESTRUCTOR can be used to initialise and destroy an instance of a class
Very special and essential methods are the CONSTRUCTOR and DESTRUCTOR. They are automatically called whenever an instance of a class is created or destroyed (variable declaration, end of program, new, delete...).
The constructor will initialize the variables of the instance, do some calculations, allocate some memory for the instance, output some text... whatever is needed.
Here is an example of a class definition with two overloaded constructors:
It is good practice to try not to overload the constructors. It is best to declare only one constructor and give it default parameters wherever possible:
The destructor is often unnecessary. You can use it to do some calculations whenever an instance is destroyed or output some text for debugging... But if variables of the instance point to some allocated memory then the role of the destructor is essential: it must free that memory! Here is an example of such an application:
Here is a short example of an array class definition. A method that is an overload of the [] operator and outputs a reference (&) is used in order to generate an error if an attempt is made to access data outside the limits of an array:
Very special and essential methods are the CONSTRUCTOR and DESTRUCTOR. They are automatically called whenever an instance of a class is created or destroyed (variable declaration, end of program, new, delete...).
The constructor will initialize the variables of the instance, do some calculations, allocate some memory for the instance, output some text... whatever is needed.
Here is an example of a class definition with two overloaded constructors:
using namespace std;
#include
class vector
{
public:
double x;
double y;
vector () // same name as class
{
x = 0;
y = 0;
}
vector (double a, double b)
{
x = a;
y = b;
}
};
int main ()
{
vector k; // vector () is called
cout << "vector k: " << k.x << ", " << k.y << endl << endl;
vector m (45, 2); // vector (double, double) is called
cout << "vector m: " << m.x << ", " << m.y << endl << endl;
k = vector (23, 2); // vector created, copied to k, then erased
cout << "vector k: " << k.x << ", " << k.y << endl << endl;
return 0;
}
| Output |
| vector k: 0, 0 vector m: 45, 2 vector k: 23, 2 |
It is good practice to try not to overload the constructors. It is best to declare only one constructor and give it default parameters wherever possible:
using namespace std;
#include
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
};
int main ()
{
vector k;
cout << "vector k: " << k.x << ", " << k.y << endl << endl;
vector m (45, 2);
cout << "vector m: " << m.x << ", " << m.y << endl << endl;
vector p (3);
cout << "vector p: " << p.x << ", " << p.y << endl << endl;
return 0;
}
| Output |
| vector k: 0, 0 vector m: 45, 2 vector p: 3, 0 |
The destructor is often unnecessary. You can use it to do some calculations whenever an instance is destroyed or output some text for debugging... But if variables of the instance point to some allocated memory then the role of the destructor is essential: it must free that memory! Here is an example of such an application:
using namespace std;
#include
#include
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char [100]; // better than malloc!
strcpy (name, n);
age = a;
cout << "Instance initialized, 100 bytes allocated" << endl;
}
~person () // The destructor
{
delete name; // instead of free!
// delete [] name would be more
// academic but it is not vital
// here since the array contains
// no C++ sub-objects that need
// to be deleted.
cout << "Instance going to be deleted, 100 bytes freed" << endl;
}
};
int main ()
{
cout << "Hello!" << endl << endl;
person a;
cout << a.name << ", age " << a.age << endl << endl;
person b ("John");
cout << b.name << ", age " << b.age << endl << endl;
b.age = 21;
cout << b.name << ", age " << b.age << endl << endl;
person c ("Miki", 45);
cout << c.name << ", age " << c.age << endl << endl;
cout << "Bye!" << endl << endl;
return 0;
}
| Output |
| Hello! Instance initialized, 100 bytes allocated no name, age 0 Instance initialized, 100 bytes allocated John, age 0 John, age 21 Instance initialized, 100 bytes allocated Miki, age 45 Bye! Instance going to be deleted, 100 bytes freed Instance going to be deleted, 100 bytes freed Instance going to be deleted, 100 bytes freed |
Here is a short example of an array class definition. A method that is an overload of the [] operator and outputs a reference (&) is used in order to generate an error if an attempt is made to access data outside the limits of an array:
using namespace std;
#include
#include
class array
{
public:
int size;
double *data;
array (int s)
{
size = s;
data = new double [s];
}
~array ()
{
delete [] data;
}
double &operator [] (int i)
{
if (i <>= size)
{
cerr << endl << "Out of bounds" << endl;
exit (EXIT_FAILURE);
}
else return data [i];
}
};
int main ()
{
array t (5);
t[0] = 45; // OK
t[4] = t[0] + 6; // OK
cout << t[4] << endl; // OK
t[10] = 7; // error!
return 0;
}
| Output |
| 51 Out of bounds |
BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:
Post a Comment