Using python write the code We’ve already done this for you to give you an examp
ID: 3711710 • Letter: U
Question
Using python write the code
We’ve already done this for you to give you an example.
Note: You don’t need to actually call the methods, you just need to write a print statement, as for obj1.f() above. All you need are the four print statements.
1 print( 'obj 1: C1.f, C1. g') # Cl.f calls C1.g; no superclass methods are called Inheritance Method Calls Consider the following class definitions. class C1): def f (self): return 2 self.g) def g(self): return 2 class c2 (C1) def f(self): return 3 sclf.g) class C3 (C1): def g(self): return 5 class C4 (C3): def f (self): return 7 self.g) obj1 = C1 ( ) obj2 C2() obj3C3) obj4 C4() For this problem you are to consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy working out what happens will not always be straightforward For each of obj1, obj2, obj3, and obj4, print out the methods which will be called following a call to objn.). You should print a single line in the format: objn: calll, call2, call3. So for example, when objl.fo is called, you should print: objl: Cl.f, Cl.gExplanation / Answer
PROGRAM
class C1(): # Create base class C1
def f(self): # implement method f()
return 2*self.g() # calling g() method and multiplyed by 2 and return value
def g(self): # implement method g()
return 2 # return value 2
class C2(C1): # Create derived class C2 from C1 base class
def f(self): # implement method f()
return 3*self.g() # calling g() method from base class C1 and multiplyed by 3 and return value
class C3(C1): # Create derived class C3 from C1 base class
def g(self): # implement method g()
return 5 # return value 5
class C4(C3): # Create derived class C4 from C3 base class
def f(self): # implement method f()
return 7*self.g() # calling g() method from C3 base class and multiplyed by 7 and return value
obj1=C1() # Create Object obj1 of C1() class
obj2=C2() # Create Object obj2 of C2() class
obj3=C3() # Create Object obj3 of C3() class
obj4=C4() # Create Object obj4 of C4() class
print('C1().f() Value: ',obj1.f()) # calling f() from Class C1() and return value 4
print('C2().f() Value: ',obj2.f()) # calling f() from Class C2() and return value 6
print('C1().f() Value: ',obj3.f()) # calling f() from Class C1() and return value 10 because C3 class g() method return 5 multiplyed by 2 the value is 10
print('C4().f() Value: ',obj4.f()) # calling f() from Class C4() and return value 35 becuase base class of C4 method is g() return value 5 then display 35
OUTPUT
C1().f() Value: 4
C2().f() Value: 6
C1().f() Value: 10
C4().f() Value: 35
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.