OBJECT ORIENTED CONCEPT
• UNIT 3: STRUCTUTRE
• Class & Object
• Inheritance
• Polymorphism
• Method Overloading
• Method Overriding
Class & Object:
• Classes are a way of aggregating similar data and functions. A
class is a scope inside which various code (especially function
definations) is executed, and the locals to this scope become
attributes of the class, and any objects constructed by this class.
An object constructed by a class is called an instance of that class.
Class & Object:
Defining a Class
To define a class, use the following format:
class ClassName:
"Here is an explanation about your class"
pass
The capitalization in this class definition is the convention but is not required by the
language. The pass statement in the code above is just to say to the python
interpreter just go on and do nothing. You can remove it as soon as you are adding
your first statement
Create a class
To create a new class definition in python, use the class statement.
Here is the general form to define a class in python:
class class_name:
'class doc string'
class_suite
Here, the class has a documentation string (docstring) that can be accessed
via the class_name._doc_, and the class_suite consists of all the component
statements defining the class members, data attributes, and functions.
Create a class
Let's take a look at this example, defining a class, STUDENT, in python:
class STUDENT:
'Common base class for all employees'
totalStudent = 0
def __init__(self, name, fee):
self.name = name
self.fee = fee
STUDENT.totalStudent = STUDENT.totalStudent + 1 def
displayCount(self):
print("Total Student = %d" % STUDENT.totalStudent)
def displayStudent(self):
print("Name:", self.name, ", Fee:", self.fee)
Create Instance Objects in Python
To create an instance of a class, you simply call the class using the class
name and then pass in whatever arguments its _init_ method accepts.
Here is an example.
stud1=STUDENT ("Devraj",2500)#created first object of STUDENT
class
stud2=STUDENT("Alok",2150)#created second object of STUDENT
class
stud3=STUDENT("Satyam",3200)#created third object of
STUDENT class
Access Attributes in Python
You can access the attributes of the object just by using the dot (.)
operator with object. Class variable can be access by using the class
name. Here is an example.
stud1.displayStudent()
stud2.displayStudent()
stud3.displayStudent()
print("Total Student = %d" % STUDENT.totalStudent)
My Object Oriented.pptx

My Object Oriented.pptx

  • 1.
    OBJECT ORIENTED CONCEPT •UNIT 3: STRUCTUTRE • Class & Object • Inheritance • Polymorphism • Method Overloading • Method Overriding
  • 2.
    Class & Object: •Classes are a way of aggregating similar data and functions. A class is a scope inside which various code (especially function definations) is executed, and the locals to this scope become attributes of the class, and any objects constructed by this class. An object constructed by a class is called an instance of that class.
  • 3.
    Class & Object: Defininga Class To define a class, use the following format: class ClassName: "Here is an explanation about your class" pass The capitalization in this class definition is the convention but is not required by the language. The pass statement in the code above is just to say to the python interpreter just go on and do nothing. You can remove it as soon as you are adding your first statement
  • 4.
    Create a class Tocreate a new class definition in python, use the class statement. Here is the general form to define a class in python: class class_name: 'class doc string' class_suite Here, the class has a documentation string (docstring) that can be accessed via the class_name._doc_, and the class_suite consists of all the component statements defining the class members, data attributes, and functions.
  • 5.
    Create a class Let'stake a look at this example, defining a class, STUDENT, in python: class STUDENT: 'Common base class for all employees' totalStudent = 0 def __init__(self, name, fee): self.name = name self.fee = fee STUDENT.totalStudent = STUDENT.totalStudent + 1 def displayCount(self): print("Total Student = %d" % STUDENT.totalStudent) def displayStudent(self): print("Name:", self.name, ", Fee:", self.fee)
  • 6.
    Create Instance Objectsin Python To create an instance of a class, you simply call the class using the class name and then pass in whatever arguments its _init_ method accepts. Here is an example. stud1=STUDENT ("Devraj",2500)#created first object of STUDENT class stud2=STUDENT("Alok",2150)#created second object of STUDENT class stud3=STUDENT("Satyam",3200)#created third object of STUDENT class
  • 7.
    Access Attributes inPython You can access the attributes of the object just by using the dot (.) operator with object. Class variable can be access by using the class name. Here is an example. stud1.displayStudent() stud2.displayStudent() stud3.displayStudent() print("Total Student = %d" % STUDENT.totalStudent)