Unit 6
Unit 6
Unit 6
CCs 1500
Objectives
❖ Classify the roles of member functions
❖ Identify constructors
❖ Write constructors both with and without parameters
❖ Overload constructors
❖ Utilize destructors
❖ Compare composition and aggregation
❖ Create a program using constructors, destructors, and
some user-defined methods
Classifying the Roles of
Member Functions
3
❖ Inspector functions or access functions
− return information about an object’s state
− display some or all of an objects attributes
▪ getname(), displayValues()
− Predicate functions (subcategory)
▪ isDigit()
❖ Manager functions
− create and destroy objects for you
− called constructors and destructors
Understanding
Constructors
6
❖ Constructor
− a special kind of function that is called automatically
each time an object is created
Two Types:
❖ Default constructor
− no arguments or does not accept any arguments
❖ Non-default constructor (parameterized)
− at least one argument
Default
❖ an object cannot be created if the program does not have
a constructor
− if no constructors declared, Python provides a default
Parameterized
Overloading Constructors
11
❖ Constructor overloading
− more than one constructor is present in a class with the
same name but different arguments
− not supported by Python
Class Method Decorator
❖ @classmethod
− used to declare a method in the class as a class method
that can be called using ClassName.methodName()
<Classname>.<methodname>
Using Destructors
19
❖ destructor
− called when an object is destroyed
− not that needed by Python because it has a garbage
collector for memory management, compared to C++
❖ __del__()
− destructor method
− called when all references to the object have been
deleted, that is when an object is garbage collected
Advantages of using destructor
❖ Automatic cleanup
− provides automatic cleanup of resources used by an
object which is no longer needed
− useful in cases where resources are limited, or where
failure to clean up can lead to some issues
❖ Consistent behavior
− ensures that an object is properly cleaned up, regardless
of how it is used or when it is destroyed
− ensures consistent behavior and can help to prevent
bugs and other issues
❖ Easy to use
− easy to implement in Python, and can be defined using
the __del__() method
Advantages of using destructor …
❖ Supports object-oriented programming
− an important feature of OOP, and can be used to enforce
encapsulation and other principles of object-oriented
design
❖ Helps with debugging
− can be used to trace the lifecycle of an object and
determine when it is being destroyed
Understanding
Composition and
Aggregation
24
Association
− defines the relationship between objects
▪ one-to-one, one-to-many, many-to-one, many-to-
many
• one employee to multiple projects, one project to
multiple employees
❖ Inheritance
− a mechanism that allows us to take all of the properties
of another class and apply them to our own
▪ parent class – one from which the attributes and
functions are derived
• base class
− Is-A Relation
class Parent:
# Constructor
# Variables of Parent class
# Methods
...
class Child(Parent):
# constructor of child class
# variables of child class
# methods of child class
...
Composition
− using an object with another object
− two entities are extremely reliant on one another
− can call the contained objects member-objects
− “has-a” relationship
▪ a Transaction has an InventoryItem
− if the parent object is deleted, the child object also loses
its status
▪ a car has an engine, if the car is destroyed, the engine
is also destroyed
Aggregation
− direct relation between objects
▪ Employee and Department
• one Employee to one Department
• one Department can have more than one Employee
− “has-a” relationship
− unidirectional association
▪ department can have students but vice versa is not
possible
− both the entries can survive individually which means
ending one entity will not affect the other entity
Why use Aggregation over Composition?
❖ Composition
▪ if the emp object is deleted then the object of the Salary
class which was initialized inside the EmployeeOne class
will be deleted automatically because it is completely
dependent on the object of the EmployeeOne class, which
might cause some error in the output
❖ Aggregation
▪ completely two different objects were created, emp and
salary, and passed the salary object as a parameter to
the EmployeeOne class, so even if the emp object is
deleted, the object of the Salary class will remain the
same and can be used elsewhere
Why use Aggregation over Composition? …