0% found this document useful (0 votes)
31 views4 pages

Experiment 3 Object Oriented Programming (Contd.) : What Is A Constructor?

This document discusses object oriented programming concepts like constructors, class methods, and destructors. [1] Constructors are special methods that are called when an object is created. They allow assigning values to object attributes. Class methods can perform actions on a class without needing an object. Destructors are called when objects are destroyed to perform cleanup tasks. [2] Examples demonstrate defining a constructor that assigns a default size, invoking it to create an object, defining a class method to print outputs, and defining a destructor to destroy objects. [3] The exercise asks the reader to implement the classes from previous exercises using constructors, destructors, and relevant methods.

Uploaded by

Mohammad Rafae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

Experiment 3 Object Oriented Programming (Contd.) : What Is A Constructor?

This document discusses object oriented programming concepts like constructors, class methods, and destructors. [1] Constructors are special methods that are called when an object is created. They allow assigning values to object attributes. Class methods can perform actions on a class without needing an object. Destructors are called when objects are destroyed to perform cleanup tasks. [2] Examples demonstrate defining a constructor that assigns a default size, invoking it to create an object, defining a class method to print outputs, and defining a destructor to destroy objects. [3] The exercise asks the reader to implement the classes from previous exercises using constructors, destructors, and relevant methods.

Uploaded by

Mohammad Rafae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Manual

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.

An example of how to implement a constructor in a given class is given below:

Figure 1- Constructor Example

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:

Figure 2- Usage of constructor method in Main Loop

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.

Why do we need Constructors?

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

Figure 3- Class Method Definition

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

Figure 4 - Invoking 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.

The following rules must apply for effective implementation of a destructor:


1) Name should begin with tilde sign(~) and must match class name.
2) There cannot be more than one destructor in a class.
3) Unlike constructors that can have parameters, destructors do not allow any parameter.
4) They do not have any return type, just like constructors.
5) When you do not specify any destructor in a class, compiler generates a default destructor and
inserts it into your code.

SZABIST KARACHI
Lab Manual

The next image gives an example of implementing destructors in the same program

Figure 5- Destructor Definition

EXERCISE

1. Implement the previous exercise classes using constructors and destructors.Create certain
relevant methods to check functionality

SZABIST KARACHI

You might also like