0% found this document useful (0 votes)
1 views10 pages

Class Rational

The document defines classes for Dog, Duck, and Human, each with a speak method that outputs a respective sound. It also explains the use of magic methods in Python for operator overloading, detailing binary, comparison, assignment, and unary operators. Additionally, it presents a Rational class with methods for reading, writing, and adding rational numbers, along with examples of usage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views10 pages

Class Rational

The document defines classes for Dog, Duck, and Human, each with a speak method that outputs a respective sound. It also explains the use of magic methods in Python for operator overloading, detailing binary, comparison, assignment, and unary operators. Additionally, it presents a Rational class with methods for reading, writing, and adding rational numbers, along with examples of usage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

# Define the Dog class

class Dog:
def speak(self):
print("Dog says: Bark! Bark!")

# Define the Duck class


class Duck:
def speak(self):
print("Duck says: Quack! Quack!")

# Define the Human class


class Human:
def speak(self):
print("Human says: Hello there!")
#Creating Objects and Calling Methods
dog = Dog()
duck = Duck()
human = Human()
[Link]() # Output: Dog says: Bark! Bark!
[Link]() # Output: Duck says: Quack!
Quack!
[Link]() # Output: Human says: Hello
there!

# __init__() is a constructor method that runs


when an object is created. It’s used to initialize
object data.

#self refers to the current object (instance)


and is used to access attributes and methods
inside the class.
R1, R2
R3=R1+R2
Rational operator +(int )

[Link]= num*[Link]+[Link]*den
[Link]=den*[Link];
Return R3;

Python magic methods or special functions for


operator overloading
Binary Operators:
Operator Magic Method
+ __add__(self, other)
– __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
% __mod__(self, other)
** __pow__(self, other)
>> __rshift__(self, other)
<< __lshift__(self, other)
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)
Comparison Operators:
Operator Magic Method
< __lt__(self, other)
> __gt__(self, other)
<= __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)
Assignment Operators:
Operator Magic Method

-= __isub__(self, other)

+= __iadd__(self, other)

*= __imul__(self, other)

/= __idiv__(self, other)

//= __ifloordiv__(self, other)

%= __imod__(self, other)

**= __ipow__(self, other)

>>= __irshift__(self, other)

<<= __ilshift__(self, other)


&= __iand__(self, other)

|= __ior__(self, other)

^= __ixor__(self, other)
Unary Operators:
Operator Magic Method

– __neg__(self)

+ __pos__(self)

~ __invert__(self)
Note: It is not possible to change the number of
operands of an operator. For example: If we can
not overload a unary operator as a binary
operator.

Rational()
{
N=0;d=0;
}
Rational(int n1,int d1)
{
N=n1;
D=d1;
}
Rational R1,R2(10,30)
class Rational:
def read(self):
R1=Rational()
[Link]=int(input()) [Link]()
[Link]=int(input()) [Link]()
[Link](r2)
def write(self):
print([Link],[Link])
def add(self,r):
t=Rational()
[Link]=[Link]*[Link]+
[Link]=
class Rational:
def __init__(self,n=0,d=0):
[Link]=n
[Link]=d R1=Rational(10,20)
def read(self): I=int(input())
J=int(input())
[Link]=int(input()) R2=Rational(I,j)
[Link]=int(input()) [Link]()
//[Link]()
def write(self):
[Link]()
print([Link],[Link]) [Link]()
def __add__(self,t1):
R1+R2
R1+10
T=Rational()
[Link]=[Link]+[Link]
[Link]=[Link]+[Link]
return T
def __rsub__(self,k):
T=Rational()
[Link]=
return T

R1=Rational()
R2=Rational()
[Link]()
[Link]()
[Link]()
[Link]()
R3=R1+R2
[Link]()
R3=10-R1
[Link]()

class Rational:
def __init__(self,n=0,d=0):
[Link]=n
[Link]=d
def read(self):
[Link]=int(input())
[Link]=int(input())
def __del__(self):
print('deleted')
def print(self):
print([Link],[Link])

def __add__(self,r):
result=Rational()
if (type(r)==Rational):
[Link]=[Link]*[Link]+[Link]
m*[Link]
[Link]=[Link]*[Link]
return result
else:
[Link]=[Link]+[Link]*r
[Link]=[Link]
return result
def __radd__(self,r1):
result=Rational()
[Link]=r1*[Link]+[Link]
[Link]=[Link]
return result
r1=Rational(10,20)
r2=Rational(4,5)
#[Link]()
#[Link]()
[Link]()
#r3=[Link](r2)
r3=r1+r2
r4=r1+10
r5=10+r2
[Link]()
[Link]()
[Link]()

You might also like