CHAPTER - 06
CONSTRUCTORS AND DESTRUCTORS
Unit I
Programming and Computational
Thinking (PCT-2)
(80 Theory + 70 Practical)
DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)
Department of Computer Science, Sainik School Amaravathinagar
Cell No: 9431453730
Praveen M Jigajinni
Prepared by
Courtesy CBSE
Class XII
INTRODUCTION - CONSTRUCTOR
CONSTRUCTOR
Constructor is a special type of function that
is called automatically whenever an object of
that class is created.
General format or syntax :
def __init__(self):
# body of the constructor
Once the object is created, you can
make sure that every variable in the object is
correctly initialised by defining
an __init__ method in your class, which
pretty much means init-iate.
CONSTRUCTOR EXAMPLE (CODE)
CONSTRUCTOR EXAMPLE (O/P)
TYPES OF CONSTRUCTOR
TYPES OF CONSTRUCTOR
We have two types of constructors in
Python.
1. default constructor – this is the one, which we
have seen in the above example. This
constructor doesn’t accept any arguments.
2. parameterized constructor – constructor with
parameters is known as parameterized
constructor.
DEFAULT CONSTRUCTOR EXAMPLE
PARAMETERIZED CONSTRUCTOR EXAMPLE (CODE)
PARAMETERIZED CONSTRUCTOR EXAMPLE (O/P)
DESTRUCTOR
DESTRUCTOR
Destructors are called when an object
gets destroyed. It’s the polar opposite of the
constructor, which gets called on creation.
Destructor can be very useful for releasing
resources before coming out of the program like
closing files, releasing memories etc
Destructor is defined using __del__() keyword
DESTRUCTOR CODE
__del__(self) destructor Method
DESTRUCTOR
Destructor method called automatically and object
is destroyed
PYTHON GARBAGE COLLECTION
Python deletes unneeded objects (built-in
types or class instances) automatically to free the
memory space.
The process by which Python periodically
reclaims blocks of memory that no longer are in
use is termed as Garbage Collection.
Python's garbage collector runs during
program execution and is triggered when an
object's reference count reaches zero.
END OF CHAPTER
SOLVIING CBSE QUESTION PAPER 2(C) 4 Marks
PROGRAMS ON CLASSES
CBSE QUESTION PAPER - 2015 DELHI
(c) Write a class PICTURE in Python with following
specifications : 4
Instance Attributes
– Pno # Numeric value
– Category # String value
– Location # Exhibition Location with
# String value
Methods :
– FixLocation() # A method to assign
# Exhibition Location as per
#Category
# as shown in the following table
CBSE QUESTION PAPER - 2015 DELHI
– Enter() # A function to allow user to
enter values
# Pno, Category and call
FixLocation() method
– SeeAll() # A function to display all the
data members
Category Location
Classic Amina
Modern Jim Plaq
Antique Ustad Khan
CBSE QUESTION PAPER - 2015 DELHI
CBSE QUESTION PAPER - 2015 OUTSIDE DELHI
(c) Write a class PHOTO in Python with following
specifications : 4
Instance Attributes
– Pno # Numeric value
– Category # String Value
– Exhibit # Exhibition Gallery with String value
Methods:
– FixExhibit() #A method to assign
#Exhibition Gallery as per
#Category
#as shown in the following table
CBSE QUESTION PAPER - 2015 DELHI
CBSE QUESTION PAPER - 2015 DELHI
Category Exhibit
Antique Zaveri
Modern Johnsen
Classic Terenida
-Register() #A function to allow user to
enter values #Pno, Category
and call FixExhibit() method
– ViewAll() #A function to display all the
data members
CBSE QUESTION PAPER - 2016 DELHI
CBSE QUESTION PAPER - 2016 DELHI
(c) Write a class CITY in Python with following
specifications : 4
Instance Attributes
– Code # Numeric value
– Name # String value
– Pop # Numeric value for Population
– KM # Numeric value
– Density # Numeric value for Population
Density than 12000.
CBSE QUESTION PAPER - 2016 DELHI
Methods:
– CalDen() # Method to calculate Density
as Pop/KM
– Record() # Method to allow user to
enter values Code, Name, Pop,
KM and call CalDen() method
– See() # Method to display all the
data members also display a
message “Highly Populated
Area” if the Density is more
than 12000.
CLASS TEST
1. What is Constructor? Explain
2. What are the types of constructors?
3. What is destructors? Explain
4. Write a program to illustrate constructors
5. Write a program to illustrate distructors
Class : XII Time: 40 Min
Topic: Const & distruct Max Marks: 20
Each Question carries 4 marks
Thank You

Chapter 06 constructors and destructors

  • 1.
  • 2.
    Unit I Programming andComputational Thinking (PCT-2) (80 Theory + 70 Practical) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE Class XII
  • 3.
  • 4.
    CONSTRUCTOR Constructor is aspecial type of function that is called automatically whenever an object of that class is created. General format or syntax : def __init__(self): # body of the constructor Once the object is created, you can make sure that every variable in the object is correctly initialised by defining an __init__ method in your class, which pretty much means init-iate.
  • 5.
  • 6.
  • 7.
  • 8.
    TYPES OF CONSTRUCTOR Wehave two types of constructors in Python. 1. default constructor – this is the one, which we have seen in the above example. This constructor doesn’t accept any arguments. 2. parameterized constructor – constructor with parameters is known as parameterized constructor.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    DESTRUCTOR Destructors are calledwhen an object gets destroyed. It’s the polar opposite of the constructor, which gets called on creation. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc Destructor is defined using __del__() keyword
  • 14.
  • 15.
    DESTRUCTOR Destructor method calledautomatically and object is destroyed
  • 16.
    PYTHON GARBAGE COLLECTION Pythondeletes unneeded objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed as Garbage Collection. Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero.
  • 17.
  • 18.
    SOLVIING CBSE QUESTIONPAPER 2(C) 4 Marks PROGRAMS ON CLASSES
  • 19.
    CBSE QUESTION PAPER- 2015 DELHI
  • 20.
    (c) Write aclass PICTURE in Python with following specifications : 4 Instance Attributes – Pno # Numeric value – Category # String value – Location # Exhibition Location with # String value Methods : – FixLocation() # A method to assign # Exhibition Location as per #Category # as shown in the following table CBSE QUESTION PAPER - 2015 DELHI
  • 21.
    – Enter() #A function to allow user to enter values # Pno, Category and call FixLocation() method – SeeAll() # A function to display all the data members Category Location Classic Amina Modern Jim Plaq Antique Ustad Khan CBSE QUESTION PAPER - 2015 DELHI
  • 22.
    CBSE QUESTION PAPER- 2015 OUTSIDE DELHI
  • 23.
    (c) Write aclass PHOTO in Python with following specifications : 4 Instance Attributes – Pno # Numeric value – Category # String Value – Exhibit # Exhibition Gallery with String value Methods: – FixExhibit() #A method to assign #Exhibition Gallery as per #Category #as shown in the following table CBSE QUESTION PAPER - 2015 DELHI
  • 24.
    CBSE QUESTION PAPER- 2015 DELHI Category Exhibit Antique Zaveri Modern Johnsen Classic Terenida -Register() #A function to allow user to enter values #Pno, Category and call FixExhibit() method – ViewAll() #A function to display all the data members
  • 25.
    CBSE QUESTION PAPER- 2016 DELHI
  • 26.
    CBSE QUESTION PAPER- 2016 DELHI (c) Write a class CITY in Python with following specifications : 4 Instance Attributes – Code # Numeric value – Name # String value – Pop # Numeric value for Population – KM # Numeric value – Density # Numeric value for Population Density than 12000.
  • 27.
    CBSE QUESTION PAPER- 2016 DELHI Methods: – CalDen() # Method to calculate Density as Pop/KM – Record() # Method to allow user to enter values Code, Name, Pop, KM and call CalDen() method – See() # Method to display all the data members also display a message “Highly Populated Area” if the Density is more than 12000.
  • 28.
    CLASS TEST 1. Whatis Constructor? Explain 2. What are the types of constructors? 3. What is destructors? Explain 4. Write a program to illustrate constructors 5. Write a program to illustrate distructors Class : XII Time: 40 Min Topic: Const & distruct Max Marks: 20 Each Question carries 4 marks
  • 29.