0% found this document useful (0 votes)
61 views19 pages

OOP Programs

The document provides examples of object-oriented programming concepts in Python like classes, objects, inheritance, polymorphism etc. Some key examples include: 1. Defining a simple class with attributes and creating object instances. 2. Checking built-in class attributes like __doc__, __name__ etc. 3. Creating a class with __init__ method to initialize objects. 4. Demonstrating method overriding in single and multiple inheritance hierarchies. 5. Showing how derived class objects can access methods of base classes. 6. Examples of private and public attributes and methods in classes.

Uploaded by

Rohan patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views19 pages

OOP Programs

The document provides examples of object-oriented programming concepts in Python like classes, objects, inheritance, polymorphism etc. Some key examples include: 1. Defining a simple class with attributes and creating object instances. 2. Checking built-in class attributes like __doc__, __name__ etc. 3. Creating a class with __init__ method to initialize objects. 4. Demonstrating method overriding in single and multiple inheritance hierarchies. 5. Showing how derived class objects can access methods of base classes. 6. Examples of private and public attributes and methods in classes.

Uploaded by

Rohan patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

'''

# 1-Creating a simple class


class box:
side=5

b1=box()
print(b1.side)
'''
'''
#2- Checking class built-in attributes

class Person:
"This is Person class"
print(Person.__doc__)
print(Person.__dict__)
print(Person.__name__)
print(Person.__module__)
print(Person.__bases__)

p1=Person()
print(type(p1))
'''
'''
# 3 - Creating a simple class with __init__ function

class Person:
"This is Person class"
def __init__(self,name):
self.name=name
print("Person Created :", self.name)

print(Person.__doc__)
p1=Person("John")
p2=Person("Jim")
'''
'''
#4 - Accessing object methods

class Person:
"This is Person class"
def __init__(self,name,age):
self.name=name
self.age=age
print("Person Created :", self.name)

def myfunc(name):
print("Hello my name is" +self.name)

p1=Person("John",30)
p1.myfunc()
'''
'''
#5 - Different class objects will access respective class methods

class India:
def capital(self):
print("Delhi")
def language(self):
print("Hindi")

class USA:
def capital(self):
print("Washington")
def language(self):
print("English")

obj1 = India()
obj2 = USA()

for x in (obj1, obj2):


x.capital()
x.language()
'''
'''
# 6 - Accessing class attributes

class Employee:
"This is the Employee class"
empcount=0

def __init__(self,name,salary):
self.name=name
self.salary=salary
Employee.empcount+=1

def count():
print("-------Count in Employee =",Employee.empcount)

e1=Employee("John",1000)
print("Name:",e1.name, "salary:", e1.salary)
Employee.count()
e2=Employee("Harry",2000)
print("Count outside Employee",Employee.empcount)

print("__doc__ :",Employee.__doc__)
print("__dict__ :",Employee.__dict__)
print("Class Name:",Employee.__name__)
print("Module Name :",Employee.__module__)
print("Bases : ",Employee.__bases__)
'''
'''
# 7- Accessing private class attributes through methods

class Employee:
"This is the Employee class"
__count=0

def __init__(self,name,salary):
self.name=name
self.salary=salary
Employee.__count+=1

def display(self):
print("Employee ",Employee.__count,"Name: ", self.name,"Salary :",self.salary)

def __del__(self):
class_name = self.__class__.__name__
print(class_name, "destroyed")

e1=Employee("John",1000)
e1.display()

e1.salary=200
print("Salary updated....")
e1.display()

e2=Employee("Harry",2000)
e2.display()
#print("No, of Employees = ", Employee.__count)
#del e1.salary
#del e1
e1.display()
'''
'''
# 8 -Data Hiding 1
class Counter:
__count=0

def getcount(self):
self.__count += 1
print("Count = ", self.__count)

c=Counter()
c.getcount()
#print(c.__count) # raises error.cannot access private members
'''
'''
# 9 - Data Hiding 2

class Computer:

def __init__(self):
self.__price = 1000
print("Computer object created with price: ",self.__price)

def setPrice(self, maxprice):


self.__price = maxprice

def sell(self):
print("Selling price : ",self.__price)
c = Computer()
c.sell()

c.__price = 1500
c.sell()

c.setPrice(1500)
c.sell()
'''
'''
#10- Accessing class attributes from module

import oop_module
e1=oop_module.Employee("John",1000)
e2=oop_module.Employee("Harry",2000)
e1.display()
e1.salary=200
print("Salary updated....")
e1.display()
e2.display()
'''
'''
#11-Single inheritance - 1

class Parent:
def func1(this):
print("In parent class")

class Child(Parent):
def func2(this):
print("In child class")
c = Child()
c.func1()
c.func2()
if isinstance(c,Child):
print("c is object of Child class")
if issubclass(Child,Parent):
print("Child class is subclass of Parent class")
'''
'''
# 12 - Single inheritance - 2 (with __init__ method)
class Person:
__pcount=0

def __init__(self,name,age):
self.name=name
self.__age=age
Person.__pcount+=1
print("\n\nPerson", Person.__pcount,"created")

class Employee(Person):
def __init__(self,name,age,sal):
super().__init__(name,age)
self.sal=sal
print("Employee created")

p=Person("Ram",30)
e=Employee("Sham",25,1000)
'''
'''
# 13 - Multiple inheritance
class Color:
def fun_c(this):
print("In Color class")

class Shape:
def fun_s(this):
print("In Shape class")

class Box(Color, Shape):


def fun_b(this):
print("In box class")

b = Box()
b.fun_c()
b.fun_s()
b.fun_b()
'''
'''
# 14 - Multilevel Inheritance

class Person:
def __init__(self,name):
self.name = name

class Employee(Person):
def __init__(self,name,salary):
Person.__init__(self,name)
self.salary = salary

class Worker(Employee):
def __init__(self,name,salary,workhrs):
Employee.__init__(self,name,salary)
self.workhrs = workhrs

def display(self):
print("Worker info:\n","Name:",self.name,"Salary:",self.salary,"Hrs:",self.workhrs)

w = Worker("Jill",1000,5)
w.display()
'''
'''
# 15 - Hierarchical Inheritance

class Person:
def __init__(self,name,age):
self.name = name
self.age=age

class Employee(Person):
def __init__(self,name,age,salary):
Person.__init__(self,name,age)
self.salary = salary

def display(self):
print("Emp info:\n","Name:",self.name,"Age:",self.age,"Salary:",self.salary)

class Worker(Person):
def __init__(self,name,age,wage):
super().__init__(name,age)
self.wage = wage

def display(self):
print("Worker info:\n","Name:",self.name,"Age:",self.age,"Daily wage:",self.wage)
e=Employee("EmpRam",45, 10000)
e.display()
w = Worker("Jill",45,300)
w.display()
'''
'''
# 16 - Method overloading 1

def add(x , y, z=0, w=0):


return x+y+z+w

print(add(2,3))
print(add(2,3,4))
print(add(2,3,4,5))
'''
'''
# 17 - Method overloading 2 (class method)

class Student:
def hello(self, name=None):
if name is not None:
print("Hey", name)
else:
print("Hey")

std=Student()
std.hello()
std.hello('Prasad')
'''
'''
# 18 - Method overriding 1 (in single inheritance)
class Bird:
def __init__(self):
print("Bird Ready")

def who(self):
print("I am a bird")

def swim(self):
print("I can swim")

class Penguin(Bird):
def __init__(self):
print("Penguin Ready")

def who(self):
super().who()
print("I am a Penguin")

def run(self):
print("I can run")

p = Penguin()
p.who()
p.swim()
p.run()
'''
'''
# 19 - Method overriding 2 ( in hierarchical inheritance)

class bird:
def intro(self):
print("there are many types of birds")
def flight(self):
print("Most birds can fly but some cannot")

class sparrow(bird):
def flight(self):
print("Sparrows can fly")

class ostrich(bird):
def flight(self):
print("Cannot fly")

b=bird()
s=sparrow()
o=ostrich()

b.intro()
b.flight()

s.intro()
s.flight()

o.intro()
o.flight()

'''
'''
# 20 - Manual (Method overriding)

class Animal:
multicellular=True
e=True

def breathe(self):
print("I breathe Oxygen")

def food(self):
print("I eat food")

class herbivorous(Animal):
def food(self):
print("I eat only plants")

h=herbivorous()
h.food()
h.breathe()
'''
'''
# 21 - Manual (14/Exercise XI (1) - Pg. 89)

class Check:
def prnt(self,a,c):
if type(a)==int:
print("Int ",a ,"Char ",c)
else:
print("Char", a,"Int",c)

ch = Check()
ch.prnt(2,'s')
ch.prnt('x',20)
'''
'''
# 22 - Manual - Method overloading (14/Exercise XI (2) - Pg. 89)
class Quad:
def area(self,length,breadth=0):
if breadth == 0:
return length * length
else:
return length * breadth

s=Quad()
r=Quad()
print("Area of square", s.area(2))
print("Area of rectangle",r.area(2,3))
'''
'''
# 23 - Manual - Method overriding (14/Exercise XI (3) - Pg. 89)

class Degree:
def getdegree(self):
print("I got degree")

class UG(Degree):
def getdegree(self):
super().getdegree()
print("I am undergraduate")

class PG(Degree):
def getdegree(self):
super().getdegree()
print("I am postgraduate")

d=Degree()
u=UG()
p=PG()

d.getdegree()
u.getdegree()
p.getdegree()
'''
'''
# 24 - Manual (15/Exercise XI (1)- Pg 95)
class Employee:
def get(self):
self.name = input("Enter name: ")
self.dept = input("Enter dept.: ")
self.salary = int(input("Enter salary : "))
def put(self):
print("Name : ",self.name)
print("Dept.: ",self.dept)
print("Salary : ",self.salary)

e = Employee()
e.get()
e.put()
'''
'''
# 25 - Manual (15/Exercise XI (2)- Pg 95) - Using base class init method with super keyword

class Person:
def __init__(self,name,age):
self.name=name
self.age=age

def display(self):
print("Name :",self.name)
print("Age : ",self.age)

class Student(Person):
def __init__(self,name,age,per):
super().__init__(name,age)
self.per = per

def display(self):
super().display()
print("Percentage Marks :",self.per)

name = input("Enter name:")


age = int(input("Enter age:"))
per = float(input("Enter per:"))
s=Student(name,age,per)
s.display()
'''
'''
# 26 - Manual - Multipe inheritance (15/Exercise XI (3)- Pg 95)

class A:
a = 10

def amethod(self):
print("Hello from class A")

class B:
b = 20

def bmethod(self):
print("Hello from class B")
class C(A,B):
c = 30

def cmethod(self):
print("Hello from class c")

x = C()
print("a=",x.a)
x.amethod()
print("b=",x.b)
x.bmethod()
print("c=",x.c)
x.cmethod()
'''
'''
# 27 - Specializing inherited methods (Inherit, Replace, Extend, Provide)
class Super:
def method(self):
print("in Super.method") # default

def customize(self):
self.action() # expected in derived class

class Inheritor(Super):
pass

class Replacer(Super):
def method(self):
print("in Replacer.method")

class Extender(Super):
def method(self):
print("starting Extender.method")
Super.method(self)
print ("ending Extender.method")

class Provider(Super):
def action(self):
print("in Provider.action")

for klass in (Inheritor, Replacer, Extender):


print ("\n ", klass.__name__ ," ...")
klass().method()

print("\nProvider...")
Provider().customize()
'''
'''
# 28-Composition program -1
class Engine:
def start(self):
print("Engine started")
def stop(self):
print("Engine stopped")

class Car:
def __init__(self, model,capacity,fuel):
self.model=model
self.capacity=capacity
self.fuel=fuel

class Ford(Car):
def __init__(self,model, capacity,fuel,color,avg):
super().__init__(model,capacity,fuel)
self.color = color
self.fuel = fuel
self.engine = Engine()

f=Ford("SUV",7,"PETROL","PEARL WHITE",30)
f.engine.start()
f.engine.stop()
'''

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy