HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
C++ PROGRAMMING - CONSTRUCTORS AND DESTRUCTORS

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Constructors and destructors are special member functions which are automatically called when an object is created and destroyed respectively.


TUTORIAL TAKEN FROM COURSE : C++ PROGRAMMING

FULL COURSE DETAILS

Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more popular. This course introduces object oriented design and covers the whole syntax of C++ with lots of examples. The aim is to give thorough hands on experience to a small group so that the maximum benefit is obtained by each reader.

TO ACCESS THE FULL COURSE AND HUNDREDS OF OTHERS, CLICK HERE.


While any code may appear in the constructor and destructor it is recommended that code relating to initialising data members in the class be implemented in the constructor. A destructor will perform destroy any dynamic memory that was allocated in the constructor or at some other point in the object life, as well as releasing any system resources that might similarly have been assigned to the object during its life.

Constructors always have the same name as their class. Destructors take the name of their class preceded by a tilde (~).

A class may have more than one constructor.

A class may not have two constructors that agree in terms of the number and type of their parameters.

Constructor and destructors are usually defined as public members of their class and may never possess a return value. They may however sometimes be protected so that code outside the class heirarchy can not construct objects of that class.

Example:

#include <string.h>
#include <iostream.h>
class MyClass
{
private:
 int myInt;
 char myText[20];
//
public: // destructors must be public member functions

MyClass() // constructor with no parameters - the 'default'
{
   myInt = 0;
   myText[0]='\0'; // null string
}

MyClass(const MyClass &anObjectOfMyClass) // 'copy' constructor
  {
  myInt = anObjectOfMyClass.myInt;
  myText = anObjectOfMyClass.myText;
  }

/* if assignment were defined for this class then the 
body of the copy constructor could be defined as follows:
{ *this = anObjectOfMyClass;}
*/
MyClass(const int thisint, const char* thattext = 0)
    :myInt(thisint)
 {
   strcpy(myText,thattext);
 }

~MyClass() // destructor: NB no parameters
 {   // destructor code
 }

// a friend for output:
friend ostream& operator <<(ostream&, MyClass &);
}; // end of class declaration

ostream& operator <<(ostream & os,MyClass &anObjectOfMyClass)
{
 os << anObjectOfMyClass.myInt << "+ " << anObjectOfMyClass.myText
<< "+";
 return os;
}

void main(void)
{
 MyClass mcObj1;          // default constructor
 MyClass mcObj2(1,"abcde");    // with parameters
 MyClass mcObj3(mcObj2);      // copy constructor
 MyClass mcObj4 = mcObj3;     // copy constructor called
                // by initialisation

 cout << "mcObj1 : " << mcObj1 << endl;
 cout << "mcObj2 : " << mcObj2 << endl;
 cout << "mcObj3 : " << mcObj3 << endl;
 cout << "mcObj4 : " << mcObj4 << endl;
 return;
}

...produces as output:

mcObj1 : 0+ +
mcObj2 : 1+ abcde+
mcObj3 : 1+ abcde+
mcObj4 : 1+ abcde+

Naturally the functions can be declared in the class body, and defined elsewhere as usual. For example:

MyClass::MyClass()
{
 // etc.
}
MyClass::~MyClass()

 //
}

A constructor allocates space for an object and ensures that it is initialised correctly.

Default Behaviour

If a class is straightforward, then there may be no need to worry about constructors and a destructor at all.

If no default constructor is defined then the action on creation is to allocate store for member objects; if default constructors are defined for these member objects then these constructors will in turn be called and the member objects will be initialised. If the member objects are of primitive data types then initialisation will be contingent upon their storage class.

The function of the default copy constructor is simply to make bitwise copies of all member data. This is correct if all the data relevant to the class is held within it, but is possibly useless and certainly dangerous if references and pointers are involved.

If the copying involves the copying of an address, then two objects will appear to have two different data members with the same ultimate (de-referenced) value, whereas in reality they are both pointing at the same store: changes made through one will be reflected in the other and vice versa.

Should one decide to delete the data concerned, then the other will be left with a pointer to nowhere (a ' dangling ' pointer). Strategies exist to cope with this, involving keeping track of the number of pointers to an object, but usually involve overloading the reference operators and are not for the faint-hearted. In general, it is better to avoid this situation altogether.

Continued...


NEXT PAGE



5 RELATED COURSES AVAILABLE
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
MICROSOFT VISUAL C++ 5 AND THE MFC&T 32 BIT WINDOWS PROGRAMMING
A complete 32 bit programming course highlighting the main features regarding the Microsoft Visual C++ programmin....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
C PROGRAMMING IN THE UNIX ENVIRONMENT
This course will provide readers the knowledge to use many of the UNIX 'C' library facilities, interface with the....
MICROSOFT VISUAL BASIC V6 INTRODUCTION
To go from the fundamentals of Visual Basic programming to the threshold of Advanced level. Gaining in depth prog....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Friday 21st November 2008  © COPYRIGHT 2008 - VISUALSOFT