Lab Sheet 4
Lab Sheet 4
Additional Components of a Class
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.
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.
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.
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.
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:
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.
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.
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
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.
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
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.
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.
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.
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).
Write a program that illustrates the following relationship and comment on them.
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.