# Chapter 8: Data Abstraction through Classes and User-Defined Data Types¶

## Program Source Code 8.1, page no:208¶

In [1]:
class Super:
def __init__(self):
self.__IntegerData=None  #private member
#public functions
def SetData(self,i):
self.__IntegerData=i      #refer to IntegerData
def ShowData(self):
print "Data is ",self.__IntegerData,' '

ob1=Super()
ob2=Super()
ob1.SetData(1000)
ob2.SetData(2000)
ob1.ShowData()
ob2.ShowData()

Data is  1000
Data is  2000


## Program Source Code 8.2, page no:211¶

In [1]:
class X:
def __init__(self):
self.a=None   #private members
self.b=None      #private members

#no structure type present in python
x = X()

x.a=0
x.b=1
print "x.a=",x.a,",x.b=",x.b

x.a= 0 ,x.b= 1


## Program Source Code 8.3, page no:214¶

In [3]:
class Fraction:

def SetValue(self,a,b):    #public functions
self.__num=a
self.__denom=b
def GetValue(self,a,b):
a=self.__num
b=self.__denom

f=Fraction()
n=input("Enter n: ") #user input
d=input("Enter d: ")    #user input
print "enter the numerator and denominator: ", '  ',n,d

f.SetValue(n,d) #call function SetValue
print "Numerator value set: ", ' ',n
print "Denominator value set: ", ' ',d
f.GetValue(n,d)     #call function GetData
print "Numerator value retrieved: ", ' ',n
print "Denominator value retrieved: ", ' ',d

Enter n: 3
Enter d: 4
enter the numerator and denominator:     3 4
Numerator value set:    3
Denominator value set:    4
Numerator value retrieved:    3
Denominator value retrieved:    4


## Program Source Code 8.4, page no:216¶

In [1]:
class SimpleClass:
def __init__(self):
self.__i=None  #private member

def __init__(self):
self.__i=500             #constructor

def SetData(self,d):
self.__i=d

def GetData(self):
return self.__i

#Initializing
s1=SimpleClass()
s2=SimpleClass()
print "s1 has data: ",s1.GetData()
print "s2 has data: ",s2.GetData()
s1.SetData(1000)         #function call
s2.SetData(2000)
print "s1 has data: ",s1.GetData(),' '
print "s2 has data: ", s2.GetData(),' '

s1 has data:  500
s2 has data:  500
s1 has data:  1000
s2 has data:  2000


## Program Source Code 8.5, page no:217¶

In [1]:
class SimpleClass:
__IntegerData=None
def __init__(self,data=None):
if data==None:
self.__IntegerData=500   #default constructor
else:
self.__IntegerData=data    #parameterised constructor

def SetData(self,d):
self.__IntegerData=d

def GetData(self):
return self.__IntegerData

#Initializing
s1=SimpleClass()
s2=SimpleClass()
s3=SimpleClass(400)
s4=SimpleClass(600)
print "s1 has data: ",s1.GetData(),' '
print "s2 has data: ",s2.GetData(),' '
print "s3 has data: ",s3.GetData(),' '
print "s4 has data: ",s4.GetData(),' '

s1 has data:  500
s2 has data:  500
s3 has data:  400
s4 has data:  600


## Program Source Code 8.6, page no:218¶

In [1]:
class Fraction:
def_init_num=None   #private members
def_init_denom=None   #private members
def __init__(self,a=0,b=1):
self.__num=a
self.__denom=b
print "Numerator set inside constructor",' ',n
print "Denominator set inside constructor",' ',d

def SetValue(self,a,b):
self.__num=a
self.__denom=b
def GetValue(self,a,b):
a= self.__num
b= self.__denom
return a,b

def __del__(self):       #destructor
pass

n=input("Enter n: ") #user input
d=input("Enter d: ")    #user input
print "Please enter value of numerator and denominator: ",' ',n,d
f=Fraction(n,d)
f.GetValue(n,d)
print "Numerator value retrieved: ", ' ',n
print "Denominator value retrieved: ", ' ',d

Enter n: 3
Enter d: 4
Please enter value of numerator and denominator:    3 4
Numerator set inside constructor   3
Denominator set inside constructor   4
Numerator value retrieved:    3
Denominator value retrieved:    4


## Program Source Code 8.7, page no:221¶

In [3]:
class Fraction:

def __init__(self,a=0,b=1):    #constructor
self.__num=a
self.__denom=b
print "Numerator set inside constructor: ",self.__num
print "Denominator set inside constructor:  ",self.__denom

def GetValue(self,a,b):
return self.__num,self.__denom
n=input("Enter n: ") #user input
d=input("Enter d: ")    #user input
print "Please enter the value of the numerator and denominator: ",n,d
f=Fraction(n,d)
n,d=f.GetValue(n,d)
print "Numerator value retrieved: ", ' ',n
print "Denominator value retrieved: ", ' ',d
n=input("Please enter the value of numerator only: ")
f1=Fraction(n)
n,d=f1.GetValue(n,d)
print "Numerator value retrieved: ", ' ',n
print "Denominator value retrieved: ", ' ',d
print 'ok..now I will create a fraction-no input please'
f2=Fraction()
n,d=f2.GetValue(n,d)
print "Numerator value retrieved: ", ' ',n
print "Denominator value retrieved: ", ' ',d

Enter n: 3
Enter d: 4
Please enter the value of the numerator and denominator:  3 4
Numerator set inside constructor:  3
Denominator set inside constructor:   4
Numerator value retrieved:    3
Denominator value retrieved:    4
Please enter the value of numerator only: 3
Numerator set inside constructor:  3
Denominator set inside constructor:   1
Numerator value retrieved:    3
Denominator value retrieved:    1
ok..now I will create a fraction-no input please
Numerator set inside constructor:  0
Denominator set inside constructor:   1
Numerator value retrieved:    0
Denominator value retrieved:    1


## Program Source Code 8.8, page no:223¶

In [2]:
class Fraction:
def_init_num=None   #private members
def_init_denom=None   #private members

def __init__(self,anotherFraction=None):
if anotherFraction==None:               #normal constructor
self.__num=anotherFraction
self.__denom=anotherFraction
else:                          #copy constructor
self.__num=anotherFraction.self.__num
self.__denom=anotherFraction.self.__denom

#public functions
def SetValue(self,a,b):
self.__num=a
self.__denom=b#refer to IntegerData
def GetValue(self,a,b):
a=self.__num
b=self.__denom
return a,b

f=Fraction()
n=input("Enter n: ") #user input
d=input("Enter d: ")    #user input
print "enter the numerator and denominator: ", '  ',n,d

f.SetValue(n,d) #call function SetValue
print "Numerator value set: ", ' ',n
print "Denominator value set: ", ' ',d
f.GetValue(n,d)     #call function GetData
print "Numerator value retrieved: ", ' ',n
print "Denominator value retrieved: ", ' ',d
print "Now a second clone copy is being created: ",''
f1=f
f1.GetValue(n,d)
print  "Clone's numerator value retrieved: ", ' ',n
print "Clone's denominator value retrieved: ", ' ',d

Enter n: 5
Enter d: 6
enter the numerator and denominator:     5 6
Numerator value set:    5
Denominator value set:    6
Numerator value retrieved:    5
Denominator value retrieved:    6
Now a second clone copy is being created:
Clone's numerator value retrieved:    5
Clone's denominator value retrieved:    6


## Program Source Code 8.9, page no:229¶

In [1]:
def MyNewHandler():
print "Sorry operator new failed to allocate memory"
exit(0)

def _set_new_handler(s):
s()
#In python there is no in-built  _set_new_handler function, so i made this function and passed MyNewHandler function as a parameters
_set_new_handler(MyNewHandler)

Sorry operator new failed to allocate memory


## Program Source Code 8.10, page no:230¶

In [4]:
from ctypes import  c_int,pointer
class Fraction:
def __init__(self,a=0,b=1):
if isinstance(a,int):
c = c_int(a)
d = c_int(b)
self.__num = pointer(c)
self.__denom = pointer(d)
print 'constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0]
else:
c=c_int(a.__num[0])
d = c_int(a.__denom[0])
self.__num = pointer(c)
self.__denom = pointer(d)
print 'copy constructor sets numerator = ', self.__num[0] , ', denominator = ', self.__denom[0]

def __del__(self):
print 'destructor deallocates numerator = ', self.__num[0] , ', denominator = ', self.__denom[0]

n = input("Please enter values of numerator: ")
d = input("Please enter values of denominator: ")
f = Fraction(n,d)
print 'Please enter another set of '
n = input("numerator: ")
d = input("denominator: ")
print 'Creating fraction *pf : '
pf = Fraction(n,d)
print 'Now a clone copy (f2) created from *pf: '
f2 = Fraction(pf)
print 'Now another clone copy (*pf2) created from f:'
pf2 = Fraction(f)
print '*pf2 is being destroyed:'
del pf2
print '*pf is being destroyed:'
del pf
print 'now objects f2 and f automatically destroyed : '

Please enter values of numerator: 3
Please enter values of denominator: 4
constructor sets numerator =  3 , denominator =  4
numerator: 5
denominator: 6
Creating fraction *pf :
constructor sets numerator =  5 , denominator =  6
Now a clone copy (f2) created from *pf:
copy constructor sets numerator =  5 , denominator =  6
Now another clone copy (*pf2) created from f:
copy constructor sets numerator =  3 , denominator =  4
*pf2 is being destroyed:
*pf is being destroyed:
now objects f2 and f automatically destroyed :


## Program Source Code 8.11, page no:234¶

In [4]:
def Memfail(self,s):
print "Sorry Unable to allocate memory"
sys.exit(0)

MAX_SIZE = 60 + 1

MAX_SIZE=[[0 for col in range(MAX_SIZE)]for row in range(MAX_SIZE)]
nChar=0
chArr="Hello"

print "Please input a string( 60 characters max.): ",chArr

nChar=len(chArr)+1
szStr=chArr
print "required memory space for",nChar,
print "characters"
chArr=szStr           #string copy
szStr=chArr
print "String copied in allocated space: ",szStr
print "Memory space dellocated"

Please input a string( 60 characters max.):  Hello
required memory space for 6 characters
String copied in allocated space:  Hello
Memory space dellocated


## Program Source Code 8.12, page no:236¶

In [2]:
class Fraction:

def __init__(self,a=0,b=1): #constructor
self.__num=a
self.__denom=b

def __del__(self):     #destructor
pass

def GetValue(self,a,b):
a=self.__num
b=self.__denom
return self.__num,self.__denom
n=4
d=5
f=Fraction(4,5)
n,d=f.GetValue(n,d)


## Program Source Code 8.13, page no:239¶

In [1]:
class X:
__sa=20    #initialising static member
a = None
def __init__(self):
self.a=None    #public member
def f(self,b):
a=30
print "Global a= ",b
print "Local a= ",a
print "Nonstatic member a= ",self.a
print "static member sa= ",self.__sa

a=10

aXobj=X()
aXobj.a=40
aXobj.f(a)

Global a=  10
Local a=  30
Nonstatic member a=  40
static member sa=  20

In [ ]: