Sunteți pe pagina 1din 3

C++ Programming - Constructors and Destructors

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; MyClass mcObj2(1,"abcde"); MyClass mcObj3(mcObj2); MyClass mcObj4 = mcObj3; cout << cout << cout << cout << return; "mcObj1 "mcObj2 "mcObj3 "mcObj4 : : : : " " " " << << << << mcObj1 mcObj2 mcObj3 mcObj4

// default constructor // with parameters // copy constructor // copy constructor called // by initialisation << << << << endl; endl; endl; endl;

...produces as output:
mcObj1 mcObj2 mcObj3 mcObj4 : : : : 0+ 1+ 1+ 1+ + abcde+ abcde+ 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.

S-ar putea să vă placă și