Inheritance

  

PYTHON

       Inheritance

       Fill in the blanks

          1.One Object acquires property and behaviour of parent class is called Inheritance.

        2.Overloading is Function name same ,argument different

        3.Overriding is Function name same,argument same

        4.Polymorphism means Multitask

        5.less than or equal to operator internally called __le__

        6.Write operator overloading // expression  __floordiv__

        7.Write bitwise left shift expression in operator overloading __lshift__

        8.Write bitwise NOT  expression in operator overloading  __invert__

        9. Write operator overloading / expression  __truediv__

        10.point(1,1) < point(1,1).    Output=    False

 

Answer the following Question

                                                          (4 marks)

        1.Write a program for Single Inheritance

class Person:

    def __init__(self,no,name):

       self.Empno =no

       self.Ename =name

    def dispdata(self):

       print ("Empoyee    no....:",self.Empno )

       print ("Employee   name...:",self.Ename)

#inherited or subclass (note person in bracket)

class Employee (Person):

    def  callme (self):

         print ("I can use attribute of person")

per=Person(101, "ramkumar")

emp =Employee(102,"suresh")

print("- "*25)

per.dispdata()

emp.dispdata()

        2.Write a program for Method Overriding

class rectangle():

          def __init__(self,length,breadth):

                    self.length = length

                    self .breadth = breadth

          def getarea(self):

                    print(self.length*self.breadth,"is area of rectangle")

class square(rectangle):

        def __init__(self,side):

               self.side  =side

               rectangle.__init__(self,side,side)

        def getarea (self):

               print(self.side*self.side,"is area of square")

s  =  square(4)

r  =  rectangle(2,4)

s.getarea ()    #object s execute derived class  getArea() function not base class getArea()

r.getarea()

        3.Write a program for Polymorphism with a function

class branch1:

          def display(self):

                    print("I am belong to T.nagar ")

class branch2:

           def display(self):

                    print("I am belong to madurai")

def myfun(ok):

                    ok.display()

B1 = branch1()

B2 = branch2()

myfun(B1)

myfun(B2) 



Answer the following Question

                                                          (10 marks)

                1.Write a Program for Multiple Inheritance

class student(object):

          tot=0;  avg=0.0 ;  res=" "

          def __init__(self,no,na):

                self.stdno=no ;  self.stdname=na

          def stdfun(self):

                print ("student   no:",self.stdno,"\nstudent name:",self.stdname)

class test1(object):

          def __init__(self,m1,m2):

                    self.p1 = m1 ;  self.p2 = m2

          def markdisp1(self):

                    print ("c language  :",self.p1,)

                    print ("c++     :",self.p2)

class test2(object):

          def __init__(self,m3,m4):

                    self.p3=m3;self.p4=m4

          def markdisp2(self):

                    print ("java    :",self.p3)

                    print ("python    :",self.p4 )

 

class result(student,test1,test2):

          def __init__(self,no,na,m1,m2,m3,m4):

          # calling constructors of test1

          # and test2 classes

                    student.__init__(self,no,na)

                    test1.__init__(self,m1,m2)

                    test2.__init__(self,m3,m4)

 

          def calfun(self):

                    self.tot=self.p1+self.p2+self.p3+self.p4

                    self.avg=self.tot/4

                          if self.p1>=40 and self.p2>=40 and self.p3>40 and self.p4>=40:

                              self.res="pass"

                    else:

                              self.res="fail"

 

          def resdisplay(self):

                      print("total    :",self.tot)

                      print("avg    :",self.avg)

                      print("result    :",self.res)

 

ob= result(101,"raj",56,67,78,90)

ob.calfun()

print("-"*25)

ob.stdfun()

print("-"*25)

ob.markdisp1()

ob.markdisp2()

print("-"*25)

ob.resdisplay()

print("-"*25)

          2.Write a Program for Multilevel Inheritance

class student :

          def getstudent(self):

                    self.name = input("name: ")

                    self.age = input("age: ")

                    self.gender = input("gender: ")

class test(student):

          def getmarks(self):

                    self.stuclass = input("diploma name: ")

                    print("enter the marks of the respective subjects")

                    self.c = int(input("c language: "))

                    self.cplus = int(input("c + +: "))

                    self.java = int(input("java: "))

                    self.python = int(input("python: "))

 

class marks (test):

#method

          def display(self):

                    print("\n\nname: ",self.name)

                    print("age:",self.age)

                    print("gender: ",self.gender)

                    print("diploma name: ",self.stuclass)

                    print("total marks:{}/400 ".format( self.c + self.cplus + self.java + self.python))

m1 = marks()

#call base class method 'getstudent()'

m1.getstudent()

# call first derived class method 'getmarks()'

m1.getmarks()

#call second derived class method 'display()'

m1.display()

          3.Write a Program for Operator Overloading using Arithmetic Operations(+,-,*,/,//,%)

class arithmetic:

   def __init__(self):

        self.a=int(input("enter first number.."))

   def __add__(self,other):

       return(self.a+other.a)

   def __sub__(self,other):

       return(self.a-other.a)

   def __mul__(self,other):

       return(self.a*other.a)

   def __truediv__(self,other):

       return(self.a/other.a)

   def __floordiv__(self,other):

       return(self.a//other.a)

   def __mod__(self,other):

       return(self.a%other.a)

 

x=arithmetic()

y=arithmetic()

while True:

          print("\n 1.addition")

          print("\n 2.subtraction")

          print("\n 3.multiplication")

          print("\n 4.division")

          print("\n 5.integer division")

          print("\n 6.remainder")

          print("\n 7.exit")

          ch=int(input("\choice ….[1..7]"))

          if  ch>=1 and ch <= 7:

                    if ch == 1: print("\n addition value=",x+y)

                    if ch == 2: print("\n subtraction value=",x-y)

                    if ch == 3: print("\n multiplication value=",x*y)

                    if ch == 4: print("\n true division value=",x/y)

                    if ch == 5: print("\n integer value=",x//y)

                    if ch == 6: print("\n remainder value=",x%y)

                    if ch == 7: break

          else:

                continue

No comments:

Post a Comment