Demonstrate use OOP principles, data structures and file handling operations to carry out the following tasks.
a) Define a class named PERSON, with child classes named DAUGHTER and SON who also have derived classes named GRANDSON and GRANDDAUGHTER respectively. All children classes have derived genetic features and skills from PERSON.
Create 5 methods for getting and setting genetic features, setting and getting skills and demonstrate the concept of polymorphism.
b) Modify the answer in a) and show how the PERSON features are captured in a file called person.txt. Also create another method to read from the file person.csv and store the data in a list called persondata.
Clearly show how exceptions are handled in year program.
1. Single inheritance
class Parent:
def f1(self):
print("Function of parent class.")
class Child(Parent):
def f2(self):
print("Function of child class.")
object1 = Child()
object1.f1()
object1.f2()
2. Multiple Inheritance
class Parent_1:
def f1(self):
print("Function of parent_1 class.")
class Parent_2:
def f2(self):
print("Function of parent_2 class.")
class Parent_3:
def f3(self):
print("function of parent_3 class.")
class Child(Parent_1, Parent_2, Parent_3):
def f4(self):
print("Function of child class.")
object_1 = Child()
object_1.f1()
object_1.f2()
object_1.f3()
object_1.f4()
3. Multi-level Inheritance
class Parent:
def f1(self):
print("Function of parent class.")
class Child_1(Parent):
def f2(self):
print("Function of child_1 class.")
class Child_2(Child_1):
def f3(self):
print("Function of child_2 class.")
obj_1 = Child_1()
obj_2 = Child_2()
obj_1.f1()
obj_1.f2()
print("\n")
obj_2.f1()
obj_2.f2()
obj_2.f3()
class Car: #parent class
def __init__(self, name, mileage):
self.name = name
self.mileage = mileage
def description(self):
return f"The {self.name} car gives the mileage of {self.mileage}km/l"
class BMW(Car): #child class
pass
class Audi(Car): #child class
def audi_desc(self):
return "This is the description method of class Audi."
obj1 = BMW("BMW 7-series",39.53)
print(obj1.description())
obj2 = Audi("Audi A8 L",14)
print(obj2.description())
print(obj2.audi_desc())
import csv
file = open("Salary_Data.csv")
csvreader = csv.reader(file)
header = next(csvreader)
print(header)
rows = []
for row in csvreader:
rows.append(row)
print(rows)
file.close
Comments
Leave a comment