-
Notifications
You must be signed in to change notification settings - Fork 6
/
classes and modules 2.py
114 lines (80 loc) · 2.52 KB
/
classes and modules 2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#Q1.Create a class Animal as a base class and define method animal_attribute. Create another class Tiger which is inheriting Animal and access the base class method.
'''
class Animal:
def animal_attribute(self,name,atri):
print(name," has attribute to ",atri)
class Tiger(Animal):
def show_attribute(self,name,atri):
self.animal_attribute(name,atri)
T = Tiger()
T.show_attribute("Tiger","roar")
'''
####################
#Q2.What will be output of following code.
'''
class A:
def f(self):
return self.g()
def g(self):
return 'A'
class B(A):
def g(self):
return 'B'
a = A()
b = B()
print a.f(), b.f()
print a.g(), b.g()
'''
''' Output :
A B
A B '''
######################
#Q3.Create a class Cop. Initialize its name, age , work experience and designation. Define methods to add, display and update the following details. Create another class Mission which extends the class Cop. Define method add_mission _details. Select an object of Cop and access methods of base class to get information for a particular cop and make it available for mission.
"""
class Cop:
name=""
work=""
experince=1
designation=""
def add(self,n,w,e,d):
self.name=n
self.work=w
self.experince=e
self.designation=d
def display(self):
print("Name: ",self.name)
print("Work: ",self.work)
print("Experince(in months): ",self.experince,"months")
print("Designation: ",self.designation)
class Mission(Cop):
def add_mission_details(self,m):
print("Mission: ",m)
m1 = Mission()
m1.add("Ashish Rathor","Crime Branch",48,"ACP")
m1.display()
m1.add_mission_details("Murder case of Rohit")
"""
######################
#Q4. Create a class Shape.Initialize it with length and breadth Create the method Area. Create class rectangle and square which inherits shape and access the method Area.
'''
class Shape:
length=int()
breadth=int()
def area(self,l,b):
self.length=l
self.breadth=b
return self.length*self.breadth
class Rectangle(Shape):
def display_area(self):
l=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
print("Area of Rectangle: ",self.area(l,b))
r= Rectangle()
r.display_area()
class Square(Shape):
def display_area(self):
a=int(input("Enter side length of square: "))
print("Area of Rectangle: ",self.area(a,a))
s= Square()
s.display_area()
'''