II [Link].
CS ‘C’
OOPS Programs in Python
Note:
While writing the programs indentation is very important.
Indentation refers to the spaces at the beginning of a code line (every line).
All the programs are executed very well.
Write in your note book without any mistakes.
1. Instance of that class
class MyClass(object):
var = 9
# Create first instance of MyClass
this_obj = MyClass()
print(this_obj.var)
# Another instance of MyClass
that_obj = MyClass()
print (that_obj.var)
Output
9
9
----------------------------------------------------
2. Instance and Methods
class MyClass(object):
var=9
def firstM(self):
print("hello, World")
obj = MyClass()
print([Link])
[Link]()
Output
9
hello, World
----------------------------------------------------
3. Class and Objects
class Parrot:
# class attribute
name = ""
age = 0
# create parrot1 object
parrot1 = Parrot()
II [Link]. CS ‘C’
[Link] = "Blu"
[Link] = 10
# create another object parrot2
parrot2 = Parrot()
[Link] = "Woo"
[Link] = 15
# access attributes
print(f"{[Link]} is {[Link]} years old")
print(f"{[Link]} is {[Link]} years old")
Output
Blu is 10 years old
Woo is 15 years old
---------------------------------------------------------------------
4. Function, Instance and Class
class employee():
def __init__(self,name,age,id,salary): #creating a function
[Link] = name # self is an instance of a class
[Link] = age
[Link] = salary
[Link] = id
emp1 = employee("harshit",22,1000,1234) #creating objects
emp2 = employee("arjun",23,2000,2234)
print(emp1.__dict__)
print(emp2.__dict__)#Prints dictionary
Output
{'name': 'harshit', 'age': 22, 'salary': 1234, 'id': 1000}
{'name': 'arjun', 'age': 23, 'salary': 2234, 'id': 2000}
---------------------------------------------------------------------
5. Class Method with self as argument
class MyClass(object):
def firstM(self):
print("hello, World")
print(self)
obj = MyClass()
[Link]()
print(obj)
Output
hello, World
<__main__.MyClass object at 0x036A8E10>
<__main__.MyClass object at 0x036A8E10>
--------------------------------------------------------------------------
II [Link]. CS ‘C’
6. Encapsulation
class MyClass(object):
def setAge(self, num):
[Link] = num
def getAge(self):
return [Link]
zack = MyClass()
[Link](45)
print([Link]())
[Link]("Fourty Five")
print([Link]())
Output
45
Fourty Five
-------------------------------------------------------------
7. Constructor
class Person:
def __init__( self, s ):
[Link] = s
def hello( self ):
print ('Hello', [Link])
t = Person("John")
[Link]()
Output
Hello John
-----------------------------------------------------------------
8. Inheritance
8.1.1 Single Inheritance
class A:
def add(self,x,y):
self.x=x
self.y=y
print("The addition is:",self.x+self.y)
class B(A): #Single Inheritance
def sub(self,x,y):
self.x=x
self.y=y
II [Link]. CS ‘C’
print("The subtraction is:",self.x-self.y)
#read data into a and b
a=int(input("Enter a value:"))
b=int(input("Enter b value:"))
#create object from derived object
ob=B()
[Link](a,b)
[Link](a,b)
Output
Enter a value:10
Enter b value:7
The addition is: 17
The subtraction is: 3
-----------------------------------------------------------------
8.1.2 Single Inheritance
# base class
class Animal:
def eat(self):
print( "I can eat!")
def sleep(self):
print("I can sleep!")
# derived class
class Dog(Animal):
def bark(self):
print("I can bark! Woof woof!!")
# Create object of the Dog class
dog1 = Dog()
# Calling members of the base class
[Link]()
[Link]()
# Calling member of the derived class
[Link]();
II [Link]. CS ‘C’
Output
I can eat!
I can sleep!
I can bark! Woof woof!!
---------------------------------------------------------------------
8.2 Multi-Level Inheritance
class A:
def add(self,x,y):
self.x=x
self.y=y
print("The addition is:",self.x+self.y)
class B(A): #Single Inheritance
def sub(self,x,y):
self.x=x
self.y=y
print("The subtraction is:",self.x-self.y)
class C(B):
def mul(self,x,y):
self.x=x
self.y=y
print("The product is:",self.x*self.y)
#read data into a and b
a=int(input("Enter a value:"))
b=int(input("Enter b value:"))
#create object from derived object
ob=C()
[Link](a,b)
[Link](a,b)
[Link](a,b)
Output
Enter a value:2
Enter b value:4
The addition is: 6
The subtraction is: -2
The product is: 8
-----------------------------------------------------------
II [Link]. CS ‘C’
9. Encapsulation
class Computer:
def __init__(self):
self.__maxprice = 900
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
def setMaxPrice(self, price):
self.__maxprice = price
c = Computer()
[Link]()
# change the price
c.__maxprice = 1000
[Link]()
# using setter function
[Link](1000)
[Link]()
Output
Selling Price: 900
Selling Price: 900
Selling Price: 1000
-------------------------------------------------------------
10.1 Polymorphism (Method Overloading)
class Polygon:
# method to render a shape
def render(self):
print("Rendering Polygon...")
class Square(Polygon):
# renders Square
def render(self):
print("Rendering Square...")
class Circle(Polygon):
# renders circle
def render(self):
print("Rendering Circle...")
# create an object of Square
s1 = Square()
II [Link]. CS ‘C’
[Link]()
# create an object of Circle
c1 = Circle()
[Link]()
Output
Rendering Square...
Rendering Circle...
-------------------------------------------------------
10.2 Polymorphism (Method Overloading)
class Employee:
def Hello_Emp(self,e_name=None):
if e_name is not None:
print("Hello "+e_name)
else:
print("Hello ")
emp1=Employee()
emp1.Hello_Emp()
emp1.Hello_Emp("Besant")
Output
Hello
Hello Basent
------------------------------------------------------------
11. Method overriding -super () method
class BC:
def disp(self):
print("Base class Method")
class DC(BC):
def disp(self):
print("Derived class method")
ob=DC()
[Link]()
Output
Derived class method