Python OOps
Python OOps
------
OOPS: Stands for Object oriented Programing System.
2. class
---------
--> class is one container
but it is logical contianer
eg:
---
faculty
------
bank:
in visable thing
human: renuka:
vehicle: car
---------------------
3. inheritence
----------------------
process of sharing code form one class to another class class
parent class new class is chid class
-->
4. polymorphism
----------------
poly : many
morphism: forms
1. method overloading
2. method overriding
----------------------
5. abstraction:
---------------
public
private
protected
-->
atm:
----
6. encpsulation
=================
-->
Advantage of OOPs
Eg:
----
navya
----
state:hieght, weight, color......etc (Properties)
behaviour:sleep(), walk(), run().....etc (Funtionality)
identity:memory allocation (unigue)
fan
----
state:color,cost,no_ofwings, company ...etc
behaviour:rotation_on(), rotation_off().....etc
identity: power unique memory location in heap area (PVM).
Bike:
----
state: color, cost,shape, company_name,tyre_name.....etc
behaviour: start(), stop()
identity: memory allocation
-------------------------------------------------
sbi atm
-------
--> acc_no,acc_name,balance...etc
--> withdraw(),deposit().pin_gen().....etc
wayed:
-------
--> hieght,color,weight,..etc
--> walk(),sleep(),run(),eat()..etc
honda bike
----------
color,cc_value,cost,company_name.....etc
--> engine_start(),stop(),rinding()....etc
======================================
--> the main use of object is to provide memory to the class
syntax:
-------
object_name=class_name()
eg:
---
a=ATM()
f=Fan()
h=Honda_Bike()
n=Navya()
=======================================
Class:
---------
--> class is collection of object
--> class is collection of variables,methods ,constructors.
--> class is model,or blue print.
--> we can write a class to reprasent properties(attributes) and
behaviour (functions-methods) of object.
--> properties can be reprasent by variables
--> Actions can be reprasent by methods (functions)
-->
class Student:
variables
methods
s=Student()
--------------------
class Atm:
variables
methods
a=Atm()
a.variable
a.method_name()
-------------------------------------------
eg1:
class Student:
def insert(cls):
cls.sid=120 #instence variable
cls.sname="Navya"
def display(cls):
print(cls.sid,"\t",cls.sname)
s=Student()
s.insert()
s.display()
# cls:--> current class intence variable or it is reference variable
-------------------------------------------
Example
----------
class Student:
def m1(self): # current class intences
self.id=int(input("Enter Your id value:"))
self.name=input("Enter your name")
print("MY id=",self.id)
print("My name=",self.name)
obj=Student()
print(id(obj))
obj.m1()
obj.m1()
output
-------
2386168090976
Enter Your id value:100
Enter your namePriya
MY id= 100
My name= Priya
Enter Your id value:200
Enter your namenavya
MY id= 200
My name= navya
--------------------------
class Student:
def m1(self): # current class intences / object
self.id=int(input("Enter Your id value:"))
self.name=input("Enter your name")
print("MY id=",self.id)
print("My name=",self.name)
s=Student()
s.m1()
s.m1()
---------------------------------------
class Student:
def m1(self,sid,sname): # current class intences / object
self.id=sid
self.name=sname
print("MY id=",self.id)
print("My name=",self.name)
s=Student()
s.m1(100,"NihariPriya")
s.m1(200,"Anjali")
s.m1(300,"Navya")
-----------------------
with static values
with input funstion
with pass value throught object
-----------------------------------
=============================================
3 types variable
------------------
1. global variable
2. local
3. non-local
=======================
at class level
-------------
3 types
-------------
1. local variable (inside method)
2. instence variable( object level variable with self keyword)
3. reference variable (variable refer by the class name)
4. static variable( class level variable)
---------------------------------
class Student:
def m1(self):
self.id=120
self.name='sai'
print("MY id=",self.id)
print("My name=",self.name)
def m2(self):
print("MY id=",self.id)
print("My name=",self.name)
def m3(self):
print("MY id=",self.id)
print("My name=",self.name)
obj=Student()
print(id(obj))
obj.m1()
obj.m2()
obj.m3()
========================================
# program for variable
class Emp:
company="TCS" static variable
def __init__(self,eid,ename): # here eid and ename are local variable
self.eid=eid
self.ename=ename # self.eid,self.ename are instence variable
def print_value(self):
print("My id=",self.eid)
print("my name=",self.ename)
e1=Emp(100,'Yakub') # here e1 is a reference variable
e1.print_value()
=================================================
class Emp:
def insert_record(self,eid,ename,esal): # local variable
self.eid=eid
self.ename=ename
self.esal=esal
def print_record(self):
print("My id=",self.eid)
print("my name=",self.ename)
e1=Emp()
e1.insert_record(99,'aryan',90000.98)
e1.print_record()
e2=Emp()
e2.insert_record(199,'Renuka',190000.98)
e2.print_record()
e3=Emp()
e3.insert_record(1,'wayed',990000.98)
e3.print_record()
============================================
class Emp:
def insert_record(self):
self.eid=int(input("Enter eid value:"))
self.ename=input("Enter Your name:")
self.esal=float(input("Enter emp salary:"))
def print_record(self):
print("My id=",self.eid)
print("my name=",self.ename)
print("my name=",self.esal)
e1=Emp()
e1.insert_record()
e1.print_record()
e2=Emp()
e2.insert_record()
e2.print_record()
e3=Emp()
e3.insert_record()
e3.print_record()
==========================================
self: variable
---------------
eg:
def m1(self):
def __init__(self):
self.varaibale_nme=value
=======================================
constructors
-----------------
--> constructor is a special method in a python
--> in python constructor define with __init__(self)
--> constructor will be executed automatically at the time of
objcet creation.
--> The main purpose of constructor is to declare and initialize instence
variable.
--> per object constructor will execute only once.
--> constructor can take atleast one argument(i.e self).
eg:
---
class Con_Demo:
def __init__(self):
print("Constructor executed with out calling with object")
def sms(self):
print("My Own Method")
obj=Con_Demo()
ob=Con_Demo()
ob.sms()
====================================
eg2:
----
class Emp:
def __init__(self):
self.eid=int(input("Enter eid value:"))
self.ename=input("Enter Your name:")
self.esal=float(input("Enter emp salary:"))
def print_record(self):
print("My id=",self.eid)
print("my name=",self.ename)
print("my name=",self.esal)
e1=Emp()
e1.print_record()
e2=Emp()
e2.print_record()
e3=Emp()
e3.print_record()
===========================================
eg:
---
class Emp:
def __init__(self,eid,ename,esal):
self.eid=eid
self.ename=ename
self.esal=esal
def print_record(self):
print("My id=",self.eid)
print("my name=",self.ename)
print("my name=",self.esal)
e1=Emp(100,'aryan',9000.99)
e1.print_record()
e2=Emp(99,'ksv',7890.88)
e2.print_record()
=========================================
1. program for student record?
2. program for employess record?
3. program for ATM mechine?
----------------------------------------