Lecture-_Constructor _ Destructor by zuhaibriaz


More Info
									  PhD (CS),UCP (Lahore), Continue
◦ MS (CS), University of Lahore, (Lahore),2008
◦ BS (CS) , Gomal University, (D.Ikhan),2005
   Constructor
   Destructor
   Constructor is a special function, called whenever we instantiate an object
    of a class. If we do not define a constructor function in a class, the C++
    provides a default constructor. It is executed at the time of instantiating an
   To under majority of programming problems, which we call bugs, occur
    due to the use of uninitialized data. That is, we declare variables and use
    them without providing them any value. For example, we declare an
    integer as int i ; And it is not initialized with a value like i= 0; or i = 5;
    And then somewhere in the program, we write, say, j = 2 * i ;. This is the
    usage of an uninitialized data variable. This technique has a demerit that
    despite having no syntax error, it may cause a logical error, which is
    difficult to find. Thus, initialization of data is a very critical activity. The
    constructors are used to initialize the data members of an object.
class Date
       int month ;
       int day ;
       int year ;

         Date ( int day = 1 , int month = 1 , int year = 1 )
Date :: Date ( )
  cout<< “Default constructor
  without arguments called ” ;
Date :: Date ( int month , int day , int year )
  cout<< “A constructor with paramerters ” ;
   Compiler Generated Constructor
    If a constructor is not defined by the use the compiler
    generates it automatically. This constructor has no parameter.
    It does nothing.
    Simple Constructor
    A simple constructor can do initialization without any need to
    take any argument. So we can write a constructor of Date
    class like Date ();. When we write such a constructor, it
    automatically assumes the roll of the default-constructor. The
    compiler will not call the default constructor.
   This constructor will be automatically called when the
    required number of arguments are passed to it. Through this,
    we can easily assign the passed values to our class data
    members for that particular object.
   In our previous example of class Date, we have written a
    constructor as follows
    Date (int, int, int);
   This is a parameterized constructor which takes three
    arguments of type in
   We can provide more than one constructors by using function
    overloading. The rules for function overloading are that the
    name of the function remains the same. However, its argument
    list may be different. There are two ways to change the
    argument list. It can either vary in the number or type of the
    arguments. We cannot have two functions with the same
    number and type of arguments. In such a case, these will be
    identical. So it will not be function overloading. The function
    overloading requires the argument list to be different. The
    same concept of function overloading applies to constructors.
   The name of the destructor is the same as that of a class with a
    preceding tilde sign (~). The ~ and name of the class is written
    as a single word without any space between them. So the name
    of the destructor of class Date will be ~Date. The destructor
    can not be overloaded. This means that there will be only one
    destructor for a class.
   A destructor is automatically called when an object is
    destroyed The destructor is normally used for memory
    manipulation purposes. Suppose we have such a class that
    when we create an object of it then its constructor has
    allocated some memory. As we know that we have to free the
    allocated memory to ensure its utilization for some other
   The destructors can be summarized as the following.
    The destructors cannot be overloaded.
    The destructors take no arguments.
    The destructors don’t return a value. So they don’t
    have a return type and no return statement in the
class Date
     public :
          Date ( ) ;
          Date ( int month , int day , int year ) ;
          ~Date ( ) ;
          setMonth ( int month ) ;
          setDay ( int day ) ;
          setYear ( int year ) ;
          int getDay ( ) ;
            int getMonth ( ) ;
          int getYear ( ) ;
          setDate (int day, int month, int year ) ;
          int month , day , year ;
main ( )
  Date mydate ( 35 , 13 , 2000 ) ;
           Example 1
main ( )
  Date mydate ;
  mydate.setDate ( 21 , 01 , 1979 ) ;
  setMonth ( int month ) ;
  setDay ( int day ) ;
  setYear ( int year ) ;
  int getMonth ( ) ;
  int getDay ( ) ;
  int getYear ( ) ;

int month ;    int month ;   int month ;
int day ;      int day ;     int day ;
int year ;     int year ;    int year ;

To top