You are on page 1of 7

II B.Sc.

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(obj.var)
obj.firstM()

Output
9
hello, World
----------------------------------------------------

3. Class and Objects

class Parrot:
# class attribute
name = ""
age = 0
# create parrot1 object
parrot1 = Parrot()
II B.Sc. CS ‘C’

parrot1.name = "Blu"
parrot1.age = 10
# create another object parrot2
parrot2 = Parrot()
parrot2.name = "Woo"
parrot2.age = 15
# access attributes
print(f"{parrot1.name} is {parrot1.age} years old")
print(f"{parrot2.name} is {parrot2.age} 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
self.name = name # self is an instance of a class
self.age = age
self.salary = salary
self.id = 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()
obj.firstM()
print(obj)

Output
hello, World
<__main__.MyClass object at 0x036A8E10>
<__main__.MyClass object at 0x036A8E10>
--------------------------------------------------------------------------
II B.Sc. CS ‘C’

6. Encapsulation

class MyClass(object):
def setAge(self, num):
self.age = num
def getAge(self):
return self.age
zack = MyClass()
zack.setAge(45)
print(zack.getAge())
zack.setAge("Fourty Five")
print(zack.getAge())

Output

45
Fourty Five
-------------------------------------------------------------
7. Constructor
class Person:
def __init__( self, s ):
self.name = s
def hello( self ):
print ('Hello', self.name)
t = Person("John")
t.hello()

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 B.Sc. 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()
ob.add(a,b)
ob.sub(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


dog1.eat()
dog1.sleep()

# Calling member of the derived class


dog1.bark();
II B.Sc. 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()
ob.add(a,b)
ob.sub(a,b)
ob.mul(a,b)

Output
Enter a value:2
Enter b value:4
The addition is: 6
The subtraction is: -2
The product is: 8
-----------------------------------------------------------
II B.Sc. 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()
c.sell()
# change the price
c.__maxprice = 1000
c.sell()
# using setter function
c.setMaxPrice(1000)
c.sell()

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 B.Sc. CS ‘C’

s1.render()
# create an object of Circle
c1 = Circle()
c1.render()

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()
ob.disp()
Output
Derived class method

You might also like