Chapter Seven : The class interface

EXAMPLE 7.1 page no : 57

In [1]:
class Point:
    def __init__(self,x,y):
        self.xM = x
        self.yM = y

    def x(self):
        return self.xM
        
    def y(self):
        return self.yM

    def x_(self,x):
        self.xM = x
        
    def y_(self,y):
        self.yM = y

    def __add__(self,p2):    
        return Point(self.x() + p2.x(), self.y() + p2.y())

example 7.2 page no : 59

In [2]:
def valueFunc(t):
    pass

def pointerFunc(tp):
    pass

def referenceFunc(tr):
    pass

example 7.3 page no : 60

In [3]:
def func1(c):
    pass

def func2(i):
    pass
    
def func3(d):
    pass
    
def func(c):
    pass

EXAMPLE 7.4 page no : 61

In [4]:
class EmcMathVector:
    def __init__(self,x,y):
        self.xM = x
        self.yM = y

    def __mul__(self,i):
        self.xM *= i
        self.yM *= i

    def x(self):
        return self.xM
        
    def y(self):
        return self.yM

    def x_(self,x):
        self.xM = x
        
    def y_(self,y):
        yM = y

a = EmcMathVector(5,10)
print a.xM,a.yM
a * 5
print a.xM,a.yM
5 10
25 50

EXAMPLE 7.5 page no : 63

In [5]:
class vector:
    def __init__(self,first,last):
        pass

    def begin(self):
        pass

    def push_back(self,x):
        pass
    
    def insert(self,x):
        pass

EXAMPLE 7.6 page no : 64

In [6]:
class basic_ofstream:
    def __init__(self,x,y):
        self.x = x
        self.y = y

    def print_(self):
        print self.x , ", " , self.y;


v = basic_ofstream(1.2, 5.5);
v.print_()
1.2 ,  5.5

EXAMPLE 7.7 page no : 65

In [7]:
def uselessPrint(v):
    # NO: Compile error
    print v.x() , ", " , v.y();

example 7.8 page no : 65

In [8]:
array = ['','','']
# assign to first element
arrayPointer =  42 #*(arrayPointer = array) = 42
print arrayPointer
42

EXAMPLE 7.10 page no : 67

In [9]:
def arraySum(array,first,last):
    s = 0
    for i in array:
        # It is possible to update first since
        # it has not been declared const.
        s += i
    return s;
    
print arraySum([5,4,3,2],5,2)    
14

EXAMPLE 7.11 page no : 68

In [10]:
class EmcStack:
    def __init__(self):
        self.repM = []
        self.allocatedM = 0
        self.topM = 0
        
    def push(t):
        if self.topM == allocatedM : # allocate more memory
            pass    #size_t newSize = 2 * allocatedM;
        self.repM.append(t)
        self.topM +=1

EXAMPLE 7.12 page no : 69

In [11]:
message1 = "Calling Orson";
message2 = "Ice Hockey";
message3 = "Terminator";
message4 = "I like candy"

print message1
print message2
print message3
print message4
Calling Orson
Ice Hockey
Terminator
I like candy

EXAMPLE 7.13 page no : 70

In [12]:
class UselessString:
    def __init__(self,c=None):
        pass
        
    def cStr(self):
        pass
        
    def length(self):
        pass
        
    def at(self,index):
        pass
        
    def print_(self):
        print self.s 

EXAMPLE 7.14 page no : 71

In [13]:
class Silly:
    def __init__(self,val):
        self.valM = val
        
    def me(self,s):
        pass

EXAMPLE 7.15 page no : 72

In [14]:
name = "John Bauer";
print name[0]
J

EXAMPLE 7.16 page no : 75

In [15]:
cosmonaut = "Juri Gagarin"
c = 'a';
cValue = c in cosmonaut # cValue == true
uri = "uri"
uriValue = uri in cosmonaut # uriValue == true
print cValue
print uriValue
True
True

EXAMPLE 7.17 page no : 76

In [16]:
def max_(x,y):
    if (x > y): # could use: < instead
        # We also expect that:
        # y < x
        return x;
    else:
        # We also expect that:
        # x <= y
        return y;

x = 42;
y = 0;
print max(x,y)
42

EXAMPLE 7.18 page no : 76

In [17]:
'''
Implementation of closely related operators
Python has inbuilt this facility. No need to write functions for that.
''' 
Out[17]:
'\nEXAMPLE 7.18 page no : 76\nImplementation of closely related operators\nPython has inbuilt this facility. No need to write functions for that.\n'

EXAMPLE 7.19 page no : 77

In [18]:
class Base:
    def f(char):
        pass
    def f(i):
        pass
    def v(char):
        pass

class Derived(Base):
    def __init__(self):
        pass

    def f(self,i):
        pass
    
    def v(self,c):
        self.f(c)

EXAMPLE 7.20 page no : 79

In [19]:
class EmcCollection:
    def isEqual(self):
        pass
        
class EmcBoundedCollection(EmcCollection):

    def isEqual(a):
        pass

example 7.21 page no : 80

In [20]:
def f(x,y = 2):
    print x
    print y
 
f(1)
f(2,3)
1
2
2
3

EXAMPLE 7.22 page no : 80

In [21]:
class RanDraw:
    def __init__(self, limit,seed, t = 'Good' ):
        pass # Default argument for t in class definition

EXAMPLE 7.23 page no : 82

In [22]:
class Other:
    def __init__(self,a): #No implicit conversion from Any
        pass
        
    def foo(self,o):
        pass

example 7.24 page no : 82

In [23]:
class EmcString:
    def cStr(): # conversion to const char*
        pass

    def log(cp):   
        pass

magicPlace ="Ngoro-Ngoro crater at dusk"
print magicPlace # Explicit conversion from String to const char*
Ngoro-Ngoro crater at dusk
In [ ]: