QUOTE(Shrikharan @ 2 Jun, 2008 - 06:43 AM)

Why cannot the normal member functions do the same task? What is the difference of data members being initialized by constructors and member functions? I find it difficult to see the difference. Can anyone help in enlightening me as I am sure I am missing something?
You can, and it's often recommended in a sense... In cases where one has multiple constructors, it is often easier to create an initialization method, which can be called from each constructor, which reduces code repetition. However, for members variables that are of class-types, one has to use a constructor to initialize them as well:
cpp
class Tail
{
public:
enum TailType { Short, Long };
Tail(TailType t) { .... }
};
class Cat
{
public:
Cat() : tail(Tail::Short)
{
// Do nothing
}
private:
Tail tail;
};
You can't call tail's constructor explicitly (at least in an obvious way) outside of a constructor.
There are a few properties of constructors that make them rather desirable:
1.) Constructors are automatically called when they need to be. One needs not rely on one's tendency to remember to do something, which is almost always a good thing.
2.) Explicitly defined copy-constructors for classes that require deep copying (i.e. those that hold on to pointers or sometimes in the case of handles) usually makes life easier for the programmer. Instead of having to duplicate by calling a method, or passing by reference (which at times is not desireable), a copy-constructor (a specialized method that the compiler recognizes) is called to actually do the work.
For example, I have class X { public: int* y; };
X v1;
v1.y = new int;
X v2;
// This copies the pointer value, not the value pointed to by member X::y
v1 = v2;
3.) RAII (resource acquisition is initialization) isn't nearly as foolproof without them. Don't worry if you've never seen this terminology... it tends to come later for most.