Experiment 3 Object Oriented Programming (Contd.) : What Is A Constructor?
Experiment 3 Object Oriented Programming (Contd.) : What Is A Constructor?
Experiment 3
Object Oriented Programming(Contd.)
OBJECTIVE
Introduction to Constructors
Introduction to Class Methods.
Introduction to Destructors.
THEORY
What is a constructor? :
A constructor is a special method or function that is invoked each time an object of a specific class
is created. This means that everytime a new instance of that particular class is created , the
constructor(or the construction method) is going to be called. To correctly implement constructors ,
Two conditions or rules must apply. The two rules are given below:
1. The constructor method will always have the same name as the name of the class.
2. Constructor Method does not have a return type.
The above shown cosntructor relates the new inputs to the already available attributes of the class.
The size variable is given a default value of 5. This is only the definition of a user-defined constructor
and not the calling of it in main program.Kindly note as the constructor is mentioned after the access
modifier of public, the constructor is available to the rest of the program. Please also note that the
variable name of the constructor inputs and the class attributes must always be different for correct
assignment of values.
SZABIST KARACHI
Lab Manual
The use or calling of the constructor method is given in the diagram below:
The above diagram shows the invoking of constructor method in the main program. Please note that
as the size of the fruit was given by default(=5). This is the reason that the constructor is not given a
value of size. One more value is missing that is of the list of other types but this can be resolved by
taking in values from the user.
Constrcutors become more useful and convenient in heavy-duty programming applications where
multiple objects of a given class are to be created. Constructors provide us with an easy way to create
objects and assign values to their attributes simultaneously.
Class Methods
Class methods are basically descriptions of behaviour of a class and help in reusing the code without
repeating it. These are similar to constructors but can have different names and can serve different
purposes. As with the example, It is evident that getting to print(or show) all the outputs on the console
is taking a great deal of effort with a continuous repeating of the code. This can be simplified by
creating a single class method that can show the output on the console and calling it with respect to
any object that has been created.
An example of defining the class method is given in the next image.
SZABIST KARACHI
Lab Manual
The class method definition is given in the previous image shows that no object name is mention or
given.Only class attributes are addressed for performing the given function. The way of programming
ensures that multiple methods can be called with utmost of ease. The next image below shows how
easy it is to invoke a class method
Please note that the class method is invoked in the main program and is called using the object
name.
What is a destructor ?
A destructor is a special member function that works just opposite to constructor, unlike constructors
that are used for initializing an object, destructors destroy (or delete) the object.
A destructor gets automatically called when the program is finished executing or when the delete
operator is called in the program.
SZABIST KARACHI
Lab Manual
The next image gives an example of implementing destructors in the same program
EXERCISE
1. Implement the previous exercise classes using constructors and destructors.Create certain
relevant methods to check functionality
SZABIST KARACHI