| Constructor | Destructor |
| (1) Name is same as class | (1) Name is same as class but ~ in front. |
| (2) It does not have return type. | (2) It does not have return type. |
| (3) It can have parameters, also can specify default for parameter | (3) It does not have any parameter. |
| (4) There is only one constructor for a class, but can be overloaded. | (4) There is only one destructor per class. |
| (5) It initializes objects | (5) It is to release memory allocated to objects on a heap. |
|
|
the object itself | |
|
|
the data value of member1 | (*this).member1 |
|
|
the pointer of member2 | (*this).member2 |
Note: The ->
operator is a shortcut of dot operator with a pointer
The * operator has a lower precedence than the dot operator..
For example,
#include <iostream.h>
class square
{
private:
float side;
public:
square(float s)
{
side = s;
}
float area(void)
{
return side * side;
}
};
void main()
{
square Pool(4.0), *PrtPool;
PrtPool = &Pool;
cout<< "The area is " << (*PrtPool).area()
<< endl;
cout<< "The area is " << PrtPool->area()
<< endl;
}