The two Forms of ‘new’ and ‘delete’

3 07 2011

There are two forms of new and delete in C++. They are different depending on whether you are creating a single object or an array of objects. When creating a single object you use this form :

string *strPtr = new string;
delete strPtr;

For arrays you use the square bracket form of new and delete:

string *strPtr = new string[100];
delete [] strPtr;

You cannot mix and match these operations, using the single form of new with the array form of delete or vice versa is undefined.





What Qualifies as a Default Constructor?

2 07 2011

In C++ a default constructor is one that can be called with no arguments. This does not mean a default constructor must have no arguments, just that it can be called with no arguments. So an example of a default constructor is one with no arguments but another is one for which all arguments have been given default values.

class Book{
 public:
   Book();
}; 
class Book{
 public:
   Book(int copies = 0, int edition = 0);
};