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

  1. Write a program that has a class to represent time. The class should have constructors to initialize data members hour, minute, and second to 0 and to initialize them to values passed as arguments. The class should have a member function to add time objects and return the result as a time object. There should be functions to display time in 12-hour and 24-hour format.

  2. Write a program that has a class with a dynamically allocated character array as its data member. One object should contain "Engineers are" and another should contain " Creatures of logic". Member function join() should concatenate two strings by passing two objects as arguments. Display the concatenated string through a member function. Use constructors to allocate and initialize the data member. Also, write a destructor to free the allocated memory for the character array. Make your own function for the concatenation of two strings.

  3. Write a class that can store Department ID and Department Name with constructors to initialize its members. Write destructor member in the same class and display the message "Object n goes out of the scope". Your program should be made such that it should show the order of constructor and destructor invocation.

  4. Assume that one constructor initializes data member say num_vehicle, hour, and rate. There should be a 10% discount if num_vehicle exceeds 10. Display the total charge. Use two objects and show a bit-by-bit copy of one object to another (make your own copy constructor).

  5. Write a program that illustrates the following relationship and comment on them.

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

  1. Create a class with a data member to hold a "serial number" for each object created from the class. That is, the first object created will be numbered 1, the second 2, and so on by using the basic concept of static data members. Use static member function if it is useful in the program. Otherwise, make a separate program that demonstrates the use of static member function.