Chapter Ten : Object-oriented programming

Example 10.1 page no :105

In [1]:
s = "hello"  # length() == 5
print s
print len(s)
hello
5

EXAMPLE 10.2 page no : 106

In [2]:
s = "Hello"

s = s.lower()
print s
hello

example 10.3 page no :107

In [2]:
class EmcCollection:
   
    def insert(self,t):
        pass
    
    def operator(self, coll):
        pass

class EmcArrayCollection(EmcCollection):
    initialSize = 10

    def __init__(self,maxsize):
        pass

"""
class InvalidCollectionType(EmcException):
    
    def __init__(self,i):
        pass
        """

class EmcCollectionFactory:
    def __init__(self):
        pass

    def create(self,type):
        pass

    def createArray(self):
        pass

example 10.4 page no :109

In [32]:
class EmcCollectionFactory:
    def __init__(self):
        pass

    def create(self,type):
        pass

    def createArray(self):
        pass

factory = EmcCollectionFactory()
collection = factory.create("Array")
collection.insert(42); # EmcArrayCollection<int>::insert() is called
print collection
None

example 10.5 page no : 111

In [5]:
class EmcCollection:
    def __del__(self):
        pass

class EmcArrayCollection( EmcCollection):
    def __del__(self):
        pass

example 10.6 page no :112

In [6]:
class EmcLogged:
    def writeClassName(self,o):
        pass

    def writeValue(self,o):
        pass

    def logMessage(self,message):
        pass

    def __del__(self):
        pass

class EmcLoggedCollection(EmcCollection):

    def writeValue(self,o):
        pass

    def __del__(self):
        pass        

EXAMPLE 10.7 Page no : 117

In [7]:
class EmcIntStack:
    def empty(self):
        pass

    def full(self):
        pass
        
    def top(self):
        pass
        
    def push(self,i):
        pass
    
    def pop(self):
        pass

EXAMPLE 10.8 Page no : 117

In [8]:
def makeString(stack):
    returnValue = ''
    copy = stack
    while (not copy.empty()):
        # loop condition makes precondition valid
        print copy.pop() # Precondition: ! copy.empty()
        returnValue += copy.pop

    return returnValue;

EXAMPLE 10.9 page no : 118

In [9]:
class EmcString:
    def cStr(self):
        pass
        # cStr() returns 0-terminated string

    def length(self):
        pass

EXAMPLE 10.10 page no : 119

In [10]:
class EmcCollection:
    def __del__(self):
        pass

    def insert(self,T):
        pass

    def clear(self):
        pass

    def remove(self):
        pass

EXAMPLE 10.11 page no : 120

In [36]:
class EmcCollectionFactory:
    def __init__(self):
        pass

    def create(self,type):
        pass

    def createArray(self):
        pass
    
factrory = EmcCollectionFactory()
collection = factory.create("Array");
if (not collection.isFull()):
    collection.insert(42);

EXAMPLE 10.12 page no : 121

In [12]:
def insertObject(c,element):
  
    if (not c.isFull()):
        c.insert(element)
        return True
    return False

EXAMPLE 10.13 Page no : 121

In [13]:
def insert(T):
    pass

EXAMPLE 10.14 page no : 123

In [14]:
class EmcCollection:
    pass

EXAMPLE 10.15 page no : 124

In [15]:
class EmcCollection:
    def templateRequirements(self):
        pass

    def templateRequirements(self):
       
        t1 = T1()
      
        t2 =t1

        t2 = t1;
        # Assignable
        b = (t2 == t1); # EqualityComparable
In [ ]: