Lab Sheet 4

Lab Sheet 4

Additional Components of a Class


Constructor

A constructor is basically a member function of a class used for startup tasks such as initialization of data members, allocation of memory, and so on. It is convenient if an object can initialize itself when it is first created, without the need to make a separate call to a member function. This member function has the same name as its class. A constructor is called when the object is created statically or dynamically with the new operator. A constructor does not return a value so it does not have a return type. Since the constructor is called automatically by the system there is no reason for it to return anything. The compiler knows that they are constructors by looking at their names. Let us take an example that each time an object of the date class is created, it is to be initialized with some specific value. 

class date

{

private:

     int dd,yy;

     char mm[4];

public:

     date()   //default constructor

     {    

          dd=27;

          strcpy(mm,"Jun");

          yy=2021;

     }

     showdate()  //display

     {

          cout<<dd<<" "<<mm<<" "<<yy<<endl;

     }

};

int main()

{

     date d1;  //constructor invoked

     d1.showdate();

     return 0;

}

Here when the object d1 is being created, the constructor will be involved and the data members dd, mm, and yy initialized. Note that the constructor here neither takes any parameters. So it is called a default constructor. Data members of an object can be initialized by passing arguments to the constructor when the object is created.  The constructor that takes arguments is called a parameterized constructor. The following example adds parameterized constructor in the above example program.

class date

{

     //...

public:

     //...

     date(int d, char m[], int y) //parameterized constructor

     {    

          dd=d;

          strcpy(mm,m);

          yy=y;

     }

     //....

};

int main()

{

     date d1;  //default constructor invoked

     date d2(10,"Jul",2020)

     d1.showdate();

     d2.showdate();

     return 0;

}

 

Copy Constructor

We already know that no argument constructor can initialize data members to some specific values, and parameterized constructor can initialize data members to values passed as arguments. There is also another way to initialize an object with another object of the same type. It is called the copy constructor. It is a one-argument constructor whose argument is an object of the same class. The date class in above example can also be used as follows.


int main()

{

     date d1(16,"Sep",2015);

     date d2(d1);   //copy constructor called

     date d3=d1;    //another way of invoking copy constructor

     d1.showdate();

     d2.showdate();

     d3.showdate();

     return 0;

}

The compiler implicitly generates a default copy constructor which copies all the members of the objects one by one to another object. We can override the default copy constructor by making our own copy constructor as follows.

class date

{

     //...

public:

     //...

     date(data &dt) //copy constructor

     {    

          dd=dt.dd;

          strcpy(mm,dt.mm);

          yy=dt.yy;

          cout<<"Copy constructor called"<<endl;

     }

     //....

};


Destructor

Constructors serve to automatically initialize data members of an object at the point of creation. Destructors are complimentary to constructors. They serve to clean up or do the final finishing task of objects when they are destroyed.

A destructor may be called either when the object goes out of scope or when it is destroyed explicitly using the delete operator. A destructor, like a constructor, has the same name as that of the class but is prefixed by the tilde ('~') symbol. A class cannot have more than one destructor. And destructor can't take arguments or specify a return value. The most common use of the destructor is to de-allocate memory that was allocated for the object by the constructor. A destroctor for above program is can be created as:

 class date

{

     //...

public:

     //...

     ~date() //destructor

     {    

          cout<<"Destructor called"<<endl;

     }

     //....

};


Constant Member Function

A function is made a constant function by placing the keyword const after the function header before the function body. A member function that does not change the value of its object but acquire data from their object is an obvious candidate for constant function. For example.

return_type func_name(argument list) const   //const function 

{

    //Function body;

}

 

Constant Object

When an object is declared as constant, we can't modify it. A constant object can only call its constant member functions because they are the only ones that guarantee not to modify its value.

General syntax:

const class_name object_name;    //creation of constant object

 

Constant Reference Argument

When we don't want to modify the arguments passed to the function but do not want to create objects/variables for the parameters, the constant reference parameters are made. For example

return_type func_name(const int &a, float &b)   //const ref arguments arguments

{

    //function body; 

    //a cannot be changed here but b can be.

}

 

Static Data Members

If a data item in class is declared as static, then only one copy of that item is created for the entire class, no matter how many objects there are. All the objects share a common item of information. The static data are to be defined separately and they are not allocated during object creation. 

General syntax:

class class_name

{

    //......

    static data_type variable_name;   //declaration of static data member

    //.....

};

data_type class_name::variable_name;  //definition of static data member 

//be assign to variable.

                                                                                        

Static Member Function

A static member function can have access to only other static members declared in the same class and it can be called using the class name

 General Syntax:

static return_type func_name (argument list);  //declaration of static member function

//....

class_name::fun_name(argument passed) //static member function call

 

Exercises

i) const_object.non_const_mem_function

ii) const_object.const_mem_function

iii) non_const_object.non_const_mem_function

iv) non_const_object.const_mem_function