Upload
kranthi633
View
214
Download
0
Embed Size (px)
Citation preview
8/22/2019 09 C++ Constructors and Destructors
1/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. Adestructor 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 maynever 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
#include
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;}
8/22/2019 09 C++ Constructors and Destructors
2/3
*/
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
8/22/2019 09 C++ Constructors and Destructors
3/3
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 adestructor 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 themember 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 bereflected in the other and vice versa.
Should one decide to delete the data concerned, then the other will be left with a pointerto 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.
http://counter.fateback.com/