0% found this document useful (0 votes)
4 views7 pages

Python Classes for Geometry and Inheritance

The document contains several Python programming examples demonstrating key concepts such as classes, objects, inheritance, polymorphism, and data hiding. Each example includes code snippets, expected outputs, and verification of results. Topics covered include calculating the area and perimeter of a circle, single and multi-level inheritance, compile-time and run-time polymorphism, and data hiding techniques.

Uploaded by

anu2006mercy
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)
4 views7 pages

Python Classes for Geometry and Inheritance

The document contains several Python programming examples demonstrating key concepts such as classes, objects, inheritance, polymorphism, and data hiding. Each example includes code snippets, expected outputs, and verification of results. Topics covered include calculating the area and perimeter of a circle, single and multi-level inheritance, compile-time and run-time polymorphism, and data hiding techniques.

Uploaded by

anu2006mercy
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

EX:5 To Calculate Area And Perimeter Of Circle Using Classes And Objects In Python

Program Code:

import math

class Circle:

def __init__(self, radius):

[Link] = radius

def calculate_circle_area(self):

return [Link] * [Link]**2

def calculate_circle_perimeter(self):

return 2 * [Link] * [Link]

radius = float(input("Input the radius of the circle: "))

c = Circle(radius)

area = c.calculate_circle_area()

perimeter = c.calculate_circle_perimeter()

print("Area of the circle:", area)

print("Perimeter of the circle:", perimeter)

Output:

Input the radius of the circle: 7

Area of the circle: 153.93804002589985

Perimeter of the circle: 43.982297150257104

Result:

The output is successfully verified


EX:6A(I) Python Program to Demonstrate Inheritance (Single)

Program Code:

class Animal:

def __init__(self, name):

[Link] = name

def speak(self):

pass

class Dog(Animal):

def speak(self):

return f"{[Link]} says Woof!"

class Cat(Animal):

def speak(self):

return f"{[Link]} says Meow!"

dog = Dog("Buddy")

cat = Cat("Whiskers")

print([Link]())

print([Link]())

Output:

Buddy says Woof!

Whiskers says Meow!

Result:

The output is successfully verified


EX:6A(II) Python Program to Demonstrate Inheritance (Multi Level)

Program Code:

class student:

'A student class'

def __init__(self):

[Link]=(input("Enter student name:"))

[Link]=int(input("Enter rollno:"))

def display(self):

print("\nName:",[Link])

print("\nRollNo:",[Link])

class marks(student):

'A marks class'

def __init__(self):

self.m1=float(input("Enter marks of subject 1:"))

self.m2=float(input("Enter marks of subject 2:"))

self.m3=float(input("Enter marks of subject 3:"))

def display_marks(self):

print("\nMarks of subject1=",self.m1)

print("\nMarks of subject2=",self.m2)

print("\nMarks of subject3=",self.m3)

class results(marks):

'A result class'

def __init__(self):

student.__init__(self)
marks.__init__(self)

self.total_marks=self.m1+self.m2+self.m3

[Link]=self.total_marks*100/300

def display_result(self):

print("\m Total marks:",self.total_marks)

print("\n Percentage:",[Link])

r1=results()

[Link]()

r1.display_marks()

r1.display_result()

Output:

Enter student name:venus

Enter rollno:005

Enter marks of subject 1:43

Enter marks of subject 2:55

Enter marks of subject 3:89

Name: venus

RollNo: 5

Marks of subject1= 43.0

Marks of subject2= 55.0

Marks of subject3= 89.0

\m Total marks: 187.0

Percentage: 62.33333333333333

Result:

The output is successfully verified


EX:6B(I) Python Program to Demonstrate Polymorphism (Compile Time -Overloading)

Program Code:

class Calculator:

def add(self, a, b=0, c=0):

return a + b + c

calc = Calculator()

result1 = [Link](5)

result2 = [Link](5, 3)

result3 = [Link](5, 3, 2)

print(result1)

print(result2)

print(result3)

Output:

10

Result:

The output is successfully verified


EX:6B(II) Python Program to Demonstrate Polymorphism (Run Time- Overriding)

Program Code:
class Parent():

def __init__(self):
[Link] = "Inside Parent"

def show(self):
print([Link])

class Child(Parent):

def __init__(self):
[Link] = "Inside Child"

def show(self):
print([Link])

obj1 = Parent()
obj2 = Child()

[Link]()
[Link]()
Output:

Inside Parent

Inside Child

Result:

The output is successfully verified


EX:7 Python Program to Demonstrate Data Hiding

Program Code:

class Solution:

__privateCounter = 0

def sum(self):

self.__privateCounter += 1

print(self.__privateCounter)

count = Solution()

[Link]()

[Link]()

print(count._Solution__privateCounter)

Output:

Result:

The output is successfully verified

You might also like