Skip to content

harini2007-T/Module-5

 
 

Repository files navigation

Program to demonstrate a default constructor in Python

class Student: # Default constructor def init(self): self.a = input("Enter the student's name: ")

# Method to display message
def show(self):
    print("This is non-parameterized constructor")
    print("Welcome", self.a, "!")

Create object and call method

s1 = Student() s1.show() Enter the student's name: Pika This is non-parameterized constructor Welcome Pika !

Program to demonstrate a Destructor in Python

class Demo: # Constructor def init(self): self.status = "Alive" print("Object is created.") print("Status:", self.status)

# Destructor
def __del__(self):
    print("Destructor is called. Object is being destroyed.")

Create an object

obj = Demo()

Delete the object

del obj

print("End of the program.") Object is created. Status: Alive Destructor is called. Object is being destroyed. End of the program.

Program to demonstrate Hierarchical Inheritance in Python

Base class

class Details: def init(self, name, age): self.name = name self.age = age

def getName(self):
    return self.name

def getAge(self):
    return self.age

Derived class 1 - Employee

class Employee(Details): def init(self, name, age, employee_id, department): super().init(name, age) self.employee_id = employee_id self.department = department

def getEmployeeDetails(self):
    print("\n--- Employee Details ---")
    print("Name:", self.getName())
    print("Age:", self.getAge()

Enter Employee Details: Enter Name: John Enter Age: 30 Enter Employee ID: E101 Enter Department: HR

Enter Patient Details: Enter Name: Alice Enter Age: 25 Enter Patient ID: P205 Enter Disease: Fever

--- Employee Details --- Name: John Age: 30 Employee ID: E101 Department: HR

--- Patient Details --- Name: Alice Age: 25 Patient ID: P205 Disease: Fever

Program to demonstrate Multilevel Inheritance in Python

Parent class

class Person: def init(self, name): self.name = name

def getName(self):
    return self.name

Child class (inherits Person)

class Age(Person): def init(self, name, age): super().init(name) self.age = age

def getAge(self):
    return self.age

Grandchild class (inherits Age)

class Location(Age): def init(self, name, age, location): super().init(name, age) self.location = location

def getLocation(self):
    return self.location

Taking user input

name = input("Enter Name: ") age = int(input("Enter Age: ")) location = input("Enter Location: ")

Creating object of grandchild class

person1 = Location(name, age, location)

Displaying details

print("\n--- Person Details ---") print("Name:", person1.getName()) print("Age:", person1.getAge()) print("Location:", person1.getLocation()) [10/22, 3:51 PM] Bushpika Python Class: Enter Name: Sarah Enter Age: 28 Enter Location: Chennai

--- Person Details --- Name: Sarah Age: 28 Location: Chennai [10/22, 3:54 PM] Bushpika Python Class: # Class 1: For Addition class Calculation1: def Summation(self, a, b): return a + b

Class 2: For Subtraction

class Calculation2: def Subtraction(self, a, b): return a - b

Derived Class: Inheriting from both Calculation1 and Calculation2

class Derived(Calculation1, Calculation2): def Division(self, a, b): if b == 0: return "Division by zero is not allowed" return a / b

--- Main Program ---

Step 1: Get user input

a = float(input("Enter first number: ")) b = float(input("Enter second number: "))

Step 2: Create object of Derived class

obj = Derived()

Step 3: Perform operations

sum_result = obj.Summation(a, b) sub_result = obj.Subtraction(a, b) div_result = obj.Division(a, b)

Step 4: Display results

print("\n--- Arithmetic Operations Result ---") print(f"Addition of {a} and {b} = {sum_result}") print(f"Subtraction of {a} and {b} = {sub_result}") print(f"Division of {a} and {b} = {div_result}") Enter first number: 20 Enter second number: 5

--- Arithmetic Operations Result --- Addition of 20.0 and 5.0 = 25.0 Subtraction of 20.0 and 5.0 = 15.0 Division of 20.0 and 5.0 = 4.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published