Constructor
A member function with the same name as the class.
It initializes the data members of a class object.
Called automatically when an object of that class is created.
There can be many constructors accomplished through function overloading.
Specifying a return type or a return value for a constructor is a syntax error.
Default Constructor is a constructor with no parameters
The default constructor takes no parameters, whether you declare it or get it free from
the compiler
If you declare any constructor, the compiler will not provide a default constructor
Once the class has been defined, it can be used as a type in object, array, pointer or a
reference.
Time sunset, arrayoftimes[5], *ptrToTime,&dTime=sunset------- C++ is extensible
Data members may be private as they may be of no concern to class’ clients. Thus, implementation is hidden and this information hiding promotes program modifiability and simplifies the clients’ perception of a class.
Member functions are usually shorter than non-object oriented programs because the data stored in data members have ideally been validated by a constructor and/or by member functions that store new data. Because the data are already in the object, the member function calls often have no arguments or atleast few arguments than typical function calls in non object oriented languages. Thus, the calls are shorter, function definitions are shorter and function prototypes are shorter.
Attempting to initialize a data member of a class explicitly in a class definition is a syntax
error.
Destructor
A member function the same name as the class and with prefix ~ i.e. ~className( );
If no destructor is specified then the system ‘plugs-in’ one destructor.
The destructor does ‘termination housekeeping’ on each class object before the memory
for the object is reclaimed by the system.
Destructors cannot take arguments and hence, cannot be overloaded.
Destructors have no return value.
Declaring member functions inside a class definition (via their function prototypes) & defining those member functions outside the class definition separates the interface from of a class from its implementation. This promotes good Software Engineering as it hides implementation from class’ clients and also class’ need not recompile if implementation changes; provided interface remains same. Member functions declared outside in a class definition can be defined outside class definition using binary scope resolution operator ::. If member function is defined outside then use of class name and :: is mandatory as it ties function to a class and two classes may have functions with same name. However, scope of such functions is that of class only.