0% found this document useful (0 votes)
2 views

Program No 6 7 and 8

Uploaded by

Vandana Bharti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program No 6 7 and 8

Uploaded by

Vandana Bharti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program No : 6

Write a simple program implement constructor.

class Student:
# Constructor to initialize the object's attributes
def __init__(self, name, age, student_id):
self.name = name
self.age = age
self.student_id = student_id

# Method to display student details


def display_details(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
print(f"Student ID: {self.student_id}")

# Creating an instance of the Student class


student1 = Student("Alice", 20, "S12345")

# Displaying details of the student


student1.display_details()
Program 7:
Write a program to implement inheritance.
# Base class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def display_details(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")

# Derived class
class Student(Person):
def __init__(self, name, age, student_id, major):
# Call the constructor of the base class
super().__init__(name, age)
self.student_id = student_id
self.major = major

def display_details(self):
# Call the base class method
super().display_details()
print(f"Student ID: {self.student_id}")
print(f"Major: {self.major}")
# Instantiate objects and demonstrate inheritance
print("Base Class Example:")
person = Person("Alice", 45)
person.display_details()

print("\nDerived Class Example:")


student = Student("Bob", 20, "S12345", "Computer Science")
student.display_details()

output: Base Class Example:


Name: Alice
Age: 45

Derived Class Example:


Name: Bob
Age: 20
Student ID: S12345
Major: Computer Science

Program no 8:
Write a program to implement function overloading.
class Calculator:
def add(self, a=None, b=None, c=None):
if a is not None and b is not None and c is not None:
return a + b + c
elif a is not None and b is not None:
return a + b
elif a is not None:
return a
else:
return 0

# Instantiate the Calculator class


calc = Calculator()

# Demonstrate different "overloaded" usages


print("Adding two numbers (3 + 5):", calc.add(3, 5)) # Two arguments
print("Adding three numbers (1 + 2 + 3):", calc.add(1, 2, 3)) # Three arguments
print("Adding one number (10):", calc.add(10)) # One argument
print("No numbers provided:", calc.add()) # No arguments

Output:
class Calculator:
def add(self, a=None, b=None, c=None):
if a is not None and b is not None and c is not None:
return a + b + c
elif a is not None and b is not None:
return a + b
elif a is not None:
return a
else:
return 0

# Instantiate the Calculator class


calc = Calculator()

# Demonstrate different "overloaded" usages


print("Adding two numbers (3 + 5):", calc.add(3, 5)) # Two arguments
print("Adding three numbers (1 + 2 + 3):", calc.add(1, 2, 3)) # Three arguments
print("Adding one number (10):", calc.add(10)) # One argument
print("No numbers provided:", calc.add()) # No arguments

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy