Skip to content

OBJECT ORIENTED PROGRAMMING

Class and Objects:

class acts as blueprint that determines the properties and abilities of each object declared out of it may have.

class can be defined using ‘class’ keyword.

each class definition can have variables and function definitions.

These are called as members of the class.

 

object can be declared from the class using the following syntax:
<identifier>=className()

 

Each Object have one copy of all the members of its class.

object can access its copy of members using dot(.) operator.

🐍
filename.py
class sample:
    x=10
    y=2.34
    L1=[1,2,3]   
    
obj1=sample()
obj2=sample()

print(obj1.x)
print(obj1.y)
print(obj1.L1)
print()
obj2.x=34
obj2.y=67.89
print(obj2.x)
print(obj2.y)
print(obj2.L1)

Functions can also be members of class

🐍
filename.py
#Example class definition
class sample:
    x=10	#data member
    y=20
    def test(): #member function
        print("x=",x)
        print("y=",y)
        return

Class is an user defined datatype using which variables can be declared later.

variable declared out of class type is called as ‘object’.

 

Self parameter:
Each member function must have a special positional parameter known as ‘self’ which is used to store the reference of current object that is invoking the function.

The word ‘self’ is not compulsory, we can use any other valid identifier.

 

the declaration process of object from class type is called as instantiation.
Each object is called as instance of the class.

🐍
filename.py
#Example class definition
class student:
    age=0	#data member
    marks=0.0
    def disp(self): #member function
        print("Age=",self.age)
        print("Marks=",self.marks)
        return

#mainprogram
#declaration of object from student class
phani=student()

phani.age=20
phani.marks=99.7

phani.disp()
Practice Questions:
1. Write python code to define a class with name ‘student’ with the following members:
  variables:
      name
      age
      course
  functions:
      show() # to access values of data members
 
declare two objects from the class ‘student’ with identifiers ‘obj1’ and ‘obj2’.  initialise them with appropriate values and display their data.
 
 
2. Re-Write the above class definition to have two member functions like below:
setData(n,a,c) # to initalise the object with desired values
getData() # to display values of the object
 
 
3. Re-Write the setData() function in the above class to accept user input for the class members.
 
 
4. Write python code to define a class with name ‘car’ with the following members:
variables:
     model
     make
     bodytype
functions:
     show() # to access values of data members
 
declare two objects from the class ‘car’ with names ‘Baleno’ and ‘Tiago’.  initialise them with appropriate values and display their data.

Every OOP language must support the following features:
Data Hiding
Encapsulation
abstraction
Polymorphism
Inheritance

 

Example program to get user input for the data members of class using member function

🐍
filename.py
#Definition of class
class Student:
    name="None"
    age=0
    marks=0
    
    def getData(self):
        self.name=input("Enter your name:")
        self.age=int(input("Enter your age:"))
        self.marks=int(input("Enter you marks:"))
        
    def show(self):
        print(self)
        print("Name =",self.name)
        print("Age =",self.age)
        print("Marks =",self.marks)
        return

#main program
anil=Student()
anil.getData()
print("Details of Anil")
anil.show()

mounica=Student()
mounica.getData()
print()
print("Details of Mounica")
mounica.show()