| A real life Inheritance Example Program Description :- Here i tried to create a more practical and realistic example of inheritance . First of all i created a Car class which is by default inheriting the object class . inside the Car class showModel () and showColor() are the two methods created which takes model and color of the car respectively . Then created four cars Company class namely tata , maruti , mahindra and ashok leyland inside each class showPrice() method is created to calculate the car model price of each company . Then I created four variables namely CarName (to store car Company name i.e. - tata) , carModel(to store the car model of that company) , carColor (to store the color of the car) , and Price (to store the price of the car) . Now here i have created a if elif and else loop to create the instance of the class only if the carName i.e. car Company name is matched . else it will show the car not in the stock message . Base Class => Car Derived classes = >Maruti , tata , Ashokleyland , Mahindra Code : #---------------------------------------------------------- #Name :- carInheritance.py #Purpose :- an example code of inheritance #Author :- Dipayan Dutta #Created :- 30/08/2015 #---------------------------------------------------------- class Car (object): #creating the Car Class (The Base Class ) def __init__(self,name,color): self.name = name self.color = color def showModel(self,name): self.name = name print "Car Model is :"+str(self.name) def showColor(self,color): self.color = color print "Car color is :"+str(self.color) class Maruti(Car): #Maruti the derived class def showPrice(self,price): self.price = (price*12.5)+1000 print "Car Price is :"+str(self.price) class Tata(Car): def showPrice (self,price): self.price = (price * 14.5)+5000 print "Car Price is :"+str(self.price) class Ashokleyland(Car): def showPrice(self,price): self.price = (price * 20.5)+12506 print "Car Price is :"+str(self.price) class Mahindra(Car): def showPrice(self,price): self.price = (price *5.52)+12345 print "Car Price is :"+str(self.price) #creating and storing car data in the four variable carName = raw_input("Enter Car Company").lower() carModel = raw_input("Please Enter Car Model") carColor = raw_input("Please Enter Car Color") price = input("Please enter car price") #printing the car data print carName print carModel print carColor print price #tata = Tata("nano","Electric Blue") #tata.showModel("nano") #tata.showColor("Electric Blue") #tata.showPrice(100000) #now using the if-elif-else loop to check the car model and there model , color and price if (carName == "tata"): tata = Tata(carModel,carColor) #print tata print "===============================" tata.showModel(carModel) tata.showColor(carColor) tata.showPrice(price) elif (carName == "maruti"): maruti = Maruti(carModel,carColor) print "===============================" maruti.showModel(carModel) maruti.showColor(carColor) maruti.showPrice(price) elif(carName == "ashokleyland"): ashok = Ashokleyland(carModel,carColor) print "=================================" ashok.showModel(carModel) ashok.showColor(carColor) ashok.showPrice(price) elif(carName == "mahindra"): mahindra = Mahindra(carModel , carColor) print "=================================" mahindra.showModel(carModel) mahindra.showColor(carColor) mahindra.showPrice(price) else: print "Not in stock" print "Thanks for Query shall update soon!" Click to Download the Code . |
carinheritance.py |